How to apply filter to specific datatable

后端 未结 6 1371
既然无缘
既然无缘 2020-12-08 21:03

Is it possible to apply a certain filter to only one datatable? I have the following filter function that I am applying on document ready, I don\'t know if this is proper pr

相关标签:
6条回答
  • 2020-12-08 21:27

    You could create an array of tables to have the filter - then in your filter check if the current table is present in that array ... something like :

    // setup an array of the ids of tables that should be allowed
    var allowFilter = ['productTable'];
    
    $.fn.dataTableExt.afnFiltering.push(function(oSettings, aData, iDataIndex) {
    
        // check if current table is part of the allow list
        if ( $.inArray( oSettings.nTable.getAttribute('id'), allowFilter ) == -1 )
        {
           // if not table should be ignored
           return true;
        }
        var checked = $('#instock').is(':checked');
        var qntStock = 1; 
        var stockCol = 3; 
    
        if (!checked) {
            return true;
        }
        if (checked && aData[stockCol] > qntStock) {
            return true;
        }
    
        return false;
    });
    
    0 讨论(0)
  • 2020-12-08 21:34

    Haven't tried, but how about something like this ?

    $.fn.dataTableExt.afnFiltering.push(
    function( oSettings, aData, iDataIndex ) {
            if ( oSettings.nTable == document.getElementById( 'productTable' )){
                var checked = $('#instock').is(':checked');
                var qntStock = 1; 
                var stockCol = 3; 
    
                if (!checked) {
                    return true;
                }
                if (checked && aData[stockCol] > qntStock) {
                    return true;
                }
    
                return false;
            }
    }
    );
    

    the idea came from this thread : 2 datatables & 2 filters on the same page


    You can also try out my yadcf plugin for datatable , here its showcase url, its has 9 different types of filters + additional API functions that can help you load table pre filtered or add single filter for filtering multiple table and many other cool stuff..

    0 讨论(0)
  • 2020-12-08 21:34

    This is what we do:

                var temporarilySetFilterFunctions = $.fn.dataTableExt.afnFiltering;
                $.fn.dataTableExt.afnFiltering = [function (settings, data, index) {
                    // our filter function
                } ];
    
                this._table.dataTable().fnDraw(); // filter!
    
                $.fn.dataTableExt.afnFiltering = temporarilySetFilterFunctions;
    

    Basically store the existing filters in a TEMP variable and then revert it after we are done. Weird design descion on Allan's part to implement it like this. Ugly hack, but it works.

    0 讨论(0)
  • 2020-12-08 21:45

    It turns out filtering is global and indeed you have to filter the table element... it's quite disappointing.

    0 讨论(0)
  • 2020-12-08 21:47

    you can do something like this: add a parameter to the configuration:

    var oTable = $('#productTable').dataTable({
            "applyFilter":true,
            "aoColumnDefs": [{
                "sClass": "my_class", 
                "aTargets": [4]
                }],
            "bAutoWidth": false,
            "iDisplayLength": 100,
            "fnDrawCallback": function() {
                $("td.my_class").editable(function(value, settings) 
                { 
                    return(value);
                }, 
                {
                    indicator : 'Save...',
                    tooltip   : 'Click to Edit...'
                }
                );
            }
        });
    

    and then, verify if your filter is active:

    //Filter Function in Stock 
    //$('#productTable').
    $.fn.dataTableExt.afnFiltering.push(function(oSettings, aData, iDataIndex) {
        if(oSettings.applyFilter)
        {
            var checked = $('#instock').is(':checked');
            var qntStock = 1; 
            var stockCol = 3; 
    
            if (!checked) {
                return true;
            }
            if (checked && aData[stockCol] > qntStock) {
                return true;
            }
    
            return false;
        }
        else
            return true;
     });
    
    0 讨论(0)
  • 2020-12-08 21:50

    The following link will help in applying filter to data table. http://live.datatables.net/oyinin/3/edit#javascript,html

    I have used it like this:

      drawTable = function (tableId, url,    stateEngineURL) {
            resUrl = url;
            sUrl = stateEngineURL;
            oTable = $('#' + tableId).dataTable({
                "bAutoWidth" : false,
                "bProcessing" : false,
                "bServerSide" : false,
                "sScrollY" : "150px",
                "bPaginate" : true,
                "bDeferRender" : true,
                "bScrollInfinite" : true,
                "bSortCellsTop" : true,
                "sAjaxSource" : url,
                "aoColumns" : [
                    {
                        "mDataProp" : "clusterName"
                    }, {
                        "mDataProp" : "type"
                    }, {
                        "mDataProp" : "actions",
                        "bSortable": false
                    }
                ],
                "fnServerData": function (sSource, aoData, fnCallback) {
                    aoData.push({"name" : "REQUESTTYPE", "value" : "getCredentialResrcURL" });
                    $.getJSON(sSource, aoData, function (json) {
                        fnCallback(json);
                    });
                },
                "fnRowCallback" : function (nRow, aData) {
                    onRowCallBack(nRow, aData);
                }
            });
            oTable.dataTableExt.afnFiltering.push(
                    function( oSettings, aData, iDataIndex ) {
                       if(aData.type=='OSS 5.x'){
                           return false;
                       }
                    }
                );
            oTable.fnDraw();
    
    0 讨论(0)
提交回复
热议问题