JS动态根据前面的列合并后面的列

需求合并行,在上一篇的基础上改进。


解决思路

1、获取前排列的rowspan值,存入数组,

2、再循环取出数组,根据rowspan的值合并

3、关键点用到jQuery的$("#xxx tbody td:nth-child(xx)")方法获取指定列,在循环遍历处理

代码:

   /**
     * 依据前面的列内容合并后面的列内容
     * @param table_id tableID
     * @param table_colnum 待合并的列
     * @param table_colnum_decide 依据的列
     */
    function servermergetable_rowspan(table_id, table_colnum, table_colnum_decide) {
//    查找依据列是否有相同待合并的
        var table_firsttd_decide = "";
        var table_currenttd_decide = "";
        var table_SpanNum_decide = 0;
        var array = [];
        var table_Obj_decide = $(table_id + " tbody td:nth-child(" + table_colnum_decide + ")");
        table_Obj_decide.each(function (i) {
            if (i == 0) {
                table_firsttd_decide = $(this);
                table_SpanNum_decide = 1;
            } else {
                table_currenttd_decide = $(this);
                if (table_firsttd_decide.text() == table_currenttd_decide.text()) {
                    table_SpanNum_decide++;
                } else {
                    table_firsttd_decide = $(this);
                    table_SpanNum_decide = 1;
                }
            }
            //将依据列td的rowspan属性加入数组
            array.push(table_SpanNum_decide);
        });
        var table_firsttd = "";
        var table_currenttd = "";
        var table_SpanNum = 0;
        var table_Obj = $(table_id + " tbody td:nth-child(" + table_colnum + ")");
        table_Obj.each(function (i) {
            if (i == 0) {
                table_firsttd = $(this);
                table_SpanNum = array[i];
            } else {
                table_currenttd = $(this);
                table_SpanNum = array[i];
                if (table_SpanNum == 1) {//rowspan值为1的不合并
                    table_firsttd = $(this);
                } else {//否则合并
                    table_firsttd.attr("rowSpan", table_SpanNum);
                    table_currenttd.remove();
                }
            }

        });
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QC班长

班长有话说:要是有瓶水喝就好了

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值