I know that jquery can operate the dom to check and then obtain it. But I just learned angularjs and I don’t know much about it. As follows:
This is in thead
<td><input type="checkbox" ng-model="selectAll">
This is on tbody:
<td><input ng-checked="selectAll" value="{{ x.id }}" name="beDel" type="checkbox"/></td>
This is the icon:
My question is: How to determine whether these checkboxes are selected and put the selected values into a collection?
Teach me if you are proficient in angularjs.
某草草2017-05-15 17:03:07
I just have experience, so let me think about it differently:
1: The select all button is a checkbox, and the other sub-buttons are lists of multiple checkboxes;
2: The checkbox of the sub-button is also bound to the checked value of the object when repeating (the default is / Does not exist/i.e. flase/i.e. not selected)
3: Add two extension methods to the array
(1):// 设置元素及子元素的选择状态(可关联自身所有子级)
Array.prototype.setCheck = function (value, children) {
var set_check = function (main) {
for (var i = 0; i < main.length; i++) {
main[i].checked = value;
if (children && main[i][children]) {
set_check(main[i][children]);
}
}
};
set_check(this);
};
(2):// 将元素中所有级别子元素中被选中的元素的id打包为一个数组
Array.prototype.checked = function (children, id_key) {
var ids = [];
var find_check;
if (arguments.length < 2) {
find_check = function (main) {
for (var i = 0; i < main.length; i++) {
if (main[i].checked) {
ids.push(main[i].id);
};
if (!!children && main[i][children]) {
find_check(main[i][children]);
}
}
};
} else {
find_check = function (main) {
for (var i = 0; i < main.length; i++) {
if (main[i].checked) {
ids.push(main[i][id_key]);
};
if (!!children && main[i][children]) {
find_check(main[i][children]);
}
}
};
};
find_check(this);
return ids;
};
4: Bind the extension event (1) to the select all button, and use the extension event (2) to get the id of the selected element
Demo:
// js
$scope.list = [
{
id: 1,
children: [...]
}, {
id: 2,
children: [...]
},
...
];
< !-- html/check-all -- >
<label>
<input class="check-all"
type="checkbox"
ng-model="check_all"
ng-change="list.setCheck(check_all, 'children')">
<span></span>
</label>
< !-- html/check-item -- >
<li class="checkbox" ng-repeat="item in list">
<label>
<input type="checkbox"
name="item[{{ $index }}]"
ng-model="item.checked"
ng-change="item.children.setCheck(item.checked, 'children')">
< !-- 如果是只有一级数据的话,此处无需再绑定自身子级 -- >
</label>
</li>
Get the selected value:
// 一级数据
$scope.checked_ids = $scope.list.checked(); // return [1,32,4,...]
// 多级别数据
$scope.checked_ids = $scope.list.checked('children'); // return [1,32,4,...]
// 如果需要转字符串
$scope.checked_ids = $scope.list.checked('children').join(','); // return "1,32,4"
習慣沉默2017-05-15 17:03:07
http://stackoverflow.com/questions/12648466/how-can-i-get-angular-js-checkboxes-with-select-unselect-all-functionality-and-i
http://stackoverflow.com/questions/14514461/how-to-bind-to-list-of-checkbox-values-with-angularjs