简体   繁体   English

如何根据 props 更改 react-admin 组件中一行的背景颜色?

[英]How to change the background color of a row in a react-admin component based on props?

I'm designing a react-admin table, and I want to determine a row's background color by the "active" source of the row - if it's true I want the row background color to be green, otherwise I want it to be red.我正在设计一个 react-admin 表,我想通过行的“活动”源来确定行的背景颜色 - 如果是真的,我希望行背景颜色为绿色,否则我希望它为红色。 I tried material ui's "makeStyles" with no success (all of this stuff is new to me).我尝试了材料 ui 的“makeStyles”但没有成功(所有这些东西对我来说都是新的)。

This is my table:这是我的桌子:

const myTable = (props) => {

    return (
        <List {...props} pagination={<PostPagination />}>
            <Datagrid>
                <NumberField source="id" />
                <TextField source="name" />
                <TextField source="category" />
                <TextField source="platform" />
                <NumberField source="major" />
                <NumberField source="minor" />
                <BooleanField source="active" label="active"/>
                <DateField source="audit" />
                <EditButton basePath="versions" />
                <DeleteButton basePath="versions" />
            </Datagrid>
        </List>
    )

}

Thank you very much for any help!非常感谢您的帮助!

Good afternoon, i didn't really understand that you have strings here, but there are many different options for example:下午好,我真的不明白你在这里有字符串,但是有很多不同的选项,例如:

  1. <MyRow style={{background: (isActive? 'red': 'green')} />
  2. <MyRow className={isActive? classes.activeRow: null />

In the first variant, you do not need to do anything with the styles, and in the second you will have to create a style for example through makeStyles.在第一个变体中,您不需要对 styles 执行任何操作,而在第二个变体中,您必须通过 makeStyles 创建样式。

const useStyles = makeStyles((theme) => ({
    activeRow:{
        backgroundColor: theme.palette.primary.main
    }
  }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM