How to add Rowspan in JQuery datatables

2019-01-10 23:57发布

Im using Jquery datatables to construct a table.
My requirement is like below
enter image description here

This is not a static table, and we are rendering it using json data.
Here I'm, rendering the rows dynamically using "aoColumns".
Is there any way to use rowspan so that the cells (1,2,David,Alex) can be spanned.
Does datatables support this kind of table ?

3条回答
【Aperson】
2楼-- · 2019-01-11 00:29

add a below code and modify according to your requirement

$(window).on("load",function() {           
            MakeRows();
            addRowspan();
            $(".paginate_button").on("click", function() {
                MakeRows();
                addRowspan();
            });
        });

        function MakeRows() {
            var tmp_tbl = $("#dashboardDetails");
            var _l = tmp_tbl.find("tr");
            var _td = "",_t_td = "", old_txt = "",_t_txt = ""; _tr_count = 1;_tr_countadd = 1;
            for(i = 0;i< _l.length; i ++) {
                _t_td = tmp_tbl.find("tr").eq(i).find("td").eq(0).find("span");
                _t_txt = $(_t_td).text();
                _t_txt = _t_txt.replace(/\//,"_");_t_txt = _t_txt.replace(/\//,"_");

                if (_t_txt.length > 0) {
                    if(_t_txt != old_txt) {
                        if($(_l).eq(i).hasClass(_t_txt) == false) {
                            _tr_count = 1;_tr_countadd = 1;
                            $(_l).eq(i).addClass("" + _t_txt + "").addClass(_t_txt + "_" + i);
                        }
                        old_txt = _t_txt;
                    } else {
                        _tr_count = _tr_count + 1;
                        if (_tr_countadd == 1) {
                            $(_l).eq(i).addClass("" + _t_txt + "").addClass(_t_txt + "_" + i)
                                .addClass("hiddenClass").addClass("maintr").attr("trcount", _tr_count).attr("addedtrcount", "maintr");                            
                            _tr_countadd = _tr_countadd + 1;
                        } else {
                            $(_l).eq(i).addClass("" + _t_txt + "").addClass(_t_txt + "_" + i)
                                .addClass("hiddenClass").attr("trcount", _tr_count)
                        }
                    }
                }
                _t_td = "";
            }
        }

        function addRowspan() {
            var t_txt = "";
            var _alltrmain = $(".maintr");
            var _h_td = ["0","10","11","12","13"];
            for (i = 0; i <= _alltrmain.length; i ++) {
                for (j = 0; j <= _h_td.length; j ++) {
                    t_txt = $(_alltrmain).eq(i).attr("trcount");
                    $(_alltrmain).eq(i).prev().find("td").eq(_h_td[j]).attr("rowspan", t_txt);
                }
            }
        }
查看更多
Viruses.
3楼-- · 2019-01-11 00:34

I tried the RowsGroup plugin, but it achieves this just by hijacking the DataTables sort mechanism. If you tell it to group a given column, what it does for you is basically to apply a sort to that column that you can't turn off. So, if you want to sort by another column, you can't. That didn't work in my application.

Instead, here's a working fiddle for a recipe that allows you to achieve this result:

https://jsfiddle.net/bwDialogs/fscaos2n

The basic idea is to flatten all of your multi-row data into a single row. Content in your 2nd, 3rd, etc. rows are stored as a hidden <script> template tag within your first row.

It works by using DataTables' drawCallback function to manipulate the DOM once DataTables has rendered it, without confusing DataTables by having to try parsing rowspan cell content.

Since this modifies the DOM after DataTables has done its magic, your multi-row sections will stick together even with pagination, searching, and sorting.

Cheers.

查看更多
神经病院院长
4楼-- · 2019-01-11 00:37

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:

enter image description here

查看更多
登录 后发表回答