狐狸.fox 2016-02-18 02:39 采纳率: 0%
浏览 21

隐藏数据表中的记录

What I want to do is to hide the records/data from the datatable so that, by default, it will show "no data available in table". But, when the user uses the search function, it will display the matching records. If there is no data in the search and in my column filters, there will be no data to be displayed. In short, I just want my records to not appear as default. The records should only appear when searched.

Setting deferLoading: 0 doesn't load the data at first, but when I delete the info from my search bar, the records still show.

My script:

$(document).ready(function(){
    var dataTable = $('#example').dataTable({
        dom: 'T<"clear">lfrtip',
        "tableTools": {
            "sSwfPath": "../assets/swf/copy_csv_xls_pdf.swf",
            "sRowSelect": "multi",
            "aButtons": [
                "select_all",
                "select_none",
                {
                    "sExtends":    "collection",
                    "sButtonText": "Advance Tools",
                    "aButtons":    [ "csv", "xls", "pdf","print" ]
                }
            ]
        },
        bProcessing: true,
        bServerSide: true,
        bRegex:true,
        "oLanguage" : {"sZeroRecords": "", "sEmptyTable": ""},
        deferLoading: 0,
        sAjaxSource: "server_processing.php"
    });
});
  • 写回答

1条回答 默认 最新

  • weixin_33709609 2016-02-18 08:05
    关注

    You could use a custom filter to hide the rows, and then reset / set this filter when the user do a search / clears the search box.

    function hideRowsFilter() {
      $.fn.dataTableExt.afnFiltering.push(function(oSettings, aData, iDataIndex) {
         return false
      })
      dataTable.fnDraw();
    }  
    function clearFilter() {
      $.fn.dataTableExt.afnFiltering.pop();
      dataTable.fnDraw();  
    } 
    

    after you have initialised the dataTable, set the filter

    hideRowsFilter();
    

    reset / set the filter according to user input

    $('.dataTables_filter input').on('keyup', function() {
       if (this.value != '' ) {
          clearFilter();
       } else {
          hideRowsFilter();
       }
    })
    

    demo -> http://jsfiddle.net/zusz9a0n/

    The demo and the code above is using 1.9.x, something tells me you are using 1.9.x - but it will work with 1.10.x as well.

    评论

报告相同问题?