合并列中相同的单元格

第一步:在列上加上cellattr属性

name: 'Category', index: 'Category', width: 40, align: "center",
                cellattr: function (rowId, tv, rawObject, cm, rdata) {
                    //合并单元格
                    return 'id=\'Category' + rowId + "\'";
                }

 

第二步:给gridComplete加方法

gridComplete: function() {
                var gridName  = "tbList";
                Merger(gridName, 'Category');
            }

 

第三步:Merger(gridName, CellName)方法

//公共调用方法
    function Merger(gridName, CellName) {
        //得到显示到界面的id集合
        var mya = $j("#" + gridName + "").getDataIDs();
        //当前显示多少条
        var length = mya.length;
        for (var i = 0; i < length; i++) {
            //从上到下获取一条信息
            var before = $j("#" + gridName + "").jqGrid('getRowData', mya[i]);
            //定义合并行数
            var rowSpanTaxCount = 1;
            for (j = i + 1; j <= length; j++) {
                //和上边的信息对比 如果值一样就合并行数+1 然后设置rowspan 让当前单元格隐藏
                var end = $j("#" + gridName + "").jqGrid('getRowData', mya[j]);
                if (before[CellName] == end[CellName]) {
                    rowSpanTaxCount++;
                    $j("#" + gridName + "").setCell(mya[j], CellName, '', { display: 'none' });
                } else {
                    rowSpanTaxCount = 1;
                    break;
                }
                $j("#" + CellName + "" + mya[i] + "").attr("rowspan", rowSpanTaxCount);
            }
        }
    }

看效果:

 

posted @ 2018-06-15 14:59  爱生活,爱代码  阅读(237)  评论(0编辑  收藏  举报