需求
1.有一列导航条,我们需要根据当前点击的导航来设置样式。
2。表格中有价格一列,价格为空时显示为-,不为空时显示价格, 但是价格是红色,如下
当金额小记为空时,显示的时黑色的'-',有价格时应当显示红色,
实现
1.动态设置类名:
可以通过es的模版字符串实现动态设置类名
handle = (index) => {
this.setState({
current: index
})
}
let list = [1,2,3];
list.map((item, index) => {
return (
<div className={{`${current === index?: 'active': ''}`}} onClick=
{this.handle.bind(this, index)}>
)
})
2.动态设置内联样式
const priceStyle = {color: '#d44',fontWeight: 600}
<span key='sumprice' style={record.amount && record.amount!=null ? priceStyle:''}>
{(record.amount==''||record.amount==null)?'-':`¥${record.amount}`}
</span>
//需要注意,这里的priceStyle是一个对象