使用ng-repeat在html表中选择行着色
Alternative row coloring in html table with ng-repeat
我有一个使用ng-repeat填充值的表。我需要颜色的行表交替绿色和黄色的颜色。我用下面的方法试了试,没有ng-repeat,效果很好。
.table-striped>tbody>tr:nth-of-type(odd)
{
background: yellow !important;
}
.table-striped>tbody>tr:nth-of-type(even)
{
background: green !important;
}
<html>
<div><table class="table table-striped">
<thead style="border: 1px solid">
<tr>
<th>Heading</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{value.val1}}</td>
</tr>
<tr>
<td>{{value.val2}}</td>
</tr>
<tr>
<td>{{value.val3}}</td>
</tr>
</tbody>
</table>
</div>
<html>
但是在使用ng-repeat时不工作,如下所示(所有行本身都是黄色)。请帮助。提前谢谢。
.table-striped>tbody>tr:nth-of-type(odd)
{
background: yellow !important;
}
.table-striped>tbody>tr:nth-of-type(even)
{
background: green !important;
}
<html>
<div><table class="table table-striped">
<thead style="border: 1px solid">
<tr>
<th>Heading</th>
</tr>
</thead>
<tbody ng-repeat="value in values">
<tr>
<td>{{value.val1}}</td>
</tr>
</tbody>
</table>
</div>
<html>
我认为你应该给ng-repeat="value in values"来代替body。
您可以动态地为您的行添加一个类,然后给出您想要的样式。
<tr class="myrow">
$(".myrow:odd").addClass("odditem");
$(".myrow:even").addClass("evenitem");
你可以使用ng-class-odd="'classname'"指令。
<html>
<div>
<table class="table table-striped">
<thead style="border: 1px solid">
<tr>
<th>Heading</th>
</tr>
</thead>
<tbody ng-repeat="value in values">
<tr class="row-color" ng-class-odd="'odd'">
<td>{{value.val1}}</td>
</tr>
<tr class="row-color" ng-class-odd="'odd'">
<td>{{value.val2}}</td>
</tr>
<tr class="row-color" ng-class-odd="'odd'">
<td>{{value.val3}}</td>
</tr>
</tbody>
</table>
</div>
<html>
那么在你的CSS中你可以这样做
.row-color {
background: green;
}
.row-color.odd {
background: yellow;
}
这也摆脱了你的!重要,通常被认为是不好的做法。
虽然我认为OP的解决方案是将ng-repeat
从<tbody>
移动到<tr>
,因为它与没有ng-repeat
的预期结构相匹配;当ng-repeat
在<tbody>
层上时,完全可以通过单独使用css来替换颜色。
tbody:nth-of-type(odd)>tr {
background-color: pink;
}
tbody:nth-of-type(even)>tr {
background-color: purple;
}
<table>
<tbody>
<tr><td>row1</td></tr>
</tbody>
<tbody>
<tr><td>row2</td></tr>
</tbody>
<tbody>
<tr><td>row3</td></tr>
</tbody>
</table>
相关文章:
- AngularJS:根据其他对象预先选择ng repeat中的select元素
- AngularJS选择带有ng-model和ng-repeat的默认值,而不是ng-option
- AngularJs:选择在使用ng-repeat时不显示任何选项的下拉列表
- 使用 ng-if 选择 ng-repeat 循环中的第二个项目
- 将用户选择的多个单选按钮选项存储在使用 ng-repeat(s)构建的角度 js 中
- ng-repeat中的动态选择选项
- 将 ng 模型分配给 ng-repeat(用于下拉选择)
- 如何使用 AngularJS ng-click / ng-repeat 取消选择非选定项目
- ng-repeat指令中的角度选择:如何访问子范围
- 使用 AngularJS 和 ng-repeat初始化选择
- 使用ng repeat重新加载时,不会设置选择项的内容
- 使用ng repeat选择json数组
- 当从ng repeat中选择时,只显示json中的一个项目
- 简单的ng-Repeat选择框的选项
- 使用ng-repeat在html表中选择行着色
- Ng-repeat适用于<
- < light >等,但在选择完全相同的源时失败
- Angular JS -在Ng-Repeat中选择由Ng-Options生成的选项
- 从javascript中选择ng-repeat标记
- 选择除选中的NG-Repeat迭代之外的所有NG-Repeat迭代
- 检查选项选择ng-repeat