Disable drawCallback on table (re)draw

Disable drawCallback on table (re)draw

dylanmacdylanmac Posts: 49Questions: 7Answers: 1

I am grouping rows in my table using the code example provided here:
https://datatables.net/examples/advanced_init/row_grouping.html

I want to provide a button next to my table that will resort the rows using a different column, and that will not group on that row. Since the grouping is accomplished via the drawCallback, how would I bypass the drawCallback when I resort and redraw the table?

This question has an accepted answers - jump to answer

Answers

  • dylanmacdylanmac Posts: 49Questions: 7Answers: 1
    Answer ✓

    OK I figured it out. I surround the drawCallback function with a flag that is initially set to true, e.g.:

    var groupRows = true;
    
    "drawCallback": function ( settings ) { // add grouping
        if (groupRows) {
            var api = this.api();
            var rows = api.rows( {page:'current'} ).nodes();
            var last = null;
            api.column(orderByIndex, {page:'current'} ).data().each( function ( group, i ) {
                if ( last !== group ) {
                    $(rows).eq( i ).before(
                        '<tr class="group"><td colspan="' + visibleCols + '">'+group+'</td></tr>'
                    );
                    last = group;
                }
            } );
        }
    },
    

    When the user clicks on the sort button, change the global var to false and pass it into the draw() function, e.g.

    orderBy = function (actionType, colName) {
        orderByIndex = $('#productList th.' + colName).index();
        groupRows = (actionType != 'sortBy'); // set to false if sorting rows
        $table.order( [ orderByIndex, 'asc' ] ).draw(groupRows);
    };
    

    Voila.

This discussion has been closed.