Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i display loading/processing message inside DataTable?

In my application i am using datatables.net

var ticketHistoryDataTable = $('#ticketHistoryData').DataTable({ 
        paging: false,
        data: [],
        searching: false,
        columns: [
            { data: 'ticket_id'         ,   title: "Ticket Number" },
            { data: 'transactiondate'   ,   title: "Date"          } 
        ]
} );

I am adding data to the table following way:

    var result_data = [{
            ticket_id         : '' ,
            transactiondate   : '' 
    },{
            ticket_id         : '' ,
            transactiondate   : '' 
    }];

    ticketHistoryDataTable.clear().draw();
    ticketHistoryDataTable.rows.add(result_data).draw();

result_data itself comes from jquery ajax get call to server. Retrieving the information may take some time, during which i want to display loading-processing message from datatable. What is correct way of doing this?

like image 463
Tornike Shavishvili Avatar asked Jun 06 '16 06:06

Tornike Shavishvili


People also ask

How do I know if a DataTable is loaded?

fn. dataTable. isDataTable() method provides the ability to check if a table node is already a DataTable or not. It can be accessed at any time, even before any DataTable has been created on the page.

How do you demonstrate the use of Ajax loading data in DataTables?

This can be implemented by using the columns. data option of DataTables plugin API. The source returns an array of objects which is used to display the data in the HTML table.


2 Answers

You can use a loader in your html. Position should be same as the table. How to add a loader in HTML

or Message container: <div id="MessageContainer"></div> and Apply some CSS styles for good look and feel.

     $('#ticketHistoryData')
        .on( 'draw.dt', function () {
            console.log( 'Loading' );
          //Here show the loader.
          // $("#MessageContainer").html("Your Message while loading");
        } )
        .on( 'init.dt', function () {
            console.log( 'Loaded' );
           //Here hide the loader.
            // $("#MessageContainer").html("Your Message while load Complete");
        } )
        .DataTable({ 
            paging: false,
            data: [],
            searching: false,
            columns: [
                { data: 'ticket_id'         ,   title: "Ticket Number" },
                { data: 'transactiondate'   ,   title: "Date"          } 
            ]
     });

For more go through Events of DataTable

I think this might help you.

You might show message

like image 175
Tuhin Avatar answered Oct 16 '22 23:10

Tuhin


You can use dom option to show loading:

$('#example').dataTable( {
  "dom": 'lrtip'
} );

"r" letter is related to show loading element.
For more information refer to this link

like image 20
Majid Basirati Avatar answered Oct 17 '22 01:10

Majid Basirati