【问题标题】:React Bootstrap Table: Inline Style to Change Background Color Based on ValueReact Bootstrap Table:根据值更改背景颜色的内联样式
【发布时间】:2021-01-19 07:57:00
【问题描述】:

我有一个看起来很简单的问题,但我不知道如何解决它。基本上,我有一个 React Bootstrap Table,它呈现来自 API 的表数据。如果数据中的特定值大于零,我想做的是将行颜色更改为绿色......这是一个例子:

const TableComponent = ({ fixtures }) => {
    return (
        <Table>
            <tbody>
                {fixtures.map((fixture) => (
                    <tr
                        key={fixture.id}
                        style={{
                            backgroundColor: 'green'
                        }}
                    >
                        <td> {fixture.value1} </td>
                    </tr>
                ))}
            </tbody>
        </Table>
    );
};

因此,该行始终默认为绿色backgroundColor。是否可以编写一个函数,使得如果fixture.value2fixture.value3 大于零,则backgroundColor 为绿色,否则设置为默认值?

【问题讨论】:

  • 你试过这样吗? 0|| fixture.value3>0?{backgroundColor: 'green'}:{}}> {fixture.value1}
  • 谢谢 George,我没有意识到你可以在 style prop 的花括号内写函数!

标签: javascript css reactjs react-bootstrap react-bootstrap-table


【解决方案1】:

它对我有用。 试试这样。

const TableComponent = ({ fixtures }) => {
    return (
        <Table>
            <tbody>
                {fixtures.map((fixture) => (
                    <tr
                        key={fixture.id}
                         style={fixture.value2>0|| fixture.value3>0?{backgroundColor:'green'}:{}}
                    >
                        <td> {fixture.value1} </td>
                    </tr>
                ))}
            </tbody>
        </Table>
    );
};

【讨论】:

    猜你喜欢
    • 2021-01-30
    • 2019-03-22
    • 1970-01-01
    • 2011-10-05
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    相关资源
    最近更新 更多