我已经列出了我要具有选中/取消选中功能的类别和列表项。在点击类别的地方,还应该选中所有子复选框。我有一个列表(JSON中的特权),其中某些项目具有相同的类别,如下所示;
我在plunkr中有示例代码-https://next.plnkr.co/edit/au1XPEjj7MuP77gc
我有如下的JSON代码。
var list = [{
"uuid": "0023",
"title": "AM_FULL_ACCESS",
"displayName": "Application monitoring Full access",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Application Monitoring"
},
{
"uuid": "0025",
"title": "CM_FULL_ACCESS",
"displayName": "Client management Full access",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Client Management"
},
{
"uuid": "0031",
"title": "COMPLIANCE_FULL_ACCESS",
"displayName": "Compliance Full access",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Compliance"
},
{
"uuid": "0054",
"title": "FAILED_APPLICATION_ACCESS",
"displayName": "Failed application access",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Compliance"
},
{
"uuid": "0068",
"title": "FUND_TRANSFER",
"displayName": "Fund Transfer",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Funds"
},
{
"uuid": "0067",
"title": "FUND_LOADING",
"displayName": "Fund Loading",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Funds"
},
{
"uuid": "0066",
"title": "FUND_LOADING_TRANSFER",
"displayName": "Fund Loading and transfer",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Funds"
}
];
要将每个列表项置于特定类别下,我遍历了JSON,并使用ng-if指令对其进行了过滤,以显示具有相同类别的特定列表项。
此处视图(HTML代码)
<label>
<input type="checkbox" id="" ng-model="IsAllChecked" ng-change="CheckUncheckAll()">
<span class="ml-1">Application Monitoring</span>
</label>
<ul class="list-unstyled">
<li ng-repeat="pr in privilegesList track by $index" ng-if="pr.category === 'Application Monitoring'">
<label class="control-label">
<input id="pr.title" type="checkbox" ng-model="pr.isActive" ng-change="CheckUncheckHeader()">
<span>{{pr.displayName}}</span>
</label>
</li>
</ul>
<label>
<input type="checkbox" id="" ng-model="IsAllChecked" ng-change="CheckUncheckAll()">
<span class="ml-1">Client Management</span>
</label>
<ul class="list-unstyled">
<li ng-repeat="pr in privilegesList track by $index" ng-if="pr.category === 'Client Management'">
<label class="control-label">
<input id="pr.title" type="checkbox" ng-model="pr.isActive" ng-change="CheckUncheckHeader()">
<span>{{pr.displayName}}</span>
</label>
</li>
</ul>
<label>
<input type="checkbox" id="" ng-model="IsAllChecked" ng-change="CheckUncheckAll()">
<span class="ml-1">Compliance</span>
</label>
<ul class="list-unstyled">
<li ng-repeat="pr in privilegesList track by $index" ng-if="pr.category === 'Compliance'">
<label class="control-label">
<input id="pr.title" type="checkbox" ng-model="pr.isActive" ng-change="CheckUncheckHeader()">
<span>{{pr.displayName}}</span>
</label>
</li>
</ul>
<label>
<input type="checkbox" id="" ng-model="IsAllChecked" ng-change="CheckUncheckAll()">
<span class="ml-1">Funds</span>
</label>
<ul class="list-unstyled">
<li ng-repeat="pr in privilegesList track by $index" ng-if="pr.category === 'Funds'">
<label class="control-label">
<input id="pr.title" type="checkbox" ng-model="pr.isActive" ng-change="CheckUncheckHeader()">
<span>{{pr.displayName}}</span>
</label>
</li>
</ul>
执行ng-if似乎效果很好,但我想使用更好的解决方案(如果有的话)。我也想为所有类别及其各自的子项添加“检查/取消选中”功能。因此,如果我选择任何类别,则所有子复选框也将被选中。并且,如果未选中任何/所有项目复选框,则应取消选中父复选框。
我正在遵循JS代码,该代码似乎可以选中/取消选中所有代码。我可以针对特定类别执行此操作,但是我想拥有针对所有类别及其子复选框执行此操作的脚本。
答案 0 :(得分:0)
也许此脚本有用。
angular.module('someApp', [])
.controller('someController', function someController($scope) {
$scope.getCategories = getCategories;
$scope.getPrivilegesByCategory = getPrivilegesByCategory;
$scope.selectCategory = selectCategory;
$scope.checkCategory = checkCategory;
$scope.privileges = getCategories().reduce(
function(acc, category, index) {
acc.push({
category: category,
data: getPrivilegesByCategory(category)
});
return acc;
}, []);
});
angular.bootstrap(
document.body, ['someApp']
);
function getCategories() {
var categories = privilegesService().reduce(
function(acc, privilege) {
if (acc.indexOf(privilege.category) === -1) {
acc.push(privilege.category);
}
return acc;
}, []);
return categories;
}
function getPrivilegesByCategory(category) {
var privileges = privilegesService().filter(
function(privilege) {
return privilege.category === category;
});
return privileges;
}
function selectCategory(item) {
checkCategory(item);
item.data.forEach(function(privilege) {
privilege.isActive = !item.isActive;
});
}
function checkCategory(item) {
var res = item.data.filter(function(privilege) {
return privilege.isActive !== true;
});
item.isActive = res.length === 0;
}
function privilegesService() {
return [{
"uuid": "0023",
"title": "AM_FULL_ACCESS",
"displayName": "Application monitoring Full access",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Application Monitoring"
},
{
"uuid": "0025",
"title": "CM_FULL_ACCESS",
"displayName": "Client management Full access",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Client Management"
},
{
"uuid": "0031",
"title": "COMPLIANCE_FULL_ACCESS",
"displayName": "Compliance Full access",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Compliance"
},
{
"uuid": "0054",
"title": "FAILED_APPLICATION_ACCESS",
"displayName": "Failed application access",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Compliance"
},
{
"uuid": "0068",
"title": "FUND_TRANSFER",
"displayName": "Fund Transfer",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Funds"
},
{
"uuid": "0067",
"title": "FUND_LOADING",
"displayName": "Fund Loading",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Funds"
},
{
"uuid": "0066",
"title": "FUND_LOADING_TRANSFER",
"displayName": "Fund Loading and transfer",
"status": "ACTIVE",
"type": "ADMIN",
"category": "Funds"
}
];
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-controller="someController">
<div ng-repeat="item in privileges">
<label class="h5 mb-2">
<input type="checkbox" ng-click="selectCategory(item)" ng-model="item.isActive">
<span class="ml-1" ng-bind="item.category"></span>
</label>
<ul class="list-unstyled ml-4">
<li ng-repeat="pr in item.data track by $index">
<label class="control-label">
<input type="checkbox" ng-model="pr.isActive" ng-change="checkCategory(item)">
<span ng-bind="pr.displayName"></span>
</label>
</li>
</ul>
</div>
</div>