How to set class on table rows or cells #4103
-
QuestionHow do you set a different class on each row or cell of a table? All I see in docs is this example of setting class for columns. However it doesn't seem to work for rows. Including a columns = [
{'name': 'name', 'label': 'Name', 'field': 'name'},
{'name': 'age', 'label': 'Age', 'field': 'age'},
]
rows = [
{'name': 'Alice', 'age': 18, 'classes' : 'foo'},
{'name': 'Bob', 'age': 21, 'classes' : 'bar'},
]
ui.table (columns=columns, rows=rows)
# html page has no class foo or bar on any items.
# Expected to find it on <tr> element. Other examples use |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I don't think there is an easy way to define dynamic classes on QTable rows. But you can either overwrite the "body" slot to define the look of individual rows from ground up, or you use the "classes" field for column definitions to defined row-dependent column classes: columns = [
{'name': 'name', 'label': 'Name', 'field': 'name', ':classes': '(row) => row.classes'},
{'name': 'age', 'label': 'Age', 'field': 'age', ':classes': '(row) => row.classes'},
]
rows = [
{'name': 'Alice', 'age': 18, 'classes': 'text-blue'},
{'name': 'Bob', 'age': 21, 'classes': 'text-red'},
]
ui.table(columns=columns, rows=rows) |
Beta Was this translation helpful? Give feedback.
-
Here's an improvement for future reference - Answer provides unique classes per row. First attemptSetting classes and :classes together on column doesn't work. :classes overrides and classes is lost. It's not obvious this would happen, because align classes are preserved: columns = [
{
'name': 'name', 'label': 'Name', 'field': 'name',
':classes': '(row) => row.classes',
'classes' : 'name' # does nothing, value lost
},
{
'name': 'age', 'label': 'Age', 'field': 'age',
':classes': '(row) => row.classes' ,
'classes' : 'age' # does nothing, value lost
},
]
rows = [
{'name': 'Alice', 'age': 18, 'classes': 'woman'},
{'name': 'Bob', 'age': 21, 'classes': 'man'},
]
ui.table(columns=columns, rows=rows)
# classes age and name not in table Second attemptYou can move column class values into :classes and it all works swimmingly: columns = [
{'name': 'name', 'label': 'Name', 'field': 'name',
':classes': '(row) => row.classes + " name"' },
{'name': 'age', 'label': 'Age', 'field': 'age',
':classes': '(row) => row.classes + " age"' },
]
rows = [
{'name': 'Alice', 'age': 18, 'classes': 'woman'},
{'name': 'Bob', 'age': 21, 'classes': 'man'},
]
ui.table(columns=columns, rows=rows) which produces table like this: <table>
<tr>
<td class=""> Name </td>
<td class=""> Age </td>
</tr>
<tr>
<td class="woman name"> Alice </td>
<td class="woman age"> 18 </td>
</tr>
<tr>
<td class="man name"> Bob </td>
<td class="man age"> 21 </td>
</tr>
</table> Now I can write css rules for any row-column combination such as: .woman.name { color : pink; }
.man.name { color : blue; } And scripts can select: agesmen = table.querySelectorAll ('.man.age')
nameswomen = table.querySelectorAll ('.woman.name') Obviously selectors like man and woman won't be unique across a large table. If needed, you can add a unique string to each row, like say employee id : Final attemptNow let's say you need a class only added to certain rows. Say you want age cells greater than 65 to have class "retire" on them.
No problem! Just add a row field for each column to your row data. Let's call them columns = [
{'name': 'name', 'label': 'Name', 'field': 'name',
':classes': '(row) => row.classes + " name " + row.name_extra' },
{'name': 'age', 'label': 'Age', 'field': 'age',
':classes': '(row) => row.classes + " age " + row.age_extra' },
]
rows = [
{'name': 'Alice', 'age': 18, 'classes': 'woman'},
{'name': 'Bob', 'age': 21, 'classes': 'man'},
{'name': 'Eve', 'age': 66, 'classes': 'man', 'age_extra' = 'retire'},
{'name': 'Nebuchadnezzar', 'age': 32, 'classes': 'man', 'name_extra' = 'biblical'},
]
ui.table(columns=columns, rows=rows) Now only Eve will have class "retire" on her age cell. Only Nebuchadnezzar will have class "biblical" on his name cell. Rows without a defined name_extra will just have Hope this helps others. |
Beta Was this translation helpful? Give feedback.
I don't think there is an easy way to define dynamic classes on QTable rows. But you can either overwrite the "body" slot to define the look of individual rows from ground up, or you use the "classes" field for column definitions to defined row-dependent column classes: