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

时间:2012-07-07 22:02:34

标签: javascript jquery datatables

您好我使用jquery datatables plugin multi_filter_select个别列过滤(与单个列过滤相同,只是选择框)。 我有=前两列隐藏,这导致过滤框出现在错误的列下,因为前2个过滤框用于隐藏列,将所有内容向下推到2列。

这是我的代码:

用于个人过滤的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>';
}

这是准备好的文件:

$(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);
                } );
            } );
         }
    } );

和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 个答案:

答案 0 :(得分:1)

这是一个黑客,但你可以手动告诉fnGetColumnData()

正确的索引
this.innerHTML = fnCreateSelect(table.fnGetColumnData(i + 2));

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