通过数据表搜索确切的单词 [英] search exact word by datatables

查看:63
本文介绍了通过数据表搜索确切的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am implementing the sorting and search functionality for my php script by datatables. For sorting its okay. For searching I have 3 text fields , 1 age range field and 1 drop down. The different values of dropdownbox are "owner","superadmin","admin" and "agent". So when I am selecting the value "admin", the list of records are showing which contains "admin" as well as "superadmin". I can understand this is showing as in the "superadmin" word "admin" word is there. So for only this drop down box, how can I restrict for exact word. My code is like:

<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页面中,

and in the datatabledb.php page,

...
   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;
    }
...

请帮助我。

推荐答案

要使用完全匹配过滤器,可以通过多种方法进行,但基本上必须禁用该列的智能过滤器功能。这可以通过两种方式完成:

To have an exact match filter there are various ways, but basically you must disable the smart filter functionaliti for the column. This can be done in two ways:

使用 fnFilter()并将true作为lastr参数传递

Using fnFilter() and passing true as the lastr parameter

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

使用设置

// 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;

这取自此处,并且应该可以正常运行

this is taken from here and should work

这篇关于通过数据表搜索确切的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆