欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Jquery.DataTable合并单元格

程序员文章站 2024-02-28 18:50:16
...

$(function() {
    var grid = new Datatable();

    grid.init({
        src: $("#list-table"),
        onSuccess: function(grid) {},
        onError: function(grid) {},
        loadingMessage: '加载中...',
        dataTable: {
            "columns": [
   {
    "data": "id",
       "bSortable": false
   },
            {
                "data": "org_name",
                "bSortable": false,
                "sWidth":"100px"
            },
            {
                "data": "resource_name",
                "bSortable": false,
                "sWidth":"160px"
            }
            
        ],
            "columnDefs": [
     {
      "targets": [0],
      "render": function(data,type,full){
       return "<input type='checkbox' name='id' value='" + data + "' status='" + full.status+ "'/>";
      },
       "createdCell": function(cell, cellData, rowData, rowIndex, colIndex){
        var row_name = "id";
                       handleRowSpan(row_name, cell, rowData);  
               }
            
           ],
            "pageLength": 10,
            "ajax": {
                "url": ',
                'data' : function (d) {
                 d.resource_name=$('#resource_name').val();
                 d.cata_title=$('#cata_title').val();
                  },
             "dataSrc": function(res) {
               return getRowSpanData(res.data, ['id','org_name']);
              }
            },
            "order": [[0, "des"]]
        }
    });




function handleRowSpan(row_name, cell, rowData){
    if(rowData[row_name+'-row-span-num']>1){
        $(cell).attr('rowspan',rowData[row_name+'-row-span-num']);
    }
    else if(rowData[row_name+'-row-span-num']<1){
        $(cell).remove();
    }
};


function getRowSpanData(data, rowNameArr) {
    function rowSpanNum(row_name, current_row) {
        var num = 1;
        //如果与上一行的数据相同,则标记为0
        if((current_row > 0)&&(data[current_row - 1][row_name] === data[current_row][row_name])){
            num = 0;
        }
        //否则 计算共有几行重复
        else{
            var i = current_row + 1;
            while(i < data.length && (data[i][row_name] === data[current_row][row_name])){
                num++;
                i++;
            }
        }
        return num;
    }

    $.each(data,function (key, value) {
        $.each(rowNameArr, function (index, item) {
            value[item+'-row-span-num'] = rowSpanNum(item, key);
        });
    });
    return data;
};