How to use rowspan and colspan in tbody using datatable.js?

前端 未结 7 1519
既然无缘
既然无缘 2020-11-29 06:36

Whenever I use , I get the following error:

Uncaught TypeError: Cannot set property \'_DT_CellIndex\'

相关标签:
7条回答
  • 2020-11-29 06:42

    COLSPAN: Ajax or JavaScript sourced data

    Excellent solution provided by Dirk Bergstrom only works with HTML sourced data.

    It is also possible to use the same idea with Ajax sourced data.

    You need to use createdRow option to modify required cells when TR element is created in table body.

    For example:

    var table = $('#example').DataTable({
        ajax: 'https://api.myjson.com/bins/qgcu',
        createdRow: function(row, data, dataIndex){
            // If name is "Ashton Cox"
            if(data[0] === 'Ashton Cox'){
                // Add COLSPAN attribute
                $('td:eq(1)', row).attr('colspan', 3);
    
                // Hide required number of columns
                // next to the cell with COLSPAN attribute
                $('td:eq(2)', row).css('display', 'none');
                $('td:eq(3)', row).css('display', 'none');
    
                // Update cell data
                this.api().cell($('td:eq(1)', row)).data('N/A');
            }
        }
    });
    

    See this example for code and demonstration.

    See jQuery DataTables: COLSPAN in table body TBODY - Ajax data article for more details.

    ROWSPAN

    It is possible to emulate ROWSPAN attribute using RowsGroup plug-in.

    To use the plugin, you need to include JavaScript file dataTables.rowsGroup.js and use rowsGroup option to indicate indexes of the columns where you want grouping to be applied.

    var table = $('#example').DataTable({
       'rowsGroup': [2]
    });
    

    See this example for code and demonstration.

    See jQuery DataTables: ROWSPAN in table body TBODY article for more details.

    COLSPAN and ROWSPAN

    It is possible to group cells both vertically and horizontally simultaneously if we combine both workarounds described above.

    See this example for code and demonstration.

    See jQuery DataTables: COLSPAN in table body TBODY - COLSPAN and ROWSPAN article for more details.

    0 讨论(0)
  • 2020-11-29 06:42

    Because datatables doesn't support colspan, I usually ended up with using unordered lists (ul) with list-style: none;

    here is the result

    0 讨论(0)
  • 2020-11-29 06:53

    Refer below link.

    fnFakeRowspan

    Also refer the plugin here.

    fnFakeRowspan plugin

    Hope this will help you

    0 讨论(0)
  • 2020-11-29 07:02

    Simple solution is here.

    <tr>
        <td colspan="4">details</td>
        <td style="display: none"></td>
        <td style="display: none"></td>
        <td style="display: none"></td>
    </tr>
    

    No other solution https://datatables.net/forums/discussion/33497/cannot-set-property-dt-cellindex-of-undefined

    0 讨论(0)
  • 2020-11-29 07:06

    You can hack around the lack of colspan support by adding an "invisible" cell for every cell that's eliminated:

    <tr>
     <td colspan="3">Wide column</td>
     <td style="display: none;"></td>
     <td style="display: none;"></td>
    </tr>
    <tr>
     <td>Normal column</td>
     <td>Normal column</td>
     <td>Normal column</td>
    </tr>
    

    DataTables doesn't complain, the table renders normally and sorting works (invisible columns sort as the empty string).

    I haven't tried this with rowspan.

    0 讨论(0)
  • 2020-11-29 07:06

    If you are using scrollX

    <thead>
    <tr>
     <th style="width: 20px;"></th>
     <th>column H</td>
     <th>column H</td>
    </tr>
    </thead>
    <tbody>
    <tr>
     <td colspan="3">Wide column</td>
     <td style="display: none;"></td>
     <td style="display: none;"></td>
    </tr>
    <tr>
     <td>123</td>
     <td>Normal column</td>
     <td>Normal column</td>
    </tr>
    </tbody>
    

    Modified answer of #Dirk_Bergstrom

    0 讨论(0)
提交回复
热议问题