admin管理员组文章数量:1516870
in my React application, I have a table (using semantic ui). I want to change bgcolor via a condition.
in most examples I see like bgcolor={(condition)?'red':'blue'}
but I need to check if the value exists in an array. so if value is in arrayOne apply a bgcolor, if value is in arrayTwo apply another color else no bgcolor
I tried this which is wrong
<Table.Cell
key={value}
selectable
{...arrayOne.includes(value)?{bgcolor="red"}:{}}
{...arrayTwo.includes(value)?{bgcolor="blue"}:{}}
>
{value}
</Table.Cell>
in my React application, I have a table (using semantic ui). I want to change bgcolor via a condition.
in most examples I see like bgcolor={(condition)?'red':'blue'}
but I need to check if the value exists in an array. so if value is in arrayOne apply a bgcolor, if value is in arrayTwo apply another color else no bgcolor
I tried this which is wrong
<Table.Cell
key={value}
selectable
{...arrayOne.includes(value)?{bgcolor="red"}:{}}
{...arrayTwo.includes(value)?{bgcolor="blue"}:{}}
>
{value}
</Table.Cell>
Share
Improve this question
asked Sep 4, 2018 at 18:34
Amir-MousaviAmir-Mousavi
4,59315 gold badges78 silver badges133 bronze badges
3 Answers
Reset to default 4Use style instead of bgcolor as it is no longer supported in HTML5. Even if you try it without the conditional logic, bgcolor will not affect the <td>, regardless of React. Per W3Schools:
The bgcolor attribute of is not supported in HTML5. Use CSS instead.
Setting style property conditionally within the render() function. This example uses @OlivierBoissé approach for conditionally setting the value, but you could really use any conditional approach you are fortable with and ESLint doesn't plain about. You can use CSS inherit as a default value when working with background-color:
// default
let backgroundColor = 'inherit';
if (arrayOne.includes(value)) {
backgroundColor = 'red';
} else if (arrayTwo.includes(value)) {
backgroundColor = 'blue';
}
{/* or if you need one color to take precedence when value is in both arrays
if (arrayOne.includes(value)) {
backgroundColor = 'red';
}
if (arrayTwo.includes(value)) {
backgroundColor = 'blue';
}
*/}
<Table.Cell
key={value}
selectable
style={{backgroundColor}}
>
{value}
</Table.Cell>
Alternatively you can also use className instead of style:
.foo { background-color: red; }
.bar { background-color: blue; }
let backgroundColor = '';
if (arrayOne.includes(value)) {
backgroundColor = 'foo';
} else if (arrayTwo.includes(value)) {
backgroundColor = 'bar';
}
<Table.Cell className={backgroundColor} ...>
Here is a working StackBlitz example.
Hopefully that helps!
Create a function
getColor = (value) => array2.includes(value) ? {bgcolor:'red'} : array1.includes(value) ? {bgcolor:'blue'} : {}
And <Cell {...getColor()} />
You can declare a variable and use conditions to determine its value
let bgcolor;
if(arrayOne.includes(value)) {
bgcolor = 'red';
} else if( arrayTwo.includes(value)) {
bgcolor = 'blue';
}
then
<Table.Cell
key={value}
selectable
bgcolor={bgcolor}
>
{value}
</Table.Cell>
本文标签: javascriptReactJStable cell with different background colorStack Overflow
版权声明:本文标题:javascript - Reactjs, table cell with different background color - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.betaflare.com/web/1744182677a2594140.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论