Showing posts with label DataTable.js. Show all posts
Showing posts with label DataTable.js. Show all posts

Saturday, March 7, 2020

Datatable.net - Get Reordering of the columns & Get Column visible

Datatable.net - Get Reordering of the columns & Get Column visible

Please add a button on the form like below
<a class="btn btn-success visiblecheck" href="javascript:void(0);" id="visiblecheck" value="test">visiblecheck</a>

Here is 'table' is variable of Datatable.net like

var table = $('#example').dataTable();


$('.visiblecheck').click(function () {
        alert(table.colReorder.order());
        var columnvisible = '';

        for (i = 0; i < table.columns().count(); i++) {
            if (columnvisible != '') {
                columnvisible += ',';
            }
            if (table.column(i).visible() === true) {
                columnvisible += '1';
            }
            else {
                columnvisible += '0';
            }
        }
        alert(columnvisible);
    });


table.colReorder.order() -  this will return the ordering of columns

Tuesday, October 22, 2019

DataTable.js, Change the Color of Row based on the Cell Value

DataTable.js, Change the Color of Row based on the Cell Value


Suppose you wants to change the color of Table Row on the bases of column value, then we have to use rowCallback as the below code

rowCallback: function (row, data, index) {
            if (data[3] ==="some value") {
                $(row).css('color', 'red');
            }           
        }

This code should be used like the below:

var table = $('#TableName').DataTable({
        'paging': true,
        'lengthChange': true,
        'searching': true,
        'ordering': true,
        'info': true,
        'autoWidth': false,
        'ajax': 'JsonData.json',
        'columnDefs': [{
            'targets': -1,
            'data': null,
            'defaultContent': 'html">'
        }],
        dom: 'Bfrtip',
        buttons: [{
            extend: 'colvis',
            text: 'Hide/Show Columns',
            columnText: function (dt, idx, title) {
                return (idx + 1) + ': ' + title;
            }
        }],
        rowCallback: function (row, data, index) {
           if (data[3] ==="some value")  {
                $(row).css('color', 'red');
            }
        }
    })