0

I am trying to show data of a Column inside chip I tied to implement Chip to
code : https://stackblitz.com/edit/react-hymlnb?file=demo.tsx

webLink : https://react-hymlnb.stackblitz.io

I tried to implement using

import Chip from '@mui/material/Chip';

but could not implement Chip to that particular row.

Expectation :

enter image description here

Code :

import * as React from 'react';
import Box from '@mui/material/Box';
import { DataGrid, GridColDef, GridValueGetterParams } from '@mui/x-data-grid';

const columns: GridColDef[] = [
  { field: 'id', headerName: 'ID', width: 90 },
  {
    field: 'Name',
    headerName: 'Name',
    width: 150,
    editable: true,
  },
  {
    field: 'Status',
    headerName: 'status',
    width: 150,
    editable: true,
  },
];

const rows = [
  { id: 1, Status: 'Filled', Name: 'Jon' },
  { id: 2, Status: 'Filled', Name: 'Cersei' },
  { id: 3, Status: 'Rejected', Name: 'Jaime' },
  { id: 4, Status: 'Rejected', Name: 'Arya' },
  { id: 5, Status: 'Rejected', Name: 'Daenerys' },
  { id: 6, Status: 'Rejected', Name: 'Anurag' },
  { id: 7, Status: 'Filled', Name: 'Ferrara' },
  { id: 8, Status: 'Filled', Name: 'Rossini' },
  { id: 9, Status: 'Filled', Name: 'Harvey' },
];

export default function DataGridDemo() {
  return (
    <Box sx={{ height: 400, width: '100%' }}>
      <DataGrid rows={rows} columns={columns} pageSize={5} />
    </Box>
  );
}
NearHuscarl
  • 66,950
  • 18
  • 261
  • 230

1 Answers1

0

You need to add a function prop

renderCell: (params) => {}

to your column definition.

so your column definition would look like this:

const GridColDef = [
  {
    field: 'id',
    headerName: 'Id',
    width: 90
  },
  {
    field: 'name',
    headerName: 'Name',
    width: 150,
    editable: true,
  },
  {
    field: 'status',
    headerName: 'Status',
    width: 150,
    editable: true,
    renderCell: (params) => {
      const isRejected = params.value === "Rejected";
      return <Chip icon={isRejected ? <WarningIcon/> : <CheckIcon/>}  label={params.value} variant={"outlined"} color={isRejected ? "error" : "success"} />;
    }
  },
];

I am using Material UI <Chip/> component

Also, check the mess with capitalization in field names and header names as this might bring issues representing data in the MUI DataGrid