Check if row exists in Datatable Jquery

Check if row exists in datatable jquery:

In this tutorial, we will talk about how we can check if the data table is empty or not. Before that, we need to know how jquery data table works.


Check if row exists in Datatable Jquery


1. DataTables have inbuilt many customizable features like pagination, data search and showing a particular number of rows. We can add the class to any row data.

2. Always when we bind any data table, it initialization first.


Check if row exists in Datatable Jquery

Step 1: Create a function which contains timeout function


function check_data_count() {

            var table = $('#mytbl').DataTable();

            setTimeout(function () {

                var isEmpty = table.rows().count() === 0;

                if (!isEmpty)

                    alert('table is empty');

                else

                    alert('table is not empty');

            }, 1000);

        }


Step 2:
Call the above function just after binding the data table.


function get_data() {

            $("#mytbl").DataTable({

                destroy: true,

                dom: "Bflrtip",

                "serverSide": false,

                scroller: true,

                ajax: {

                    "url": path + '/WebService.asmx/getdata', "dataSrc": "",

                    "type": "POST",

                    "data":

                    { },

                },

                columns:

                    [

                        { 'data': 'total' },

                        { 'data': 'item' },

                        { 'data': 'category' }

                    ],

                order: [0, "desc"],

                select: {

                    style: 'multi'

                },

                buttons: [

                    'excel',

                    'pdf',

                    'print'

                ]

            });

            check_data_count();

        }

Note: I am using the timeout function to give time to bind the data table, If you remove the timeout then it will not work properly.

Post a Comment

Post a Comment (0)

Previous Post Next Post