/**
* Created by Administrator on 2018/4/16.
*/
export class CheckBoxUtil {
/**
* 单选
* @param e $event 事件
* @param item 选中对象
* @param selectedItem 更新后的数组
*/
public static updateSelected(e: any, item: any, selectedItem: any[]) {
const obj = e.target;
let addFlag = true;
if (selectedItem.length > 0) {
selectedItem.forEach(data => {
if (CheckBoxUtil.diff(data, item)) {
addFlag = false;
}
});
}
if (typeof (obj.checked) == "undefined") {
if (obj && addFlag) {
console.log('执行添加');
selectedItem.push(item);
}
if (obj && !addFlag) {
console.log('执行删除');
selectedItem.splice(selectedItem.indexOf(item), 1);
}
} else {
if (obj.checked && addFlag) {
console.log('checkbox执行添加');
selectedItem.push(item);
}
if (!obj.checked && !addFlag) {
console.log('checkbox执行删除');
selectedItem.splice(selectedItem.indexOf(item), 1);
}
}
}
/**
* 判断俩对象是否为同一对象
* @param obj1
* @param obj2
* @returns {boolean}
*/
private static diff(obj1: any, obj2: any): boolean {
let o1 = obj1 instanceof Object;
let o2 = obj2 instanceof Object;
if (!o1 || !o2) {/* 判断不是对象 */
return obj1 === obj2;
}
if (Object.keys(obj1).length !== Object.keys(obj2).length) {
return false;
//Object.keys() 返回一个由对象的自身可枚举属性(key值)组成的数组,例如:数组返回下表:let arr = ["a", "b", "c"];console.log(Object.keys(arr))->0,1,2;
}
for (let attr in obj1) {
let t1 = obj1[attr] instanceof Object;
let t2 = obj2[attr] instanceof Object;
if (t1 && t2) {
return this.diff(obj1[attr], obj2[attr]);
} else if (obj1[attr] !== obj2[attr]) {
return false;
}
}
return true;
}
}
网友评论