Yoshita Arora
Yoshita Arora

Reputation: 495

Hide "show entries" if search results are less than 10 in datatables

I am using jQuery datatable plugin in my tables inside Django. I want the show-x-rows dropdown to hide when the search results are less than 10. I know how to permanently hide it using the bLengthChange:false. But I want to hide it only when the search results are <=10. Here's my javascript:

$('.datatable').DataTable({
    conditionalPaging: true,
    "dom": '<"table-search clearfix"f>t<"table-entries"i><"table-num"l><"table-pagination"p>',
    "language": {
      "emptyTable":     "{% trans "No data available in table" %}",
      "info":           "{% trans "Showing _START_ - _END_ of _TOTAL_" %}",
      "infoEmpty":      "{% trans "Showing 0 - 0 of 0" %}",
      "infoFiltered":   "{% trans "(filtered from _MAX_ total rows)" %}",
      "lengthMenu":     "{% trans "Show _MENU_ rows" %}",
      "search": '<div class="input-group"><span class="input-group-addon"><span class="glyphicon glyphicon-search"></span></span>',
      "searchPlaceholder": '{% trans "Search" %}',
      "zeroRecords":    "{% trans "No matching records found" %}",
      "paginate": {
        "next": '<span class="glyphicon glyphicon-triangle-right"></span>',
        "previous": '<span class="glyphicon glyphicon-triangle-left"></span>',
        "first":      "{% trans "First" %}",
        "last":       "{% trans "Last" %}"
      },
      "aria": {
        "sortAscending":  ": {% trans "activate to sort column ascending" %}",
        "sortDescending": ": {% trans "activate to sort column descending" %}"
      }

    }
  });

Maybe I'll need to fire callback function? If that's so, I don't know how to include it in there correctly. I am very new to this. Please help. Thanks.

Upvotes: 0

Views: 1155

Answers (1)

Yoshita Arora
Yoshita Arora

Reputation: 495

I resolved the problem by making a callback function like so:

$('.datatable').DataTable({

      // other properties:

             :
             :

    // callback function to hide the "show-x-row" dropdown for results <=10:

       "fnDrawCallback": function(oSettings){
            var rowCount = this.fnSettings().fnRecordsDisplay();
            if(rowCount<=10){
            $('.dataTables_length').hide();
            }
       }
 });

Upvotes: 1

Related Questions