如何对jquery插件数据表进行负过滤


how to do negative filtering on jquery plugin data tables?

hi我正在使用jquery的数据表插件,我想修改我的服务器端脚本,以允许负面搜索!foo。它会显示除foo之外的所有内容。我已经写了一些代码,但它似乎不起作用,我不知道为什么。我可以定期过滤,但当我使用!foo它只是返回表中的所有内容。

$sWhere = "";
if ( $_GET['sSearch'] != "" )
{
    $aWords = preg_split('/'s+/', $_GET['sSearch']);
    $sWhere = "WHERE (";
    for ( $j=0 ; $j<count($aWords) ; $j++ ) {
        if ( $aWords[$j] != "" ) {
            if(substr($aWords[$j], 0, 1) == "!") {
                    $notString = substr($aWords[$j], 1);
                    $sWhere .= "(";
                    for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
                            $sWhere .= $aColumns[$i]." NOT LIKE '%".mysql_real_escape_string( $notString )."%' OR ";
                    }
            } else {
                $sWhere .= "(";
                for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
                        $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $aWords[$j] )."%' OR ";
                }
            }
            $sWhere = substr_replace( $sWhere, "", -3 );
            $sWhere .= ") AND ";
        }
    }
    $sWhere = substr_replace( $sWhere, "", -4 );
}

感谢

DataTables插件有一个内置的过滤器函数,可用于正则表达式。如果你可以使用它,你可以尝试一个否定的前瞻性断言。

^((?!foo).)*$

看看这里的例子,看看这是否是您所需要的:http://datatables.net/release-datatables/examples/api/regex.html

您粘贴的所有代码都是用PHP编写的,所以它根本不使用DataTables框架进行搜索,我假设您启用了服务器端处理。

为了使用SQL NOT LIKE从服务器端进行负过滤,可以像您尝试过的那样,但所有的正过滤和负过滤代码都在同一个摘要循环中处理。我会添加另一个类似$_GET['sSearch_type']$_GET,以在一个完全独立的循环中处理负滤波器。只要您可以在PHPMyAdmin(或您使用的任何SQL管理器)中重现查询,就不会有任何问题,使用单独的$_GET在单独的循环中设置查询以控制搜索类型。

对于那些想要实际使用DataTables进行筛选并需要负筛选表达式的人:@Nate使用regex表达式是正确的,但没有讨论实现。

我知道有两种API方法可以以这种方式使用regex。fnFilter()search()

fnFilter()已弃用,您仍然可以使用它,但最好使用search() API函数,该函数是为取代fnFilter()而编写的。

文件:

  • fnFilter()-https://legacy.datatables.net/ref#fnFilter
  • search()-https://datatables.net/reference/api/search()

示例:

// case insensitive filter a column for all
// values that don't match 'closed'
column.search( '^(?!closed).*$', true, false, true ).draw();

示例实现:

/**
 * This example creates a select box filter which 
 * appends to the thead cell of the 3rd column 
 * from the left (i.e. column at index 2). The values
 * of the select <option></option> are generated
 * automatically based on the column's real values
 * except for the 2 top most filters. The first is "All"
 * which passes and empty string to the regex filter, 
 * and the second top most filter is "All Open Projects"
 * which triggers the negative regex filter to remove 
 * all rows where the column value is set to "Closed" 
 * or "closed"
 */
$(document).ready(function() {
    $('#report-table').DataTable({
        initComplete: function () {
            this.api().columns().every( function () {
                var column_index = this[0][0],
                    column = this,
                    not_closed = 'not_closed',
                    select = $('<select id="status_col_filter" style="margin-left:10px;"><option value="">All</option><option value="'+ not_closed +'">All Open Projects</option></select>');
                if (column_index === 2) {
                    select
                        .appendTo( $(column.header()) )
                        .on( 'change', function () {
                            var val = $.fn.dataTable.util.escapeRegex( $(this).val() );
                            if (val === not_closed) {
                                // negative regex for all open status projects
                                column.search( '^(?!closed).*$', true, false, true ).draw();
                            } else {
                                // regular positive regex - filter by column value
                                column.search( val ? '^'+val+'$' : '', true, false ).draw();
                            }
                        });
                    column.data().unique().sort().each( function ( d, j ) {
                        select.append( '<option value="'+d+'">'+d+'</option>' )
                    });
                }
            });
        }
    });
});