最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Custom sort for custom render cell Data Grid Material UI - Stack Overflow

programmeradmin4浏览0评论

I'm using Material UI V4 Data Grid ponent in a React Js app.

The Data Grid ponent has required rows (list type) and columns (list type) props.

Here is a sample of a row item:

const rows = [
  {
    id: 1,
    customerName: "Silvestr Irma",
    carModel: {
      name: "Centurion",
      color: "red"
    },
    location: {
      name: "Belgium",
      address: "avenue Verheyen 93"
    }
  },
];

and here is a sample of the column list:

const columns = [
    {
      field: "customerName",
      headerName: "Custom Name",
      flex: 1,
      minWidth: 200
    },
    {
      field: "carModel",
      headerName: "Car Model",
      flex: 1,
      minWidth: 200,
      renderCell: (params) => (
        <Typography variant="subtitle2" className={classes.bolderText}>
          {params.value.name}
        </Typography>
      )
    },
    {
      field: "location",
      headerName: "Location",
      flex: 1,
      minWidth: 300,
      renderCell: (params) => (
        <div>
          <Typography variant="subtitle2" className={classes.bolderText}>
            {params.value.name}
          </Typography>
          <Typography variant="subtitle2" className={classes.ligtherText}>
            {params.value.address}
          </Typography>
        </div>
      )
    }
  ];

If we sort the Data Grid ponent based on the customer name column, the sort works fine for both ascendant and descendant as we can see in the images below:

However, the sort doesn't work fine for columns that have render cell field (car model and location columns) as you can see in the images below:

So how I implement custom sort for columns that have render cell field? For example the car model name column (Centurion, Centaur, and Buffalo) and the location name column (Belgium, Germany, and Spain).

Here is the full code

I'm using Material UI V4 Data Grid ponent in a React Js app.

The Data Grid ponent has required rows (list type) and columns (list type) props.

Here is a sample of a row item:

const rows = [
  {
    id: 1,
    customerName: "Silvestr Irma",
    carModel: {
      name: "Centurion",
      color: "red"
    },
    location: {
      name: "Belgium",
      address: "avenue Verheyen 93"
    }
  },
];

and here is a sample of the column list:

const columns = [
    {
      field: "customerName",
      headerName: "Custom Name",
      flex: 1,
      minWidth: 200
    },
    {
      field: "carModel",
      headerName: "Car Model",
      flex: 1,
      minWidth: 200,
      renderCell: (params) => (
        <Typography variant="subtitle2" className={classes.bolderText}>
          {params.value.name}
        </Typography>
      )
    },
    {
      field: "location",
      headerName: "Location",
      flex: 1,
      minWidth: 300,
      renderCell: (params) => (
        <div>
          <Typography variant="subtitle2" className={classes.bolderText}>
            {params.value.name}
          </Typography>
          <Typography variant="subtitle2" className={classes.ligtherText}>
            {params.value.address}
          </Typography>
        </div>
      )
    }
  ];

If we sort the Data Grid ponent based on the customer name column, the sort works fine for both ascendant and descendant as we can see in the images below:

However, the sort doesn't work fine for columns that have render cell field (car model and location columns) as you can see in the images below:

So how I implement custom sort for columns that have render cell field? For example the car model name column (Centurion, Centaur, and Buffalo) and the location name column (Belgium, Germany, and Spain).

Here is the full code https://codesandbox.io/s/stackoverflow-custom-sort-data-grid-material-ui-dp609

Share Improve this question asked Dec 17, 2021 at 7:12 Jabal LogianJabal Logian 2,3406 gold badges27 silver badges54 bronze badges 1
  • 2 Thank you for providing an example to work from -- that makes life SO much easier when trying to help! – Steve G Commented Dec 17, 2021 at 9:08
Add a ment  | 

1 Answer 1

Reset to default 11

As you've probably guessed, with the renderCell, you'll need to create a custom function (sortComparator) so the DataGrid knows how to sort the columns. In your case, this sort might suffice:

    {
      field: "location",
      headerName: "Location",
      flex: 1,
      minWidth: 300,
      renderCell: (params) => (
        <div>
          <Typography variant="subtitle2" className={classes.bolderText}>
            {params.value.name}
          </Typography>
          <Typography variant="subtitle2" className={classes.ligtherText}>
            {params.value.address}
          </Typography>
        </div>
      ),
      sortComparator: (v1, v2) => v1.name.localeCompare(v2.name)
    }

Working example: https://codesandbox.io/s/stackoverflow-custom-sort-data-grid-material-ui-forked-2kw12?file=/src/App.jsx:416-1248

发布评论

评论列表(0)

  1. 暂无评论