Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datatable row grouping

I have a working datatable that is retrieving data from a file :

enter image description here

I want to group row, something like this :

https://datatables.net/examples/advanced_init/row_grouping.html

My goal would to have my datatable to look like this, grouping row by the date (last column), but even looking at the code, I have trouble to make it work. I'd like some help to make that work if you could.

Here is the code of my datatable :

$(document).ready(function() {
         $('#datatable').DataTable( {
         "ajax": "<?php echo base_url()."assets/files/data/data.txt"; ?>" ,
         "columns": [
            { "data": "etat" },
            { "data": "dest" },
            { "data": "message" },
            { "data": "exp" },
            { "data": "date" }
            ],
            "order": [[ 0, "desc" ]],
            "responsive": true
            } );
         } );
like image 949
Komarzer Avatar asked Mar 10 '16 16:03

Komarzer


1 Answers

You should use drawCallback function.

Try.

$(document).ready(function() {
$('#datatable').DataTable({
"columns": [
        { "data": "etat" },
        { "data": "dest" },
        { "data": "message" },
        { "data": "exp" },
        { "data": "date" },
        { "data": "extra" }
        ],
        "order": [[ 4, "desc" ]],
        "responsive": true,
        drawCallback: function (settings) {
            var api = this.api();
            var rows = api.rows({ page: 'current' }).nodes();
            var last = null;

            api.column(4, { page: 'current' }).data().each(function (group, i) {

                if (last !== group) {

                    $(rows).eq(i).before(
                        '<tr class="group"><td colspan="8" style="BACKGROUND-COLOR:rgb(237, 208, 0);font-weight:700;color:#006232;">' + 'GRUPO ....' + group  + '</td></tr>'
                    );

                    last = group;
                }
            });
        }

});

} );

Result: https://jsfiddle.net/cmedina/7kfmyw6x/13/

like image 182
CMedina Avatar answered Sep 28 '22 02:09

CMedina