根据数组控制表格表头显隐的 JavaScript 方法
以下是使用 JavaScript 方法 changeColumn(arr) 来控制表格表头显隐的示例代码:
changeColumn(arr) {
// 初始化 hide 值为 true
this.tableConfig.forEach((item) => {
item.hide = true;
if (item.children) {
item.children.forEach((child1) => {
child1.hide = true;
if (child1.children) {
child1.children.forEach((child2) => {
child2.hide = true;
});
}
});
}
});
// 判断传入的值并修改 hide 值
arr.forEach((value) => {
switch (value) {
case 0:
this.tableConfig.forEach((item) => {
item.hide = true;
if (item.children) {
item.children.forEach((child1) => {
child1.hide = true;
if (child1.children) {
child1.children.forEach((child2) => {
child2.hide = true;
});
}
});
}
});
break;
case 1:
this.tableConfig.forEach((item) => {
if (item.children) {
item.children.forEach((child1) => {
if (child1.children) {
child1.children.forEach((child2) => {
if (child2.label === '本期') {
child2.hide = false;
}
});
}
});
}
});
break;
case 2:
this.tableConfig.forEach((item) => {
if (item.children) {
item.children.forEach((child1) => {
if (child1.children) {
child1.children.forEach((child2) => {
if (child2.label === '本期排名') {
child2.hide = false;
}
});
}
});
}
});
break;
case 3:
this.tableConfig.forEach((item) => {
if (item.children) {
item.children.forEach((child1) => {
if (child1.children) {
child1.children.forEach((child2) => {
if (child2.label === '同期') {
child2.hide = false;
}
});
}
});
}
});
break;
case 4:
this.tableConfig.forEach((item) => {
if (item.children) {
item.children.forEach((child1) => {
if (child1.children) {
child1.children.forEach((child2) => {
if (child2.label === '同期排名') {
child2.hide = false;
}
});
}
});
}
});
break;
case 5:
this.tableConfig.forEach((item) => {
if (item.children) {
item.children.forEach((child1) => {
if (child1.children) {
child1.children.forEach((child2) => {
if (child2.label === '同比') {
child2.hide = false;
}
});
}
});
}
});
break;
}
});
}
使用方法:
- 将
changeColumn(arr)方法添加到您的项目中。 - 在需要更新表头显隐的地方调用
changeColumn(arr)方法,并将需要控制的数组值作为参数传入。
示例:
// 假设您已经定义了 this.tableConfig
let arr = [1, 3]; // 传入的数组值,表示要显示 '本期' 和 '同期' 列
changeColumn(arr);
注意事项:
tableConfig数组中的hide属性用来控制表头列的显隐。arr数组中的值代表要控制的表头列的类型,不同的值对应不同的表头列。- 可以根据实际需求修改
changeColumn(arr)方法的逻辑,以实现更复杂的表头控制功能。
希望这篇文章对您有所帮助!
原文地址: https://www.cveoy.top/t/topic/i0Z3 著作权归作者所有。请勿转载和采集!