如何在使用数据表单独列过滤时忽略隐藏列

时间:2022-02-04 14:26:22

Hi i am using the jquery datatables plugin with the multi_filter_select individual column filtering(work the same as the individual column filtering only they are select boxes). i have= the first 2 columns hidden and this is causing the filtering boxes to show up under the wrong columns because the first 2 filtering boxes are for the hidden columns pushing everything down 2 columns.

您好我正在使用带有multi_filter_select单独列过滤的jquery datatables插件(与单个列过滤相同,只是它们是选择框)。我有=前两列隐藏,这导致过滤框显示在错误的列下,因为前2个过滤框用于隐藏列,将所有内容向下推送2列。

here is my code:

这是我的代码:

api functions for individual filtering

api用于个人过滤

(function($) {
/*
 * Function: fnGetColumnData
 * Purpose:  Return an array of table values from a particular column.
 * Returns:  array string: 1d data array
 * Inputs:   object:oSettings - dataTable settings object. This is always the last argument past to the function
 *           int:iColumn - the id of the column to extract the data from
 *           bool:bUnique - optional - if set to false duplicated values are not filtered out
 *           bool:bFiltered - optional - if set to false all the table data is used (not only the filtered)
 *           bool:bIgnoreEmpty - optional - if set to false empty values are not filtered from the result array
 * Author:   Benedikt Forchhammer <b.forchhammer@mind2.de>
 */
$.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) {
        // check that we have a column id
       // alert(iColumn);
       // alert(oSettings);
        if ( typeof iColumn == "undefined" ) return new Array();

        // by default we only wany unique data
        if ( typeof bUnique == "undefined" ) bUnique = true;

        // by default we do want to only look at filtered data
        if ( typeof bFiltered == "undefined" ) bFiltered = true;

        // by default we do not wany to include empty values
        if ( typeof bIgnoreEmpty == "undefined" ) bIgnoreEmpty = true;

        // list of rows which we're going to loop through
        var aiRows;
        // use only filtered rows
        if (bFiltered == true) aiRows = oSettings.aiDisplay;
        // use all rows
        else aiRows = oSettings.aiDisplayMaster; // all row numbers
        //alert(aiRows);
        // set up data array   
        var asResultData = new Array();

        for (var i=0,c=aiRows.length; i<c; i++) {
       // alert("inloop");
                iRow = aiRows[i];
                var aData = this.fnGetData(iRow);
                var sValue = aData[iColumn];

                // ignore empty values?
                if (bIgnoreEmpty == true && sValue.length == 0) continue;

                // ignore unique values?
                else if (bUnique == true && jQuery.inArray(sValue, asResultData) > -1) continue;

                // else push the value onto the result data array
                else asResultData.push(sValue);
        }

        return asResultData;

}}(jQuery));


function fnCreateSelect( aData )
{
    var r='<select><option value=""></option>', i, iLen=aData.length;
    for ( i=0 ; i<iLen ; i++ )
    {
        r += '<option value="'+aData[i]+'">'+aData[i]+'</option>';
    }
    return r+'</select>';
}

here is the document ready:

这是准备好的文件:

$(document).ready(function() {

var customerId = $("#customer").text();

    var fleetT = $('#fleetsTable').dataTable( {
        "bProcessing": true,
        "bServerSide": false,
        "sAjaxSource": "/fleet_processing.php",
        "aoColumns": [{"bVisible":false},{"bVisible":false},null,null,null,null,null],
         "fnInitComplete": function() {
                $("tfoot th").each( function ( i ) {
                    this.innerHTML = fnCreateSelect( fleetT.fnGetColumnData(i) );
                    $('select', this).change( function () {
                     //var realIndex = fleetT.oApi._fnVisibleToColumnIndex( fleetT.fnSettings(), jQuery('tfoot select').index(this));
                    fleetT.fnFilter( $(this).val(), i);
                } );
            } );
         }
    } );

and the html:

和HTML:

<table cellpadding="0" cellspacing="0" border="0" class="display" id="fleetsTable">
    <thead>
        <tr>
            <th width="20%">fleet id</th>
            <th width="20%">customer id</th>
            <th width="20%">Equipo</th>
            <th width="25%">Marca y Modelo</th>
            <th width="25%">Filter Type</th>
            <th width="15%">Model Number</th>
            <th width="15%">quantity</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td colspan="5" class="dataTables_empty">Loading data from server</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <th>fleet id</th>
            <th>customer id</th>
            <th>Equipo</th>
            <th>Marca y Modelo</th>
            <th>Filter Type</th>
            <th>Model Number</th>
            <th>quantity</th>
        </tr>
    </tfoot>
</table>

1 个解决方案

#1


1  

This is a hack, but you could manually tell the correct index to fnGetColumnData()

这是一个hack,但您可以手动告诉fnGetColumnData()正确的索引

this.innerHTML = fnCreateSelect(table.fnGetColumnData(i + 2));

http://jsfiddle.net/nicolapeluchetti/Yvpr5/

#1


1  

This is a hack, but you could manually tell the correct index to fnGetColumnData()

这是一个hack,但您可以手动告诉fnGetColumnData()正确的索引

this.innerHTML = fnCreateSelect(table.fnGetColumnData(i + 2));

http://jsfiddle.net/nicolapeluchetti/Yvpr5/