jgrid合并单元格

方式1:根据grid索引
/**
 * @param colIndexs 索引 从0开始
 */
jQuery.fn.tuiTableRowSpan = function(colIndexs) {
    return this.each(function() {
        var indexs = eval("([" + colIndexs + "])");
        for (var i = 0; i < indexs.length; i++) {
            var colIdx = indexs[i];
            var that;
            $('tbody tr', this).each(function(row) {
                $('td:eq(' + colIdx + ')', this).filter(':visible').each(function(col) {
                    if (that != null && $(this).html() == $(that).html()) {
                        rowspan = $(that).attr("rowSpan");
                        if (rowspan == undefined) {
 
                            $(that).attr("rowSpan", 1);
                            rowspan = $(that).attr("rowSpan");
                        }
                        rowspan = Number(rowspan) + 1;
                        $(that).attr("rowSpan", rowspan); // do your action for the colSpan cell here
                        $(this).hide(); // .hide(); // do your action for the old cell here
                    } else {
                        that = this;
                    }
                    // that = (that == null) ? this : that; // set the that if not already set
                });
 
            });
        }
    });
};

方式二:根据列名合并

/**
 * jqgrid合并单元格的方法
 * @param gridName 表名
 * @param names 合并列名 数组的形式:["xm","xb"]
 */
function Merger(gridName,names) {
	//获取jgrid生成的第一个tr标签
    var trs = $("#"+gridName+">tbody>tr:gt(0)");
    //循环合并单元格
    $.each(names, function (ind, name) {
    	//获取当前tr对象,根据jgrid生成标签的规则aria-describedby='表格_属性'
          var bg = trs.eq(0).children("[aria-describedby='"+gridName+"_" + name + "']");
          var preZfbh = trs.eq(0).children("[aria-describedby='"+gridName+"_zfbh']");//此处为特殊处理,获取表格的唯一标识
          var index = bg.index();
           var  rowsp = 1;
        trs.slice(1).each(function (ind2, tr) {//slice方法可从已有的数组中返回选定的元素。
            var me = $(tr).children("td").eq(index);
            var meZfbh = $(tr).children("td").eq(preZfbh.index());
            if (bg.text() === me.text() && meZfbh.text() === preZfbh.text()) {//如果值一样并且表格的唯一标识一样就合并行数+1 然后设置rowspan,让当前单元格隐藏
                rowsp++;
                me.hide();
            } else {
                bg.attr("rowspan", rowsp);
                bg = me; 
                rowsp = 1;
            }
            bg.attr("rowspan", rowsp);
            preZfbh = meZfbh;
        });
    });
}


你可能感兴趣的:(jgrid合并单元格)