最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Reactjs, table cell with different background color - Stack Overflow

matteradmin9PV0评论

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
Add a ment  | 

3 Answers 3

Reset to default 4

Use 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>
Post a comment

comment list (0)

  1. No comments so far