How to accomplish customRender in React mui-Datatable

dhwanika chhabria picture dhwanika chhabria · May 25, 2019 · Viewed 7.6k times · Source

I want to customize the row hat data in mui-datatble in such a way that if I get Yes in the option, background color should be Red and if I say No, background color should be Blue. I am using mui-datatable first time.

I am unable to use customRowRender or customRender. How do we use it in mui-datatable

import React from 'react';
import MUIDataTable from "mui-datatables";

class Datatable extends React.Component {
    render() {
        const columns = [
            {
             name: "name",
             label: "Name",
             options: {
              filter: true,
              sort: true,
              customRowRender:(data, dataIndex, rowIndex) => {
                console.log('data' + data);
                return (
                  <div>
                    {data}{' '}{dataIndex}{' '}{rowIndex}
                  </div>
                );
              }
             }
            },
            {
             name: "company",
             label: "Company",
             options: {
              filter: true,
              sort: false,
             }
            }
           ];

           const data = [
            { name: "Joe James", company: "Test Corp" },
            { name: "John Walsh", company: "Test Corp" }
           ];

           const options = {
             filterType: 'checkbox',
           };
        return (
            <React.Fragment>
<MUIDataTable
  title={"Employee List"}
  data={data}
  columns={columns}
  options={options}
/>
            </React.Fragment>
            );
   }}
export default Datatable;

I should be able to render data in customRender where I will add a conditional render with a <div> and style depending on Yes/No

Answer

Stephane L picture Stephane L · May 25, 2019

You have put the customRowRender property in the columns object, according to the doc it should be in the options object :

   const options = {
             filterType: 'checkbox',
             customRowRender:(data, dataIndex, rowIndex) => {
                console.log('data' + data);
                return (
                  <div>
                    {data}{' '}{dataIndex}{' '}{rowIndex}
                  </div>
                );
              }
    };

    // render
    <MUIDataTable
      title={"Employee List"}
      data={data}
      columns={columns}
      options={options}
    />

But this is for rendering a custom row, if you want to render a custom column, then you can use customBodyRender property in the columns object.