Update language.info after redrawing table

Update language.info after redrawing table

hafizctnhafizctn Posts: 5Questions: 3Answers: 0

I am updating data in an already drawn datatable. I want to show only the first 10 records to my users. But at the same time I also want to show the total number of records in my database. Following is my code:

    function loadBooks (url){
              $.ajax({
                url: url,
                type: "GET"
              }).done(function (result) {
                table2 .clear().draw();
                table2 .rows.add(result.data).draw();
                table2 .columns.adjust();
                table2 .language.info = "Showing first 10 records of " + result.records;// unable to update the info.
            table2 .responsive.recalc();
              });
    } 

    table2 = $("#table2").DataTable({
        "language": {
        "info": "Showing page _PAGE_ of _PAGES_ Records: _TOTAL_"
      }
    });   

Any help please.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,363Questions: 26Answers: 4,777
    Answer ✓

    Maybe using infoCallback is more appropriate for you requirements. Assign result.records to a global variable that you can access from within the infoCallback function.

    Kevin

  • hafizctnhafizctn Posts: 5Questions: 3Answers: 0

    Thank you. It did the trick.

    Here is the complete solution for the benefit of the others:

                booksifno = '';
                function loadBooks (url){
                          $.ajax({
                            url: url,
                            type: "GET"
                          }).done(function (result) {
                            table2 .clear().draw();
                            table2 .rows.add(result.data).draw();
                            table2 .columns.adjust();
                            booksinfo = " of " + result.records;
                             table2 .responsive.recalc();
                          });
                }
             
            table2 = $("#table2").DataTable({
               "infoCallback": function( settings, start, end, max, total, pre ) {
                return "Showing " + total + booksinfo;
              },
            });   
    
Sign In or Register to comment.