【问题标题】:Server Side Filtering for JQuery Datatables plugin to mimic the built in client side filteringJQuery Datatables 插件的服务器端过滤以模仿内置的客户端过滤
【发布时间】:2011-08-11 14:52:33
【问题描述】:

DataTables 插件的客户端过滤(内置)非常好。它将根据您的搜索词在任何字段上逐字过滤您的结果。

提供的示例服务器端代码针对任何字段搜索整个字符串。我将如何修改这个

$sWhere = "";
if ($_GET['sSearch'] != "") {
    $sWhere = "WHERE (";
    for ($i = 0; $i < count($aColumns); $i++) {
        $sWhere .= $aColumns[$i] . " LIKE '%" . mysql_real_escape_string($_GET['sSearch']) . "%' OR ";
    }
    $sWhere = substr_replace($sWhere, "", -3);
    $sWhere .= ')';
}

实现我的目标?

到目前为止,我已经尝试了以下内容的再现:

$sWhere = "";
if ($_GET['sSearch'] != "") {
    $sWhere = "WHERE (";
    $words = explode(" ",$_GET['sSearch']);
    for ($i = 0; $i < count($aColumns); $i++) {
        for($j = 0; $j < count($words); $j++) {
            if($words[$j] != "") {
                $sWhere .= $aColumns[$i] . " LIKE '%" . mysql_real_escape_string($words[$j]) . "%' OR ";
            }
        }
    }
    $sWhere = substr_replace($sWhere, "", -3);
    $sWhere .= ')';
}

完全没有运气。我假设我只需要遍历搜索字符串中的所有“单词”并针对这些搜索每一列。现在我迷失了自己的逻辑。我正在搜索所有单词的所有字段,我只需要通过 AND 和 OR 将它们分组,这样如果我有下表

  |col1  |  col2  |  col3
  -----------------------
#1| aaa  | bbb    |  ccc
#2| aaa  | ddd    |  xxx
#3| hugo | aaa    |  rap

我搜索“aaa bbb”,我只返回结果 #1,而不是全部三个。 我知道这很简单..但是我一直在为此绞尽脑汁,以至于我迷失了自己。

【问题讨论】:

  • 回答了我自己的问题。见下文。谢谢。

标签: jquery filter server-side datatables


【解决方案1】:

这是我的新服务器端搜索...如果其他人想在他们的 DataTable 上实现相同的功能。

$sWhere = "";
if ($_GET['sSearch'] != "") {
    $sWhere = "WHERE (";
    $words = explode(" ",$_GET['sSearch']);
    for ($j = 0; $j < count($words); $j++) {

        if ($words[$j] != "") {
            $sWhere .= "(";
            for ($i = 0; $i < count($aColumns); $i++) {

                $sWhere .= $aColumns[$i] . " LIKE '%" . mysql_real_escape_string($words[$j]) . "%' OR ";

            }
            $sWhere = substr_replace($sWhere, "", -3);
            $sWhere .= ") AND ";
        }
    }
    $sWhere = substr_replace($sWhere, "", -4);
    $sWhere .= ')';
}

【讨论】:

    猜你喜欢
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多