Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
146
rated 0 times [  148] [ 2]  / answers: 1 / hits: 9876  / 5 Years ago, thu, november 14, 2019, 12:00:00

I created a simple table using React.js and MUI-Datatables:



import MUIDataTable from mui-datatables;

const columns = [Name, Company, City, State];

const data = [
[Joe James, Test Corp, Yonkers, NY],
[John Walsh, Test Corp, Hartford, CT],
[Bob Herm, Test Corp, Tampa, FL],
[James Houston, Test Corp, Dallas, TX],
];

const options = {
filterType: 'checkbox',
};

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


How can I add a custom CSS class to a single row inside the table. Lets say I want the second row with John Walsh to have a green background color.



Update:



Using customRowRender allows to style a certain row but I am still not 100% happy with the solution because certain features like selectable rows do not work out of the box anymore:



const options = {
filterType: checkbox,
customRowRender: (data, dataIndex, rowIndex) => {
let style = {};
if (data[0] === John Walsh) {
style.backgroundColor = green;
}
return (
<TableRow style={style}>
<TableCell />
<TableCell>
<Typography>{data[0]}</Typography>
</TableCell>
<TableCell>
<Typography>{data[1]}</Typography>
</TableCell>
<TableCell>
<Typography>{data[2]}</Typography>
</TableCell>
<TableCell>
<Typography>{data[3]}</Typography>
</TableCell>
</TableRow>
);
}
};

More From » reactjs

 Answers
2

Here you go.



setRowProps: row => { 
if (row[0] === New) {
return {
style: { background: snow }
};
}
}

[#5611] Monday, November 11, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
micayla

Total Points: 148
Total Questions: 92
Total Answers: 109

Location: Aruba
Member since Sat, Oct 2, 2021
3 Years ago
micayla questions
Fri, Dec 24, 21, 00:00, 2 Years ago
Thu, Apr 16, 20, 00:00, 4 Years ago
Fri, Jan 25, 19, 00:00, 5 Years ago
;