Search code examples
reactjsmaterial-uimui-x-data-gridmui-x

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


Current Behavior Image

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


Solution

  • 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: 'Total',
    field: 'Total',
    type: 'number',
    sortComparator: (value1, value2, params1, params2) => {
        if (params1.id === 'Total' || params2.id === 'Total') {
           return 0;
        }
        else return value1 - value2
    }
    

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

    <DataGrid 
     sortComporator={...Your Function here}
    />