Using a dropdown list to filter a table (dataTables)

前端 未结 3 1694
被撕碎了的回忆
被撕碎了的回忆 2021-02-14 23:30

I\'m using the dataTables jQuery plugin (which is totally awesome), but I\'m having trouble getting my table to filter based on the change of my select box.

Function:

相关标签:
3条回答
  • 2021-02-15 00:07
       $.extend( true, $.fn.dataTable.defaults, {
                "bFilter": true,
                    initComplete: function () {
                        this.api().column(1).every( function () {
                            var column = this;
                            var select = $('<select><option value="">All Subjects</option></select>')
                                .appendTo( $(column.header()).empty() )
                                .on( 'change', function () {
                                    var val = $.fn.dataTable.util.escapeRegex($(this).val());
                                    column
                                        .search( val ? '^'+val+'$' : '', true, false )
                                        .draw();
                                } );
                            column.data().unique().sort().each( function ( d, j ) {
                                select.append( '<option value="'+d+'">'+d+'</option>' )
                            } );
                        } );
                    },
            } );
    
    0 讨论(0)
  • 2021-02-15 00:10

    dataTables features

    I knew I had done this before, and you have to watch this little piece of information:

    Note that if you wish to use filtering in DataTables this must remain 'true' - to remove the default filtering input box and retain filtering abilities, please use sDom.

    you need to set {bFilter:true}, and move your <select></select> into a custom element registered through sDom. I can guess your code will look like this:

    $(document).ready(function() {
          $("#msds-table").dataTable({
            "sPaginationType": "full_numbers",
            "bFilter": true,
            "sDom":"lrtip" // default is lfrtip, where the f is the filter
           });
          var oTable;
          oTable = $('#msds-table').dataTable();
    
          $('#msds-select').change( function() { 
                oTable.fnFilter( $(this).val() ); 
           });
       });
    

    your code for oTable.fnFilter( $(this).val() ); will not fire while {bFilter:false}; according to the documentation

    0 讨论(0)
  • 2021-02-15 00:23

    Use this code:

     $('.btn-success').on('click',function (e) {
                   //to prevent the form from submitting use e.preventDefault()
                    e.preventDefault();
                    var res = $("#userid").val();  
                    var  sNewSource = "<?php echo base_url(); ?>myaccount/MyData_select?userid=" + res + "";
                    var oSettings = ETable.fnSettings();
                    oSettings.sAjaxSource  = sNewSource;
                    ETable.fnDraw();
                });
    
    0 讨论(0)
提交回复
热议问题