admin管理员组文章数量:1298180
Hello I have a DataGrid with a column that has a custom header. This header is a Select. What I want to do is every time the user selects an option the column should be sorted desc. The renderHeader looks like this
renderHeader: (params) => {
return <CategoryPickerHeader value={category} handleChange={setCategory} />;
},
I know the DataGrid api has a couple methods for sorting () sortColumn() & applySorting()
But I haven't found any example of how to use those api methods.
Can someone provide an example or knows how to use the DataGrid api?
Thanks in advance!
Hello I have a DataGrid with a column that has a custom header. This header is a Select. What I want to do is every time the user selects an option the column should be sorted desc. The renderHeader looks like this
renderHeader: (params) => {
return <CategoryPickerHeader value={category} handleChange={setCategory} />;
},
I know the DataGrid api has a couple methods for sorting (https://v4.mui./api/data-grid/grid-api/#main-content) sortColumn() & applySorting()
But I haven't found any example of how to use those api methods.
Can someone provide an example or knows how to use the DataGrid api?
Thanks in advance!
Share Improve this question asked Dec 15, 2021 at 18:27 ZayttZaytt 1131 gold badge1 silver badge4 bronze badges2 Answers
Reset to default 6Visit this page, this has an example: https://codesandbox.io/s/ugeb8?file=/demo.js
IMPORTANT : pass the arguments to property sortModel, is this answer
import * as React from 'react';
import { DataGrid } from '@mui/x-data-grid';
import { useDemoData } from '@mui/x-data-grid-generator';
export default function BasicSortingGrid() {
const { data } = useDemoData({
dataSet: 'Commodity',
rowLength: 10,
maxColumns: 6,
});
const [sortModel, setSortModel] = React.useState([
{
field: 'modity',
sort: 'asc',
},
]);
return (
<div style={{ height: 400, width: '100%' }}>
<DataGrid
{...data}
sortModel={sortModel}
onSortModelChange={(model) => setSortModel(model)}
/>
</div>
);
}
Here's how i added default sort with DataGrid ponent:
<DataGrid
initialState={{
sorting: {
sortModel: [{ field: 'rating', sort: 'desc' }],
},
}}
/>
本文标签: javascriptHow to sort a Material UI DataGrid column programaticallyStack Overflow
版权声明:本文标题:javascript - How to sort a Material UI DataGrid column programatically? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741314524a2371837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论