如果对你有帮助不要钱的赞点一点(嘿嘿)
在写入函数的时候先在参数中填写row 加公式row()
向下折叠函数
// 这里填写要折叠的行数
let zd = 8;
var h = document.querySelectorAll(".x-table tbody tr");
h[3].setAttribute('data-custom', 'value');
// 遍历每一行
for (let index = 0; index < h.length; index++) {
// 获取每行的第一个单元格
var firstCell = h[index].firstElementChild;
if (firstCell) {
// 获取 rowSpan 属性
var rowspan = firstCell.rowSpan;
// 获取和转换 sumdata 的值
var sumdata = parseFloat(firstCell.getAttribute('sumdata')) || 0;
if (firstCell.id.includes('A')) {
console.log(firstCell);
console.log("数: " + rowspan);
// 计算正确的范围
var effectiveSumdata = (sumdata >= -100000) ? sumdata : 0;
var rangeStart = index;
var rangeEnd = rowspan + index + effectiveSumdata;
console.log("index"+index);
console.log("effectiveSumdata"+effectiveSumdata);
console.log(rangeEnd);
if (row > index && row < rangeEnd) {
console.log("row在" + rowspan + "行中");
if (h[row].style.display === "none") {
console.log("显示");
firstCell.rowSpan += zd;
effectiveSumdata -= zd;
console.log(effectiveSumdata);
firstCell.setAttribute('sumdata', effectiveSumdata);
} else {
console.log("隐藏");
firstCell.rowSpan -= zd;
effectiveSumdata += zd;
console.log(effectiveSumdata);
firstCell.setAttribute('sumdata', effectiveSumdata);
}
}
}
}
}
// 折叠或展开行
for (let i = row; i < row + zd; i++) {
if (h[i] && h[i].style.display === "none") {
h[i].style.display = "";
} else if (h[i]) {
h[i].style.display = "none";
}
}
向上折叠函数
// 这里填写要折叠的行数
let zd = 8;
row-=1;
var h = document.querySelectorAll(".x-table tbody tr");
h[3].setAttribute('data-custom', 'value');
// 遍历每一行
for (let index = 0; index < h.length; index++) {
// 获取每行的第一个单元格
var firstCell = h[index].firstElementChild;
if (firstCell) {
// 获取 rowSpan 属性
var rowspan = firstCell.rowSpan;
// 获取和转换 sumdata 的值
var sumdata = parseFloat(firstCell.getAttribute('sumdata')) || 0;
if (firstCell.id.includes('A')) {
console.log(firstCell);
console.log("数: " + rowspan);
// 计算正确的范围
var effectiveSumdata = (sumdata >= -100000) ? sumdata : 0;
var rangeStart = index;
var rangeEnd = rowspan + index + effectiveSumdata;
console.log("index" + index);
// console.log("effectiveSumdata" + effectiveSumdata);
console.log(rangeEnd);
console.log("row在" + row + "行中");
if (row >= index && row < rangeEnd) {
console.log("row在" + row + "行中");
console.log(h[row]);
if (h[row-1].style.display === "none") {
console.log("显示");
firstCell.rowSpan += zd;
effectiveSumdata -= zd;
console.log(effectiveSumdata);
firstCell.setAttribute('sumdata', effectiveSumdata);
} else {
console.log("隐藏");
firstCell.rowSpan -= zd;
effectiveSumdata += zd;
console.log(effectiveSumdata);
firstCell.setAttribute('sumdata', effectiveSumdata);
}
}
}
}
}
let condition = true; // 控制只执行一次
for (let i = row - zd ; i < row ; i++) {
console.log(h[i]);
if (h[i].style.display === "none") {
if (condition) {
condition = false;
console.log("移出超链接第一个元素"+h[row ].firstElementChild);
// 移除 h[row-1] 中的第一个子元素
h[row - zd].removeChild(h[row - zd].firstElementChild);
h[row - zd].insertBefore(h[row ].firstElementChild.cloneNode(true), h[row - zd].firstElementChild);
h[row ].removeChild(h[row ].firstElementChild);
h[row - zd].firstElementChild.style.backgroundColor = h[row - zd].children[1].style.backgroundColor;
}
// 显示被隐藏的行
h[i].style.display = "";
} else {
// 处理非隐藏行
let first = h[i].firstElementChild;
if (first && first.id.includes('A')) {
// console.log(first);
// 将 first 元素插入到 h[row-1] 的第一个位置
h[row].insertBefore(first.cloneNode(true), h[row].firstElementChild);
}
// 隐藏当前行
h[i].style.display = "none";
}
}