Do not show table when there is no data

Do not show table when there is no data

jsiddharthjsiddharth Posts: 3Questions: 1Answers: 0

A developer has used this library to integrate into our app. But has forgotten to manage the case where the data set is 0 sized. How is this case handled ?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,075Questions: 1Answers: 10,384 Site admin
    Answer ✓

    The default way it is handled is to show a message in the table stating that it is empty: http://live.datatables.net/labosele/1/edit .

    If you want to hide it completely, use drawCallback and hide the container if there are no records (page.info() and table().wrapper() are the two methods you'll need).

    Allan

  • jsiddharthjsiddharth Posts: 3Questions: 1Answers: 0

    My java ajax method returns "" if there is no data.

    Ajax Code

    class AjaxDataTableWithCheckBox{
    
        constructor(url,tableId,columnData){
            this.url=url;
            this.tableId="#"+tableId;
            this.columnData = columnData;
        }
    
        initAjaxDataTableWithCheckBox(){
            this.table = $(this.tableId).DataTable({
                        "ajax": {
                            "type" : "GET",
                            "url" : this.url,
                            "dataSrc": function (json) {
                                return json.data;
                            }       
                            },
                            'columns' :this.columnData,
                            'columnDefs' : [ {
                                'targets' : 0,
                                'searchable' : false,
                                'orderable' : false,
                                'width' : '1%',
                                'className' : 'dt-body-center',
                                'render' : function(data, type, full, meta) {
                                    return '<input type="checkbox" data-tableid='+meta.settings.sTableId+' data-rideid='+full.rideId+' data-bookingid='+full.bookingId+' data-userId='+full.userId+'>';
                                }
                            }],
                            'order' : [ 1, 'asc' ],
                            'rowCallback' : function(row, data, dataIndex) {
                                var rowId = data[0];
                                if ($.inArray(rowId, rows_selected) !== -1) {
                                    $(row).find('input[type="checkbox"]').prop(
                                            'checked', true);
                                    $(row).addClass('selected');
                                }
                            },
                            "initComplete": function(settings, json) {
                                $(".totalLimit").text(" / " + json.data.length);
                              }
                        });
    
            var rows_selected = [];
            if (this.table.data.length <= 0) {
                             // show my notification, this shows, but so does the empty table
    
  • jsiddharthjsiddharth Posts: 3Questions: 1Answers: 0

    I had a

     $("#ajaxDataTableWithCheckBox").modal('show');
    

    I removed it and placed it inside

     if (this.table.data.length <= 0) {
    

    It worked. Thanks

This discussion has been closed.