admin管理员组

文章数量:1393858

I am trying to sort data with nested values in a Data Grid But getting undefined on sortComparator of Data Grid Columns

Code: Column Data Setup:

   { 
    headerName: 'Title',
    field: `${this.props.type}.title`,
    width: 500,
    type: "string",
    renderCell: (valueReceived) => this.getImageorVideoLogo(valueReceived.row),
    sortComparator: this.titleSorting
   }


titleSorting = (a,b)=>{
       console.log(a);
       console.log(b);
}

Data Grid:

               <DataGrid
                    rows={rowsDataGrid}
                    columns={columnsDataGrid}
                    ponents={{
                        Toolbar: this.getSearchTextField
                    }}
                    pageSize={5}
                    rowsPerPageOptions={[5]}
                   
                    // checkboxSelection
                    // disableSelectionOnClick
                    autoHeight
                />

Problem both console print undefined Ideally it should give either the whole row a and row b or atleast row a column data and row b column data

I am trying to sort data with nested values in a Data Grid But getting undefined on sortComparator of Data Grid Columns

Code: Column Data Setup:

   { 
    headerName: 'Title',
    field: `${this.props.type}.title`,
    width: 500,
    type: "string",
    renderCell: (valueReceived) => this.getImageorVideoLogo(valueReceived.row),
    sortComparator: this.titleSorting
   }


titleSorting = (a,b)=>{
       console.log(a);
       console.log(b);
}

Data Grid:

               <DataGrid
                    rows={rowsDataGrid}
                    columns={columnsDataGrid}
                    ponents={{
                        Toolbar: this.getSearchTextField
                    }}
                    pageSize={5}
                    rowsPerPageOptions={[5]}
                   
                    // checkboxSelection
                    // disableSelectionOnClick
                    autoHeight
                />

Problem both console print undefined Ideally it should give either the whole row a and row b or atleast row a column data and row b column data

Share Improve this question edited Mar 27 at 7:25 Olivier Tassinari 8,6916 gold badges25 silver badges28 bronze badges asked May 18, 2022 at 5:53 ash1102ash1102 4571 gold badge5 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Try with a custom field and the valueGetter param :

{ 
    headerName: 'Title',
    field: "CustomField",
    valueGetter: () => this.props.type.title,
    width: 500,
    type: "string",
    renderCell: (valueReceived) => this.getImageorVideoLogo(valueReceived.row),
    sortComparator: this.titleSorting
}

And add a parison algorithm in your parator function such as

const titleSorting = (a, b) => {
   a - b;
}

本文标签: javascriptSorting not working for MUI X Data Grid Data Grid sortComparator giving undefinedStack Overflow