gpt4 book ai didi

css - Angularjs ng-class多类

转载 作者:行者123 更新时间:2023-11-28 00:38:15 24 4
gpt4 key购买 nike

我正在尝试在 angularjs 中应用 ng 类,条件是如果json 的值为 1 以实现绿色,2 为红色,0 为白色。但由于某种原因,我无法得到结果。如果您想更清楚地查看代码,我还有一个 jsfiddle。

https://jsfiddle.net/0xsv2aq3/42/

任何帮助将不胜感激。谢谢

    <html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
</head>


<script>
var myApp = angular.module('myApp',[]);

function MyCtrl($scope) {

$scope.schedule =

{
"days": [
{
"day": "2018-12-09T00:00:00",
"entries": [
{
"name": "Adam D",
"value": 1,
"parts": [
"EG"
]
},
{
"name": "Chris S",
"value": 0,
"parts": []
},
{
"name": "Debbie J",
"value": 2,
"parts": []
},
{
"name": "Dennis S",
"value": 1,
"parts": [
"Lead"
]
},
{
"name": "Jamie W",
"value": 2,
"parts": []
},
{
"name": "Jen C",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Joe T",
"value": 1,
"parts": [
"Drums"
]
},
{
"name": "Karen B",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Kristene B",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Lisa R",
"value": 1,
"parts": [
"3rd"
]
},
{
"name": "Mike B",
"value": 1,
"parts": [
"AG"
]
},
{
"name": "Nancy S",
"value": 0,
"parts": []
},
{
"name": "Paul N",
"value": 2,
"parts": []
},
{
"name": "Scott J",
"value": 0,
"parts": []
},
{
"name": "Scott J",
"value": 2,
"parts": []
},
{
"name": "Trisha S",
"value": 0,
"parts": []
}
]
},
{
"day": "2018-12-16T00:00:00",
"entries": [
{
"name": "Adam D",
"value": 1,
"parts": [
"EG"
]
},
{
"name": "Chris S",
"value": 1,
"parts": [
"Keys 2"
]
},
{
"name": "Debbie J",
"value": 2,
"parts": []
},
{
"name": "Dennis S",
"value": 1,
"parts": [
"Lead"
]
},
{
"name": "Jamie W",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Jen C",
"value": 0,
"parts": []
},
{
"name": "Joe T",
"value": 1,
"parts": [
"Drums"
]
},
{
"name": "Karen B",
"value": 0,
"parts": []
},
{
"name": "Kristene B",
"value": 0,
"parts": []
},
{
"name": "Lisa R",
"value": 1,
"parts": [
"3rd"
]
},
{
"name": "Mike B",
"value": 1,
"parts": [
"AG"
]
},
{
"name": "Nancy S",
"value": 1,
"parts": [
"2nd"
]
},
{
"name": "Paul N",
"value": 1,
"parts": [
"3rd"
]
},
{
"name": "Scott J",
"value": 2,
"parts": []
},
{
"name": "Trisha S",
"value": 0,
"parts": []
}
]
},
{
"day": "2018-12-23T00:00:00",
"entries": [
{
"name": "Adam D",
"value": 1,
"parts": [
"EG"
]
},
{
"name": "Chris S",
"value": 0,
"parts": []
},
{
"name": "Debbie J",
"value": 2,
"parts": []
},
{
"name": "Dennis S",
"value": 1,
"parts": [
"Lead"
]
},
{
"name": "Jamie W",
"value": 0,
"parts": []
},
{
"name": "Jen C",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Joe T",
"value": 1,
"parts": [
"Drums"
]
},
{
"name": "Karen B",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Kristene B",
"value": 2,
"parts": []
},
{
"name": "Lisa R",
"value": 2,
"parts": []
},
{
"name": "Mike B",
"value": 2,
"parts": []
},
{
"name": "Nancy S",
"value": 0,
"parts": []
},
{
"name": "Paul N",
"value": 2,
"parts": []
},
{
"name": "Scott J",
"value": 1,
"parts": [
"AG"
]
},
{
"name": "Scott J",
"value": 1,
"parts": [
"AG"
]
},
{
"name": "Trisha S",
"value": 1,
"parts": [
"2nd"
]
}
]
},
{
"day": "2018-12-30T00:00:00",
"entries": [
{
"name": "Adam D",
"value": 1,
"parts": [
"EG"
]
},
{
"name": "Chris S",
"value": 1,
"parts": [
"Keys 2"
]
},
{
"name": "Debbie J",
"value": 2,
"parts": []
},
{
"name": "Dennis S",
"value": 1,
"parts": [
"Lead"
]
},
{
"name": "Jamie W",
"value": 0,
"parts": []
},
{
"name": "Jen C",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Joe T",
"value": 1,
"parts": [
"Drums"
]
},
{
"name": "Karen B",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Kristene B",
"value": 2,
"parts": []
},
{
"name": "Lisa R",
"value": 2,
"parts": []
},
{
"name": "Mike B",
"value": 2,
"parts": []
},
{
"name": "Nancy S",
"value": 1,
"parts": [
"2nd"
]
},
{
"name": "Paul N",
"value": 2,
"parts": []
},
{
"name": "Scott J",
"value": 2,
"parts": []
},
{
"name": "Trisha S",
"value": 0,
"parts": []
}
]
},
{
"day": "2019-01-06T00:00:00",
"entries": [
{
"name": "Adam D",
"value": 1,
"parts": [
"EG"
]
},
{
"name": "Chris S",
"value": 0,
"parts": []
},
{
"name": "Debbie J",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Dennis S",
"value": 1,
"parts": [
"Lead"
]
},
{
"name": "Jamie W",
"value": 0,
"parts": []
},
{
"name": "Jen C",
"value": 0,
"parts": []
},
{
"name": "Joe T",
"value": 1,
"parts": [
"Drums"
]
},
{
"name": "Karen B",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Kristene B",
"value": 2,
"parts": []
},
{
"name": "Lisa R",
"value": 1,
"parts": [
"3rd"
]
},
{
"name": "Mike B",
"value": 2,
"parts": []
},
{
"name": "Nancy S",
"value": 0,
"parts": []
},
{
"name": "Paul N",
"value": 2,
"parts": []
},
{
"name": "Scott J",
"value": 1,
"parts": [
"AG"
]
},
{
"name": "Trisha S",
"value": 0,
"parts": []
}
]
},
{
"day": "2019-01-13T00:00:00",
"entries": [
{
"name": "Adam D",
"value": 1,
"parts": [
"EG"
]
},
{
"name": "Chris S",
"value": 1,
"parts": [
"Keys 2"
]
},
{
"name": "Debbie J",
"value": 2,
"parts": []
},
{
"name": "Dennis S",
"value": 1,
"parts": [
"Lead"
]
},
{
"name": "Jamie W",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Jen C",
"value": 0,
"parts": []
},
{
"name": "Joe T",
"value": 1,
"parts": [
"Drums"
]
},
{
"name": "Karen B",
"value": 0,
"parts": []
},
{
"name": "Kristene B",
"value": 0,
"parts": []
},
{
"name": "Lisa R",
"value": 1,
"parts": [
"3rd"
]
},
{
"name": "Mike B",
"value": 0,
"parts": []
},
{
"name": "Nancy S",
"value": 1,
"parts": [
"2nd"
]
},
{
"name": "Paul N",
"value": 2,
"parts": []
},
{
"name": "Scott J",
"value": 1,
"parts": [
"AG"
]
},
{
"name": "Scott J",
"value": 1,
"parts": [
"AG"
]
},
{
"name": "Trisha S",
"value": 0,
"parts": []
}
]
},
{
"day": "2019-01-20T00:00:00",
"entries": [
{
"name": "Adam D",
"value": 1,
"parts": [
"EG"
]
},
{
"name": "Chris S",
"value": 1,
"parts": [
"Keys 2"
]
},
{
"name": "Debbie J",
"value": 2,
"parts": []
},
{
"name": "Dennis S",
"value": 1,
"parts": [
"Lead"
]
},
{
"name": "Jamie W",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Jen C",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Joe T",
"value": 1,
"parts": [
"Drums"
]
},
{
"name": "Karen B",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Kristene B",
"value": 0,
"parts": []
},
{
"name": "Lisa R",
"value": 0,
"parts": []
},
{
"name": "Mike B",
"value": 0,
"parts": []
},
{
"name": "Nancy S",
"value": 1,
"parts": [
"2nd"
]
},
{
"name": "Paul N",
"value": 2,
"parts": []
},
{
"name": "Scott J",
"value": 1,
"parts": [
"AG"
]
},
{
"name": "Scott J",
"value": 1,
"parts": [
"AG"
]
},
{
"name": "Trisha S",
"value": 0,
"parts": []
}
]
},
{
"day": "2019-01-27T00:00:00",
"entries": [
{
"name": "Adam D",
"value": 1,
"parts": [
"EG"
]
},
{
"name": "Chris S",
"value": 2,
"parts": []
},
{
"name": "Debbie J",
"value": 2,
"parts": []
},
{
"name": "Dennis S",
"value": 1,
"parts": [
"Lead"
]
},
{
"name": "Jamie W",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Jen C",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Joe T",
"value": 1,
"parts": [
"Drums"
]
},
{
"name": "Karen B",
"value": 0,
"parts": []
},
{
"name": "Kristene B",
"value": 1,
"parts": [
"Melody"
]
},
{
"name": "Lisa R",
"value": 0,
"parts": []
},
{
"name": "Mike B",
"value": 1,
"parts": [
"AG"
]
},
{
"name": "Nancy S",
"value": 2,
"parts": []
},
{
"name": "Paul N",
"value": 2,
"parts": []
},
{
"name": "Scott J",
"value": 2,
"parts": []
},
{
"name": "Trisha S",
"value": 1,
"parts": [
"2nd"
]
}
]
}
]
}


}
</script>
<body>

<div ng-app="myApp" ng-controller="MyCtrl">
<table class="table" border=1>
<thead>
<tr>
<th>Name</th>
<th ng-repeat=" x in schedule.days" scope="col">{{x.day}}</th>





</tr>
</thead>
<style>
.red{background-color: red;}
.white{background-color: white;}
.green{background-color: green;}
</style>

<tbody>
<tr ng-class="{'red': day.entries[$parent.$index].value === 1, 'green': day.entries[$parent.$index].value === 2, 'white': day.entries[$parent.$index].value === 0}" ng-repeat="day1 in schedule.days[0].entries" scope="row">
<td>{{day1.name}}</td>
<td ng-repeat="day in schedule.days">{{day.entries[$parent.$index].parts[0]}}>{{day.entries[$parent.$index].value}}</td>
</tr>

</tbody>
</table>
</div>



















<!-- <div ng-app="myApp" ng-controller="MyCtrl">
<table class="table" border=1>
<thead>
<tr>
<th>Name</th>
<th ng-repeat=" x in schedule.days" scope="col">{{x.day}}</th>
</tr>
</thead>
<tbody>
<tr ng-repeat=" item in schedule.days" scope="row">
<td ng-repeat="y in item.entries">{{y.value}}</td>

<td ng-repeat=" itm in item.parts">{{itm}}</td>
</tr>
</tbody>
</table>
</div>-->
</body>
</html>

最佳答案

我已经创建了一个自定义过滤器来将类应用到 td

myApp.filter('color',function(){
return function(status){
console.log(status);
switch(status){
case 0:
return 'white';
break;
case 1:
return 'red';
break;
case 2:
return 'green';
break;

}
}
});

在td标签中添加过滤器

<td class="{{day.entries[$parent.$index].value | color}}" ng-repeat="day in schedule.days">{{day.entries[$parent.$index].parts[0]}}>{{day.entries[$parent.$index].value}}</td>

关于css - Angularjs ng-class多类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54015642/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com