在angular2表单中封装的多选框方法,附带结果返回

废话不多说上代码,说明都在代码里面

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;
}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值