How to hide rows in react-data-table-component based on a condition of row element?

117 Views Asked by At

I want to hide a row depened on its column's value in react-data-table-component table, But i cant find a way to do this.

I want to hide the row depend on condition as i mentioned. If there is a best solution that you have then you can help me to figure it out.

1

There are 1 best solutions below

0
Akeel Ahmed Qureshi On

You can use the conditionalRowStyles property. This property allows you to apply custom styles to rows based on certain conditions:-

example:-

const data = [
  { id: 1, name: 'John', age: 25 },
  { id: 2, name: 'Jane', age: 30 },
  { id: 3, name: 'Doe', age: 20 },
];

const columns = [
  { name: 'ID', selector: 'id', sortable: true },
  { name: 'Name', selector: 'name', sortable: true },
  { name: 'Age', selector: 'age', sortable: true },
];

const MyDataTable = () => {
  const conditionalRowStyles = [
    {
      when: row => row.age < 25,
      style: {
        display: 'none',
      },
    },
  ];

  return (
    <DataTable
      title="My DataTable"
      columns={columns}
      data={data}
      conditionalRowStyles={conditionalRowStyles}
    />
  );
};

export default MyDataTable;

the conditionalRowStyles array contains an object with a when property that represents the condition to check for each row. In this case, it checks if the age of the row is less than 25. If the condition is true, it applies the specified styles to hide the row.