废话不多说上代码,说明都在代码里面
import {
Injectable }
from
'@angular/core';
@
Injectable()
export
class
CheckboxService {
constructor() { }
// 点击全选按钮,进行全选或者反选操作
/**
*
@param
repeatBox
当前全选多选框下对应的tbody对象
*
@param
e
鼠标点击对象(因为绑定的是全选的多选框,所以为全选多选框对象)
*/
public
allChecked(
repeatBox,
e) {
// tslint:disable-next-line:prefer-const
let
arr = [];
let
index;
for (
let
i =
0;
i <
e.
toElement.
parentNode.
parentNode.
children.
length;
i++) {
arr.
push(
e.
toElement.
parentNode.
parentNode.
children[
i]);
}
index =
arr.
indexOf(
e.
toElement.
parentNode);
console.
log(
repeatBox.
children);
for (
let
j =
0;
j <
repeatBox.
children.
length;
j++) {
repeatBox.
children[
j].
children[
index].
children[
0].
checked =
e.
toElement.
checked;
}
}
// 获取当前复选框数组的操作
/**
*
@param
repeatBox
当前全选多选框下对应的tbody对象
*
@param
dimension
1-单列全选,返回一维数组;2-多列全选,返回多维数组
*/
public
checkedList(
repeatBox,
dimension) {
// tslint:disable-next-line:prefer-const
let
arr = [];
if (
dimension ===
1) {
for (
let
i =
0;
i <
repeatBox.
children.
length;
i++) {
arr.
push(
repeatBox.
children[
i].
children[
0].
children[
0].
checked);
}
}
else
if (
dimension ===
2) {
for (
let
i =
0;
i <
repeatBox.
children.
length;
i++) {
arr.
push([]);
for (
let
j =
0;
j <
repeatBox.
children[
i].
children.
length;
j++) {
if (
repeatBox.
children[
i].
children[
j].
children.
length >
0) {
arr[
i].
push(
repeatBox.
children[
i].
children[
j].
children[
0].
checked);
}
}
}
}
return
arr;
}
// 在list中进行选择的时候,如果不全选则将全选框置空,全选则置为选定
/**
*
@param
allCheckedBox
当列对应的全选多选框所在的tr标签对象
*
@param
e
鼠标点击对象
*/
public
isAll(
e,
allCheckedBox) {
// tslint:disable-next-line:prefer-const
let
arr = [];
let
index;
for (
let
i =
0;
i <
e.
toElement.
parentNode.
parentNode.
children.
length;
i++) {
arr.
push(
e.
toElement.
parentNode.
parentNode.
children[
i]);
}
index =
arr.
indexOf(
e.
toElement.
parentNode);
for (
let
j =
0;
j <
e.
toElement.
parentNode.
parentNode.
parentNode.
children.
length;
j++) {
if (!(
e.
toElement.
parentNode.
parentNode.
parentNode.
children[
j].
children[
index].
children[
0].
checked)) {
allCheckedBox.
children[
arr.
indexOf(
e.
toElement.
parentNode)].
children[
0].
checked =
false;
return;
}
}
allCheckedBox.
children[
arr.
indexOf(
e.
toElement.
parentNode)].
children[
0].
checked =
true;
}
}