1

我正在通过数据表为我的 php 脚本实现排序和搜索功能。对于排序它的好。对于搜索,我有 3 个文本字段、1 个年龄范围字段和 1 个下拉菜单。下拉框的不同值是“所有者”、“超级管理员”、“管理员”和“代理”。因此,当我选择值“admin”时,记录列表将显示其中包含“admin”和“superadmin”。我可以理解这是在“superadmin”一词中显示的,“admin”一词在那里。所以只有这个下拉框,我怎么能限制确切的词。我的代码是这样的:

<script type="text/javascript" >
                    (function($) {
                        $.fn.dataTableExt.oApi.fnGetColumnData = function ( oSettings, iColumn, bUnique, bFiltered, bIgnoreEmpty ) {
                // check that we have a column id
                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

                // set up data array    
                var asResultData = new Array();

                for (var i=0,c=aiRows.length; i<c; i++) {
                    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>';
            }


                        function fnFilterColumn ( i )
                        {

                            $('#example').dataTable().fnFilter( 
                                $("#col"+(i+1)+"_filter").val(),
                                i
                            );
                        }

                        /* Custom filtering function which will filter data in column four between two values */
                            $.fn.dataTableExt.afnFiltering.push(
                                function( oSettings, aData, iDataIndex ) {
                                    var iMin = document.getElementById('min').value * 1;
                                    var iMax = document.getElementById('max').value * 1;
                                    var iVersion = aData[4] == "-" ? 0 : aData[4]*1;
                                    if ( iMin == "" && iMax == "" )
                                    {
                                        return true;
                                    }
                                    else if ( iMin == "" && iVersion < iMax )
                                    {
                                        return true;
                                    }
                                    else if ( iMin <= iVersion && "" == iMax )
                                    {
                                        return true;
                                    }
                                    else if ( iMin <= iVersion && iVersion <= iMax )
                                    {
                                        return true;
                                    }
                                    return false;
                                }
                            );

                        $(document).ready(function(){


                            $('#example').dataTable( {
                                "bProcessing": true,
                                //"bServerSide": true,
                                "sAjaxSource": "datatabledb.php",
                                "bJQueryUI": true,
                                "sPaginationType": "full_numbers",
                                "sDom": 'T<"clear">lfrtip',
                                "oTableTools": {
                                    "aButtons": [

                                        {
                                            "sExtends": "csv",
                                            "sButtonText": "Save to CSV"
                                        }
                                    ]
                                },
                                "oLanguage": {
                                        "sSearch": "Search all columns:"
                                    },
                                "aoColumns": [
                                                null,
                                                { "bSortable": false }, // disable the sorting property for checkbox header
                                                null,
                                                null,
                                                null,
                                                null,
                                                null,
                                                null,
                                                null,
                                                null
                                            ]


                            } );

                            $("#example").dataTable().columnFilter(
                               {  aoColumns: [
                                                    null,
                                                    null,
                                                    null,
                                                    null,
                                                    null,
                                                    null,
                                                    null,
                                                    {
                                                         type: "select",
                                                         values: [ 'Owner', 'Superadmin', 'Admin','Agent' ]
                                                    },
                                                    null,

                                                    null
                                                 ]
                               }
                 );




                            $("#col1_filter").keyup( function() { fnFilterColumn( 0 ); } );

                            $("#col3_filter").keyup( function() { fnFilterColumn( 2 ); } );

                            $("#col4_filter").keyup( function() { fnFilterColumn( 3 ); } );

                            //$("#col5_filter").keyup( function() { fnFilterColumn( 4 ); } );


                            //$("#col8_filter").keyup( function() { fnFilterColumn( 7 ); } );

                            var oTable = $('#example').dataTable();
                            /* Add event listeners to the two range filtering inputs */
                            $('#min').keyup( function() { oTable.fnDraw(); } );
                            $('#max').keyup( function() {oTable.fnDraw(); } );


                            //$('table').dataTable({"bFilter": false});




                        });





                     </script>
<table cellpadding="3" cellspacing="0" border="0" class="display userTable" aria-describedby="example_info">

                <tbody>
                    <tr id="filter_col1">
                        <td>Name: </td>
                        <td><input type="text"     name="col1_filter" id="col1_filter"></td>
                        <td>Email:                      </td>
                        <td><input type="text"     name="col4_filter" id="col4_filter" /></td>
                        <td>Username: </td>
                        <td><input type="text"     name="col3_filter" id="col3_filter" /></td>
                        <td>Min Age:</td>
                        <td><input type="text" id="min" name="min" maxlength="2" class="ageFeild" onkeypress="return numbersonly(event, false)" /></td>
                        <td>Max Age: </td>
                        <td><input type="text" id="max" name="max" maxlength="2" class="ageFeild" onkeypress="return numbersonly(event, false)" /></td>
                        <td style="padding-left:25px;">Privilege:</td>
                    </tr>
                    </tbody>
                 </table>


                     <table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="form_table display" id="example">

<thead>
                            <tr>                            
                                <th class="sorting_asc">Name</th>
                                <th >Photo</th>
                                <th >Username</th>
                                <th >Email</th>                        
                                <th>Age</th>                        
                                <th>Location</th>                        
                                <th>Contact No</th>                        
                                <th>Privilege</th>                        
                                <th>Joining Date</th> 
                                <th>Status</th>                        
                            </tr>


                     </thead>                    
                     <tbody>

                        <tr>
                            <td colspan="10" class="dataTables_empty">Loading data from server</td>
                        </tr>
                </tbody>
                <tfoot>
                    <tr>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th></th>
                        <th>All Privilege</th>
                        <th></th>
                        <th></th>
                    </tr>
                </tfoot>


            </table>

在 datatabledb.php 页面中,

...
   while ( $aRow = mysql_fetch_array( $rResult ) )
    {
        $row = array();
        if($aRow['admin_name'] != ''){
            $row[] = wordwrap($aRow['admin_name'],15,"<br />\n",TRUE);
        }
        if($aRow['admin_photo'] == ''){
                $row[] = "<img src='http://fgtpl.com/fugenx1/public_html/alertR/admins/upload/no-pic.jpg' width='50' height='50'>";
        }
        else if($aRow['admin_photo'] != ''){
            if(!file_exists("http://fgtpl.com/fugenx1/public_html/alertR/admins/upload/".$aRow['admin_photo'])){$row[] = "<img src='http://fgtpl.com/fugenx1/public_html/alertR/admins/upload/no-pic.jpg' width='50' height='50'>";}
            elseif(file_exists("http://fgtpl.com/fugenx1/public_html/alertR/admins/upload/".$aRow['admin_photo'])){$row[] = "<img src='http://fgtpl.com/fugenx1/public_html/alertR/admins/upload/".$aRow['admin_photo']."' width='50' height='50'>";}
        }
        if($aRow['username'] != ''){
            $row[] = $aRow['username'];
        }
        if($aRow['email'] != ''){
            $row[] = wordwrap($aRow['email'],15,"<br />\n",TRUE);
        }
        if($aRow['email'] == ''){
            $row[] = "N/A";
        }
        if($aRow['age'] != ''){
            $row[] = $aRow['age'];
        }
        if($aRow['age'] == ''){
            $row[] = 0;
        }
        if($aRow['location'] != ''){
            $row[] = $aRow['location'];
        }
        if($aRow['location'] == ''){
            $row[] = "N/A";
        }
        if($aRow['contact_no'] != ''){
            $row[] = $aRow['contact_no'];
        }
        if($aRow['contact_no'] == ''){
            $row[] = "N/A";
        } 
        if($aRow['role'] != ''){
            $row[] = get_role_name_by_id($aRow['role']);
        }
        if($aRow['creation_date'] != ''){
            $joiningDate = date("d-m-Y h:i:s", strtotime($aRow['creation_date']));
            $row[] = substr($joiningDate,0,10);
        }
        if($aRow['status'] != ''){
            $row[] = ($aRow['status'] == 1)?"Enable":"Disable";
        }





        $output['aaData'][] = $row;
    }
...

请帮我。

4

1 回答 1

0

要获得精确匹配过滤器有多种方法,但基本上您必须禁用列的智能过滤器功能。这可以通过两种方式完成:

使用fnFilter()并传递 true 作为最后一个参数

oTable.fnFilter("^"+some_string+"$", some_column_id, true);

使用 o设置

// after creating the table and getting the table object...

var oTable = $('#some_id').dataTable();

// ...you can use it to get a settings object...

var oSettings = oTable.fnSettings();

// ...then you can do things with the settings object (your col index should be 7 )

oSettings.aoPreSearchCols[ iCol ].sSearch = "^\\s*"+'1'+"\\s*$";
oSettings.aoPreSearchCols[ iCol ].bRegex = false;
oSettings.aoPreSearchCols[ iCol ].bSmart= false;

这是从这里获取的,应该可以工作

于 2012-06-15T13:23:37.850 回答