How to add Rowspan in JQuery datatables

后端 未结 3 1514
执笔经年
执笔经年 2020-12-02 12:28

Im using Jquery datatables to construct a table.
My requirement is like below

This is not a static table, and we are rendering it using json data.
Here

3条回答
  •  独厮守ぢ
    2020-12-02 13:24

    Datatables does not support this kind of grouping out of the box. But, as in many cases, there is a plugin available.

    It is called RowsGroup and is located here: Datatables Forums. A live example is also included.

    If you change the JS part in this example to the below you will have your desired output presented to you in the output window.

    $(document).ready( function () {
      var data = [
        ['1', 'David', 'Maths', '80'],
        ['1', 'David', 'Physics', '90'],
        ['1', 'David', 'Computers', '70'],
        ['2', 'Alex', 'Maths', '80'],
        ['2', 'Alex', 'Physics', '70'],
        ['2', 'Alex', 'Computers', '90'],
      ];
      var table = $('#example').DataTable({
        columns: [
            {
                name: 'first',
                title: 'ID',
            },
            {
                name: 'second',
                title: 'Name',
            },
            {
                title: 'Subject',
            }, 
            {
                title: 'Marks',
            },
        ],
        data: data,
        rowsGroup: [
          'first:name',
          'second:name'
        ],
        pageLength: '20',
        });
    } );
    

    Here is a screenshot of the result:

提交回复
热议问题