EasyUI之datagrid的合并单元格

  • Post author:
  • Post category:其他


function mergeCells(dg, data, cellnames) {
    for (var i = 0; i < cellnames.length; i++) {
        var cellname = cellnames[i];
        var merges = [];
        for (var r = 0; r < data.rows.length; r++) {
            var row = data.rows[r];
            var mergeinfo = new Object();
            var nr = row[cellname];
            var rowspan = 1;
            for (var r1 = r + 1; r1 < data.rows.length; r1++) {
                var row1 = data.rows[r1];
                var nr1 = row1[cellname];

                if (nr1 && nr1 == nr && nr1 != "") {
                    mergeinfo.index = r;
                    rowspan++;
                    mergeinfo.rowspan = rowspan;
                }
                else {
                    if (rowspan > 1) {
                        merges.push(mergeinfo);
                        r = r1 - 1;
                    }
                    break;
                }
                if (r1 == data.rows.length - 1) {
                    if (rowspan > 1) {
                        merges.push(mergeinfo);
                        r = r1 - 1;
                    }
                }
            }
        }
        for (var j = 0; j < merges.length; j++) {
            $('#' + dg).datagrid('mergeCells', {
                index: merges[j].index,
                field: cellname,
                rowspan: merges[j].rowspan
            });
        }
    }
}

function mergeCells_table(dg, data, cellnames) {
    var table = document.getElementById(dg);
    for (var i = 0; i < cellnames.length; i++) {
        var cellname = cellnames[i];
        var merges = [];
        for (var r = 0; r < data.rows.length; r++) {
            var row = data.rows[r];
            var mergeinfo = new Object();
            var nr = row[cellname];
            var rowspan = 1;
            for (var r1 = r + 1; r1 < data.rows.length; r1++) {
                var row1 = data.rows[r1];
                var nr1 = row1[cellname];

                if (nr1 && nr1 == nr && nr1 != "") {
                    mergeinfo.index = r;
                    rowspan++;
                    mergeinfo.rowspan = rowspan;
                    table.rows[r + 1].cells[i].rowSpan = rowspan;
                    table.rows[r1 + 1].cells[i].style.display = "none";
                }
                else {
                    if (rowspan > 1) {
                        merges.push(mergeinfo);
                        r = r1 - 1;
                    }
                    break;
                }
                if (r1 == data.rows.length - 1) {
                    if (rowspan > 1) {
                        merges.push(mergeinfo);
                        r = r1 - 1;
                    }
                }
            }
        }
    }
}

function mergeCells_table1(dg, data, cellnames,cellindexs) {
    var table = document.getElementById(dg);
    for (var i = 0; i < cellnames.length; i++) {
        var cellname = cellnames[i];
        var merges = [];
        for (var r = 0; r < data.rows.length; r++) {
            var row = data.rows[r];
            var mergeinfo = new Object();
            var nr = row[cellname];
            var rowspan = 1;
            for (var r1 = r + 1; r1 < data.rows.length; r1++) {
                var row1 = data.rows[r1];
                var nr1 = row1[cellname];

                if (nr1 && nr1 == nr && nr1 != "") {
                    mergeinfo.index = r;
                    rowspan++;
                    var cellindex = cellindexs[i];
                    mergeinfo.rowspan = rowspan;
                    table.rows[r + 1].cells[cellindex].rowSpan = rowspan;
                    table.rows[r1 + 1].cells[cellindex].style.display = "none";
                }
                else {
                    if (rowspan > 1) {
                        merges.push(mergeinfo);
                        r = r1 - 1;
                    }
                    break;
                }
                if (r1 == data.rows.length - 1) {
                    if (rowspan > 1) {
                        merges.push(mergeinfo);
                        r = r1 - 1;
                    }
                }
            }
        }
    }
}


function mergeCells_one(dg, data, cellcomp, cellnames) {
    var merges = [];
    for (var r = 0; r < data.rows.length; r++) {
        var row = data.rows[r];
        var mergeinfo = new Object();
        var nr = row[cellcomp];
        var rowspan = 1;
        for (var r1 = r + 1; r1 < data.rows.length; r1++) {
            var row1 = data.rows[r1];
            var nr1 = row1[cellcomp];

            if (nr1 && nr1 == nr && nr1 != "") {
                mergeinfo.index = r;
                rowspan++;
                mergeinfo.rowspan = rowspan;
            }
            else {
                if (rowspan > 1) {
                    merges.push(mergeinfo);
                    r = r1 - 1;
                }
                break;
            }
            if (r1 == data.rows.length - 1) {
                if (rowspan > 1) {
                    merges.push(mergeinfo);
                    r = r1 - 1;
                }
            }
        }
    }
    for (var i = 0; i < cellnames.length; i++) {
        var cellname = cellnames[i];
        for (var j = 0; j < merges.length; j++) {
            $('#' + dg).datagrid('mergeCells', {
                index: merges[j].index,
                field: cellname,
                rowspan: merges[j].rowspan
            });
        }
    }
}

调用示例:

$('#xx').datagrid({
            width: 'auto',
            height: 'auto',
            scrollbarSize: 0,
            url: 'xxx',
            queryParams: {},
            loadMsg: '正在加载....',
            columns: [[
                { field: 'ck', title: '', width: 40, checkbox: true },
                { field: 'id', hidden: true },
                { field: 'bh', title: '编号', width: 60 },
                { field: 'mc', title: '名称', width: 60 }
            ]],
            pageSize: 10,
            pageList: [10, 20, 30, 50, 100],
            pagination: true, 
            pageNumber: num,
            nowrap: false,
            fitColumns: true,
            striped: false,
            rownumbers: false, 
            singleSelect: false, 
            onLoadSuccess: function (data) {
                var cellcomp = 'id';
                var cells = ['bh', 'mc'];
                mergeCells_one('xx', data, cellcomp, cells);
            },
            rowStyler: function (i, row) {
                
            }
        });



版权声明:本文为u012949335原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。