diff --git a/docs/src/pages/demos/tables/EnhancedTable.js b/docs/src/pages/demos/tables/EnhancedTable.js index 067dd3754cc00c..a51434f0ca6b55 100644 --- a/docs/src/pages/demos/tables/EnhancedTable.js +++ b/docs/src/pages/demos/tables/EnhancedTable.js @@ -25,6 +25,12 @@ function createData(name, calories, fat, carbs, protein) { return { id: counter, name, calories, fat, carbs, protein }; } +function getSorting(order, orderBy) { + return order === 'desc' + ? (a, b) => (b[orderBy] < a[orderBy] ? -1 : 1) + : (a, b) => (a[orderBy] < b[orderBy] ? -1 : 1); +} + const columnData = [ { id: 'name', numeric: false, disablePadding: true, label: 'Dessert (100g serving)' }, { id: 'calories', numeric: true, disablePadding: false, label: 'Calories' }, @@ -197,7 +203,7 @@ class EnhancedTable extends React.Component { createData('Marshmallow', 318, 0, 81, 2.0), createData('Nougat', 360, 19.0, 9, 37.0), createData('Oreo', 437, 18.0, 63, 4.0), - ].sort((a, b) => (a.calories < b.calories ? -1 : 1)), + ], page: 0, rowsPerPage: 5, }; @@ -211,12 +217,7 @@ class EnhancedTable extends React.Component { order = 'asc'; } - const data = - order === 'desc' - ? this.state.data.sort((a, b) => (b[orderBy] < a[orderBy] ? -1 : 1)) - : this.state.data.sort((a, b) => (a[orderBy] < b[orderBy] ? -1 : 1)); - - this.setState({ data, order, orderBy }); + this.setState({ order, orderBy }); }; handleSelectAllClick = (event, checked) => { @@ -277,31 +278,34 @@ class EnhancedTable extends React.Component { rowCount={data.length} /> - {data.slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage).map(n => { - const isSelected = this.isSelected(n.id); - return ( - this.handleClick(event, n.id)} - role="checkbox" - aria-checked={isSelected} - tabIndex={-1} - key={n.id} - selected={isSelected} - > - - - - - {n.name} - - {n.calories} - {n.fat} - {n.carbs} - {n.protein} - - ); - })} + {data + .sort(getSorting(order, orderBy)) + .slice(page * rowsPerPage, page * rowsPerPage + rowsPerPage) + .map(n => { + const isSelected = this.isSelected(n.id); + return ( + this.handleClick(event, n.id)} + role="checkbox" + aria-checked={isSelected} + tabIndex={-1} + key={n.id} + selected={isSelected} + > + + + + + {n.name} + + {n.calories} + {n.fat} + {n.carbs} + {n.protein} + + ); + })} {emptyRows > 0 && (