material-table icon indicating copy to clipboard operation
material-table copied to clipboard

There is a way to apply styling for each row?

Open DaxSoft opened this issue 6 years ago • 6 comments

I know that there is a way to apply for each column and so, you can apply for the header and body (cell). However, I want to know if there is a way to do the same thing for each row — I know as well that I can apply for all row.

For example:

  • It can ben on the 'data' property, where you display all row values with the keys as matches for the columns ids. Then, a option for styling following the value would be very good: [default] data={[ {column1: value} ]}
    [styling] data={[ {column1: { value: 1, style: {} }} ]}

My idea for this is, for example: if I want to style the row by his value, if it is under '0', it would be 'red'. If it is above '100', it would be 'blue'.

Good example: https://prnt.sc/r5qs1u

DaxSoft avatar Feb 21 '20 14:02 DaxSoft

From material-table typescript definition for Options: rowStyle?: React.CSSProperties | ((data: any, index: number, level: number) => React.CSSProperties);

Then, you can have something like:

rowStyle: (data: yourDataType, index: number, level: number) => {
    return data.value <= 0 ? { color: 'red' } : { color: 'green' }; // or empty {}
}

Ebram-Tharwat avatar Feb 21 '20 15:02 Ebram-Tharwat

I have tried, but this changes all the column of the row altogether. I only want to change a specific table of the column at the row

DaxSoft avatar Feb 21 '20 16:02 DaxSoft

I have tried, but this changes all the column of the row altogether. I only want to change a specific table of the column at the row

Have you tried to use cellStyle ? (which is part of columns)

Ebram-Tharwat avatar Feb 21 '20 16:02 Ebram-Tharwat

Yes, but I don't have access to the value of the field correspondent. I only have the access to the ID (index of array) of the column, in which for me to have the result I want. I would need to create a complex calc with matrix (ok, isn't that complex to do), in which I guess it will result on low perfomance. Because I would need to do 'loop' on the values of the 'data', inside of the loop of the columns (I hope that this answer of mine don't get confused)

DaxSoft avatar Feb 21 '20 16:02 DaxSoft

I was trying and come up with a little dirty trick to do what I need. It is not the best, however works... I'll let here for everyone...

Picture Example

Important, I'm using the Next.js

This will identify the table and it will give me the power to handle with each cells

`

useEffect(() => { const table = [...document.querySelectorAll('table')].find( el => el.rows.length >= 3 ) if (!!table) { const theader = [ ...table.querySelector('thead').querySelectorAll('th'), ] const trs = [...table.querySelector('tbody').querySelectorAll('tr')] trs.map((tr, id) => { if (!theader) return false handleTDStyle([...tr.querySelectorAll('td')], theader) }) } }, [!!window])

`

Example with the 'handleTDStyles'

` function handleTDStyle(tds, theader) { tds.map((td, n) => { const headerText = theader[n].innerText.trim()

    if (/((Í|I)ndice\s?de\s?Basil(é|e)ia)/i.test(headerText)) {
        td.style.backgroundColor = 'red'
    }
})

}

`

Again, it's not the best... but until we have a better aproach, it will do his work xD

DaxSoft avatar Feb 21 '20 17:02 DaxSoft

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You can reopen it if it required.

stale[bot] avatar Jul 05 '20 20:07 stale[bot]