easyUI datagrid 多列合并单元格

為{幸葍}努か 提交于 2019-12-10 04:58:49

自定义方法: 

/**
     * datagrid 自定义合并单元格
     * @param $this 表格选择器
     * @param field_arr 合并列字段
     * @param judge 值不相同不合并
     */
    customMergeCells: function($this, field_arr, judge) {
        var rows = $this.datagrid("getRows");
        if ( (typeof (field_arr) === "undefined" || field_arr === "" || field_arr == null || field_arr === "null")
            || (typeof (field_arr) === "undefined" || field_arr === "" || field_arr == null || field_arr === "null")) {
            return;
        }
        for (var i = 1; i < rows.length; i++) {
            for (var k = 0; k < field_arr.length; k++) {
                var field = field_arr[k]; // 要排序的字段
                if(rows[i][field] === rows[i-1][field]){ // 相邻的上下两行
                    if(!(typeof (judge) === "undefined" || judge === "" || judge == null || judge === "null")){
                        if(rows[i][judge] !== rows[i-1][judge]){
                            continue;
                        }
                    }
                    var rowspan = 2;
                    for (var j = 2; i-j >= 0; j++) { // 判断上下多行内容一样
                        if(rows[i][field] !== rows[i-j][field]){
                            break;
                        }else{
                            if(!(typeof (judge) === "undefined" || judge === "" || judge == null || judge === "null")){
                                if(rows[i][judge] !== rows[i-j][judge]){
                                    break;
                                }
                            }
                            rowspan = j+1;
                        }
                    }
                    $this.datagrid("mergeCells",{ // 合并
                        index: i-rowspan+1,
                        field: field,
                        rowspan: rowspan
                    });
                }
            }
        }
    }

使用实例: 

onLoadSuccess: function(data) {
                    if (data.rows && data.rows.length > 0) {
                        // 合并单元格
                        top.$.tools.customMergeCells($(this), ["itemName", "developUnitName"], "itemName");
                    }
                },

效果图:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!