如何在 MUI X 数据表格中排序时不对行进行排序

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

How to not sort a row while sorting in MUI X Data Grid

问题

当前行为: 在排序到降序时,“Total”行出现在顶部

期望行为: 我希望在对列进行排序时,“Total”行不被排序

英文:

如何在 MUI X 数据表格中排序时不对行进行排序

Question: Is there a way to not sort the "Total" row while Sorting? Thanks in advance

   <DataGrid
     rows={dataSource}
     columns={dataColumns}
     initialState={{
         sorting: {
             sortModel: [{field: 'Total', sort: 'desc'}]
         }
     }}
     components={{
        Toolbar: CustomToolbar,
        NoRowsOverlay: EmptyRows,
     }}
     getRowId={(row) => row.value}
     filterMode="client"
     filterModel={filterModal}
     onFilterModelChange={(model) => {
          setFilterModal(model);
     }}
   />

Current Behavior: While sorting to descending the "Total" row comes at top

Expected Behavior: I want the "Total" row to be not sorted while sorting the columns

答案1

得分: 1

另一个解决方案,可以完全绕过排序问题,是将总计作为自定义页脚组件添加,而不是作为一行。

如果您想升级到高级版,还有聚合功能,基本上可以立即提供一个总计行。

英文:

Another solution, which would bypass the sorting problem entirely, would be to add the Totals as a Custom footer component instead of a row.

If you want to go Premium, there's also the Aggregation feature, which basically provides a totals row out of the box.

答案2

得分: 0

关于我而言,最自然的解决方案将是控制排序或具有一些属性,可以排除某些行不参与排序。但我在文档中没有找到这方面的信息。您可以覆盖排序的比较器,但比较器只接收到单元格的值。

根据Data Grid文档,您可以将行固定在顶部或底部。在固定行之后,它将不会参与排序和筛选。

<DataGrid
 rows={dataSource.slice(1)}
 pinnedRows={{top: [dataSource[0]]}}
 columns={dataColumns}
 initialState={{
     sorting: {
         sortModel: [{field: 'Total', sort: 'desc'}]
     }
 }}
 components={{
    Toolbar: CustomToolbar,
    NoRowsOverlay: EmptyRows,
 }}
 getRowId={(row) => row.value}
 filterMode="client"
 filterModel={filterModal}
 onFilterModelChange={(model) => {
      setFilterModal(model);
 }}
/>

问题在于它会固定在顶部或底部。您可以使用CSS来覆盖固定效果,并尝试创建更优雅的CSS解决方案以满足您的需求,但仍然这种方法可能有些不正规:

.MuiDataGrid-pinnedRows--top {
  position: relative !important;
  box-shadow: none !important;
}

您还可以在此处找到代码示例,查看index.css和demo.js文件。

英文:

As for me, the most natural solution would be to control sorting or have some property that will exclude some rows from sorting. But I don't see this in the documentation. You can override the comparator for sorting, but the comparator doesn't receive the full row, only the cell value.

As I see in the Data Grid documentation - you can pin the row to the top or to the bottom. After pinning the row will be excluded from the sorting and filtering.

   &lt;DataGrid
     rows={dataSource.slice(1)}
     pinnedRows={{top: [dataSource[0]]}}
     columns={dataColumns}
     initialState={{
         sorting: {
             sortModel: [{field: &#39;Total&#39;, sort: &#39;desc&#39;}]
         }
     }}
     components={{
        Toolbar: CustomToolbar,
        NoRowsOverlay: EmptyRows,
     }}
     getRowId={(row) =&gt; row.value}
     filterMode=&quot;client&quot;
     filterModel={filterModal}
     onFilterModelChange={(model) =&gt; {
          setFilterModal(model);
     }}
   /&gt;

The problem here is that it will stick to the top or bottom. You can use CSS to override pinning, and you can try to make a more elegant CSS solution that fit your needs, but still, it is kind of a hacky way to solve the issue:

.MuiDataGrid-pinnedRows--top {
  position: relative !important;
  box-shadow: none !important;
}

Also, you can find the code example here look into index.css and demo.js files

答案3

得分: 0

我已经找到了解决方法,而不使用Pro,我可以使用sortComparator函数来进行自定义排序。

参考代码可用于一个列:

headerName: 'Total',
field: 'Total',
type: 'number',
sortComparator: (value1, value2, params1, params2) => {
    if (params1.id === 'Total' || params2.id === 'Total') {
       return 0;
    }
    else return value1 - value2
}

或者,您可以编写一个函数,使用参考代码并将其传递给DataGrid属性sortComparator

<DataGrid 
 sortComparator={...您的函数在此}
/>
英文:

I got the solution for my question without using Pro as I can use the sortComparator function to do custom sorting

Reference code can be used for a column

headerName: &#39;Total&#39;,
field: &#39;Total&#39;,
type: &#39;number&#39;,
sortComparator: (value1, value2, params1, params2) =&gt; {
    if (params1.id === &#39;Total&#39; || params2.id === &#39;Total&#39;) {
       return 0;
    }
    else return value1 - value2
}

or you can write a function with the reference code and pass it to DataGrid prop sortComparator

&lt;DataGrid 
 sortComporator={...Your Function here}
/&gt;

huangapple
  • 本文由 发表于 2023年6月29日 14:42:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578612.html
匿名

发表评论

匿名网友

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

确定