如何在服务器端jQuery数据表中选择特定行?

时间:2012-02-27 21:37:38

标签: javascript jquery datatables

我在php中使用jQuery Data Tables服务器端示例来获取表中的记录,然后按照此示例中的说明显示它们:

http://www.datatables.net/release-datatables/examples/data_sources/server_side.html

我的问题是:如何根据我的sql中的WHERE子句仅获取特定记录?

请注意,上面的示例获取指定表中的所有记录,允许我们选择列...我们如何做同样的事情,但只选择符合某个标准的某些行...即。在示例中,在加载数据表时默认仅显示Firefox浏览器。以下是来自php脚本的代码,通过jquery调用来填充数据表...我实际上是在尝试修改此脚本以仅获取特定的行,即行在哪里浏览器如'Firefox%'......

 * Filtering
 * NOTE this does not match the built-in DataTables filtering which does it
 * word by word on any field. It's possible to do here, but concerned about efficiency
 * on very large tables, and MySQL's regex functionality is very limited
 */
$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 .= ')';
}

/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
    if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
    {
        if ( $sWhere == "" )
        {
            $sWhere = "WHERE ";
        }
        else
        {
            $sWhere .= " AND ";
        }
        $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
    }
}


/*
 * SQL queries
 * Get data to display
 */
$sQuery = "
    SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
    FROM   $sTable
    $sWhere
    $sOrder
    $sLimit
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());

/* Data set length after filtering */
$sQuery = "
    SELECT FOUND_ROWS()
";
$rResultFilterTotal = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
$aResultFilterTotal = mysql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];

3 个答案:

答案 0 :(得分:0)

如果您按照PHP中的CRUD示例,$ _GET键匹配数据表的选项设置键,例如“sSearch”用于您的位置,“iDisplayStart”和“iDisplayLength”用于限制等

答案 1 :(得分:0)

我使用.NET并且不太熟悉php,因此我无法根据您的需求进行完全定制,但我会告诉您如何接近可以自己完成其余部分的工作。

fnServerData事件将允许您发布自己的参数以进行搜索 - 或者更具体地说是aoData。使用aoData.push()方法,您可以指定参数的名称及其值。在这个例子中,我将把所选日期发送到我的查询,我将该变量称为“日期”。

"fnServerData": function (sSource, aoData, fnCallback) {
                /* Add some extra data to the sender */
                aoData.push({ "name": "date", "value": $('#datepicker').val() });
                $.getJSON(sSource, aoData, function (json) {
                    /* Do whatever additional processing you want on the callback, then tell DataTables */
                    fnCallback(json);
                });

然后,DataTables将POST发送到带有一堆参数的URL,例如server_processing.php?sEcho = 1&amp; iColumns = 21&amp; sColumns =&amp; iDisplayStart = 0&amp; iDisplayLength = 50 with&amp; date = 02% 2F27%2F2012结尾(今天的日期URL编码)。

从你的php页面,你应该能够使用我认为的$ _GET ['date']函数以及默认发送的其他键来拉动该参数。您需要将查询结果作为JSON对象返回,正如您在链接页面底部所看到的那样。

答案 2 :(得分:0)

我通过在服务器端php脚本中修改我的sql找到了一个解决方案,它只是通过相应地修改$ sWhere变量来获取数据。在这种情况下,我从我的jquery脚本传递一个GET变量$ userid:

          $userid = $_GET['userid'];
    /* 
 * Filtering
 * NOTE this does not match the built-in DataTables filtering which does it
 * word by word on any field. It's possible to do here, but concerned about efficiency
 * on very large tables, and MySQL's regex functionality is very limited
 */

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