admin管理员组文章数量:1278787
How can I set a different background color for every row? For exemple:
row1: blue
row2: red
row3: green
main.js
$scope.names = [
{fName: "John", lName: "David"},
{fName: "Richard", lName: "Daniel"},
{fName: "Paul", lName: "Mark"}
];
test.html
<table class="table table-bordered">
<tbody><tr ng-repeat="name in names">
<td>{{name.fName}}</td>
<td>{{name.lName}}</td>
</tr></tbody>
</table>
How can I set a different background color for every row? For exemple:
row1: blue
row2: red
row3: green
main.js
$scope.names = [
{fName: "John", lName: "David"},
{fName: "Richard", lName: "Daniel"},
{fName: "Paul", lName: "Mark"}
];
test.html
<table class="table table-bordered">
<tbody><tr ng-repeat="name in names">
<td>{{name.fName}}</td>
<td>{{name.lName}}</td>
</tr></tbody>
</table>
Share
Improve this question
asked Oct 20, 2015 at 20:52
Elvira EmmElvira Emm
601 gold badge1 silver badge10 bronze badges
12
- I just want to set a specific background color for a specific row. If i have a table with 10 rows i want to be able to set a background color for the row with number 6 for example. – Elvira Emm Commented Oct 20, 2015 at 20:56
- Will there be a ryhme or reason to the colors? Can you bind the color to the name? – hungerstar Commented Oct 20, 2015 at 20:57
- what determines the row you want to color, its position, or ..? – TimCodes Commented Oct 20, 2015 at 20:57
- Would you consider some jQuery? – An0nC0d3r Commented Oct 20, 2015 at 20:58
- 1 you could use ng-class – TimCodes Commented Oct 20, 2015 at 21:00
3 Answers
Reset to default 5If you would like a different color for each row you could use ngStyle
<tr ng-repeat="name in names" ng-style="{'background-color': colors[$index]}">
where your controller states $scope.colors = ['blue','red', 'green']
or
<tr ng-repeat="name in names" ng-class="colors[$index]">
and add each to your style sheet
.blue { background-color: blue }
Here is an example, with angular:
Template:
<tr ng-repeat="name in names" ng-class="{greenyellow: $index === 1}">
<td>{{name.fName}}</td>
<td>{{name.lName}}</td>
</tr>
CSS
.greenyellow {
background-color: greenyellow;
}
This will style the second row as $index
is zero-based.
demo fiddle
Below code is used to highlight the selected row .
<tr ng-repeat="orderList in orderDetailList track by $index" ng-style="{'background-color': colors[$index]}">
Declare in the controller
app.controller("OrderListController",function($scope){
$scope.colors=[];
/* call the below function on ng-click
$scope.addtoship = function(){
$scope.colors.push(['pink']);
}
}
HTML
<td>
<button type="button" data-ng-click="addtoship()"> Add to Ship </button>
</td>
版权声明:本文标题:javascript - How to change background color for a specific <tr> in AngularJS using ng-repeat - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741293151a2370664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论