如何在 MUI X 数据网格中按日期和时间排序

huangapple go评论62阅读模式
英文:

How to sort by date and time in data grid MUI X

问题

在MaterialUI X数据表格中,我有一个日期列的格式为DD/MM/YYYY HH:mm:ss,现在(数据表格的默认排序)只按日期进行排序,每天的时间混乱了,想知道是否有一种格式化的方法,以便同时显示日期和时间。

列的图片

数据表格标签

<DataGrid
    rows={rows}
    columns={columns}
    autoPageSize
    components={{
        LoadingOverlay: Loader,
        Toolbar: QuickSearchToolbar,
    }}
    loading={listLoading}
    initialState={{
        sorting: {
            sortModel: [{ field: "Date", sort: "asc" }],
        },
    }}/>

我尝试使用MUIX数据表格的文档中的sortComparator示例,但没有起作用。

英文:

in MaterialUI X data grid i have a column of date in the format of DD/MM/YYYY HH:mm:ss and now (the defualt sort of the data grid) its sorting only by the date and the time of each day is messed up, wanted to know if there is a way to format so it will be date and time

picture of the column

the data grid tag

&lt;DataGrid
         rows={rows}
         columns={columns}
         autoPageSize
         components={{
           LoadingOverlay: Loader,
           Toolbar: QuickSearchToolbar,
           }}
         loading={listLoading}
         initialState={{
            sorting: {
               sortModel: [{ field: &quot;Date&quot;, sort: &quot;asc&quot; }],
            },
         }}/&gt;

i tried to use the docs of the MUIX data grid with the sortComparator example but didnt work

答案1

得分: 0

默认情况下,MUI会将日期按照字符串进行排序。您应该先将其转换为ISO字符串,进行排序,然后在列定义中使用 valueFormatter 来显示您想要的格式。

英文:

By default MUI sort date like a string. You should first convert it to ISO string, sort it and then use valueFormatter in column definition to show the format that you want.

huangapple
  • 本文由 发表于 2023年6月27日 21:19:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76565319.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定