Aurelia中repeat.for绑定字符串属性及表格行颜色设置问题
实现方案
当然可以实现,但不建议用if.bind——因为if.bind是用来控制元素是否渲染的,而你需要的是给已渲染的行添加样式,用class.bind或style.bind会更直接高效。
方法1:通过CSS类绑定(推荐)
先定义一个CSS类:
.scrap-highlight-row { background-color: blue; color: white; /* 可选,让文字更清晰 */ }
然后在表格行上绑定这个类,直接用网络返回的字符串做判断条件:
<tbody> <tr repeat.for="receiving of yourReceivingArray" class.bind="{'scrap-highlight-row': receiving.supplier === 'Scrap Separated' || receiving.supplier === 'Scrap Sorted'}" > <td>${receiving.supplier}</td> <!-- 其他单元格内容 --> </tr> </tbody>
方法2:直接绑定行内样式
如果不想额外加CSS类,也可以直接用style.bind:
<tr repeat.for="receiving of yourReceivingArray" style.bind="{'background-color': (receiving.supplier === 'Scrap Separated' || receiving.supplier === 'Scrap Sorted') ? 'blue' : 'inherit'}" > <!-- 单元格内容 --> </tr>
注意事项
如果网络返回的字符串存在大小写不一致的情况(比如偶尔返回scrap separated小写),可以统一转成小写来匹配,避免漏判:
// 条件改成: receiving.supplier?.toLowerCase() === 'scrap separated' || receiving.supplier?.toLowerCase() === 'scrap sorted'
内容的提问来源于stack exchange,提问作者lemilevoj
相关产品推荐
相关产品推荐

