Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EuiDataGrid add column sorting #2278

21 changes: 20 additions & 1 deletion src-docs/src/views/datagrid/datagrid.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,31 @@ export default class DataGridContainer extends Component {
super(props);

this.state = {
sortingColumns: [],
data,
pagination: {
pageIndex: 0,
pageSize: 50,
},
};
}

setSorting = sortingColumns => {
const sortedData = [...data].sort((a, b) => {
for (let i = 0; i < sortingColumns.length; i++) {
const column = sortingColumns[i];
const aValue = a[column.id];
const bValue = b[column.id];

if (aValue < bValue) return column.direction === 'asc' ? -1 : 1;
if (aValue > bValue) return column.direction === 'asc' ? 1 : -1;
}

return 0;
});
this.setState({ sortingColumns, data: sortedData });
};

setPageIndex = pageIndex =>
this.setState(({ pagination }) => ({
pagination: { ...pagination, pageIndex },
Expand All @@ -102,14 +120,15 @@ export default class DataGridContainer extends Component {
);

render() {
const { pagination } = this.state;
const { data, pagination, sortingColumns } = this.state;

return (
<EuiDataGrid
aria-label="Top EUI contributors"
columns={columns}
rowCount={data.length}
renderCellValue={({ rowIndex, columnId }) => data[rowIndex][columnId]}
sorting={{ columns: sortingColumns, onSort: this.setSorting }}
pagination={{
...pagination,
pageSizeOptions: [5, 10, 25],
Expand Down
Loading