Here is my Codepen .
In this demo application I have two list arrays. One is items
and second is items1
.
I want to:
items
array should be pre-checked which are in items1
. How can I do that with the demo Codepen I shared earlier. Any help would be appreciated.
I'd suggest you to ng-init
to set the value of item.checked
on input init
ng-init="item.checked = (items1|filter: {id: item.id})[0].id == item.id;"
Depending on how dynamically you want to set the pre-checked items you can do one of the following.
Depending whether you want that grey or blue checkbox you either do
change
<input type="checkbox" ng-click=" $event.stopPropagation();" ng-model="item.checked">
to
<input type="checkbox" ng-click="toggleItem(item) $event.stopPropagation();" ng-model="item.checked">
Basically removing the click function that will set a checked value for the item.
Just disable the <input>
element using the attribute disabled
like this <input type="checkbox" disabled>
For each matching item in $scope.items
and $scope.items1
you add a key called checked:true
to the matching items. In your case that would be to create the arrays as following
$scope.items1 = [
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 9 },
{ id: 10 }
];
$scope.items = [
{ id: 1, checked:true },
{ id: 2, checked:true },
{ id: 3, checked:true },
{ id: 4 },
{ id: 5 },
{ id: 6 },
{ id: 7 },
{ id: 8 },
{ id: 9, checked:true },
{ id: 10, checked:true }
];
$scope.items1 = [
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 9 },
{ id: 10 }
];
$scope.items = [
{ id: 1},
{ id: 2},
{ id: 3},
{ id: 4 },
{ id: 5 },
{ id: 6 },
{ id: 7 },
{ id: 8 },
{ id: 9},
{ id: 10}
];
for(var i=0; i<$scope.items.length; i++){
for(var k=0; k<$scope.items1.length; k++){
if($scope.items[i].id == $scope.items1[k].id){
$scope.items[i].checked = true;
break;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.