帆软js折叠行

如果对你有帮助不要钱的赞点一点(嘿嘿)

在写入函数的时候先在参数中填写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";
            }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值