No way to customize TableRow #1478
-
When I use DataTable, I would like to have a way to add some custom CSS to rows depending on a row's value. It seems there is no anything how I could customize rows. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
A [data-row="a"] {
color: red;
} Would that work for your use case? |
Beta Was this translation helpful? Give feedback.
-
It works only depending on |
Beta Was this translation helpful? Give feedback.
-
You could compute the row IDs that contain a certain value and use those IDs to style the corresponding rows. In the following example:
<script>
import { DataTable } from "carbon-components-svelte";
const rows = Array.from({ length: 6 }).map((_, i) => ({
id: i,
name: "Load Balancer " + (i + 1),
protocol: "HTTP",
port: i % 3 ? (i % 2 ? 3000 : 80) : 443,
rule: i % 3 ? "Round robin" : "DNS delegation",
}));
const rowsWithPort80 = rows
.filter((row) => row.port === 80)
.map((row) => row.id);
const selectors = rowsWithPort80.map((id) => `[data-row="${id}"]`).join(",");
const styles = `
<style>
${selectors} {
outline: 2px solid blue;
}
<\/style>
`;
</script>
<svelte:head>
{@html styles}
</svelte:head>
<DataTable
headers={[
{ key: "name", value: "Name" },
{ key: "protocol", value: "Protocol" },
{ key: "port", value: "Port" },
{ key: "rule", value: "Rule" },
]}
{rows}
/> |
Beta Was this translation helpful? Give feedback.
-
It works! Thank you! I never thought of managing styles this way. 🤔 By the way, this code surprisingly does not work with Svelte Kit because you cannot write the // ¯\_(ツ)_/¯
const styles = "<sty" + `le>
${selectors} {
outline: 2px solid blue;
}
<\/st` + "yle>"; |
Beta Was this translation helpful? Give feedback.
You could compute the row IDs that contain a certain value and use those IDs to style the corresponding rows.
In the following example:
port
value of80
[data-row="<id>"]
svelte:head
, which are applied globallySvelte REPL