【问题标题】:Undefined index: sSearch jQuery Datatables Laravel 5.1未定义索引:sSearch jQuery 数据表 Laravel 5.1
【发布时间】:2015-12-30 11:18:38
【问题描述】:

编辑正如一些答案所暗示的,我知道发生此错误是因为变量不存在且未传递。问题是如何为 Datatables 配置它,我相信这些变量应该来自我的 Ajax 代码作为变量,但是我该如何设置它或者代码中的正确格式是什么?

我正在使用 Postgre SQL 开发 jQuery Datatables 库并关注此链接。

https://datatables.net/development/server-side/php_postgres

我遵循的是正确的,我相信数据库连接是好的。

但我收到此错误:

Undefined index: sSearch

我正在使用 Laravel 5.1,我有一个视图,我通过 Ajax 获取数据,然后将其填充到 jQuery 数据表中。我用的是服务端方法:我的代码:

public function apiGetCustomers()
{

    $aColumns = array( 'id', 'firstname', 'lastname', 'gender', 'phone_num', 'country', 'postcode' );

    $sIndexColumn = "id";

    $sTable = "customers";

    $gaSql['user']       = "postgres";
    $gaSql['password']   = "postgres";
    $gaSql['db']         = "qms";
    $gaSql['server']     = "localhost";

    $gaSql['link'] = pg_connect(
        " host=".$gaSql['server'].
        " dbname=".$gaSql['db'].
        " user=".$gaSql['user'].
        " password=".$gaSql['password']
    ) or die('Could not connect: ' . pg_last_error());

    $sLimit = "";
    if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
    {
        $sLimit = "LIMIT ".intval( $_GET['iDisplayLength'] )." OFFSET ".
            intval( $_GET['iDisplayStart'] );
    }

    if ( isset( $_GET['iSortCol_0'] ) )
    {
        $sOrder = "ORDER BY  ";
        for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
        {
            if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
            {
                $sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
                    ".($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc').", ";
            }
        }

        $sOrder = substr_replace( $sOrder, "", -2 );
        if ( $sOrder == "ORDER BY" )
        {
            $sOrder = "";
        }
    }

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

    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]." ILIKE '%".pg_escape_string($_GET['sSearch_'.$i])."%' ";
        }
    }

    $sQuery = "
    SELECT ".str_replace(" , ", " ", implode(", ", $aColumns))."
    FROM   $sTable
    $sWhere
    $sOrder
    $sLimit
    ";
    $rResult = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());

    $sQuery = "
        SELECT $sIndexColumn
        FROM   $sTable
    ";
    $rResultTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
    $iTotal = pg_num_rows($rResultTotal);
    pg_free_result( $rResultTotal );

    if ( $sWhere != "" )
    {
        $sQuery = "
            SELECT $sIndexColumn
            FROM   $sTable
            $sWhere
        ";
        $rResultFilterTotal = pg_query( $gaSql['link'], $sQuery ) or die(pg_last_error());
        $iFilteredTotal = pg_num_rows($rResultFilterTotal);
        pg_free_result( $rResultFilterTotal );
    }
    else
    {
        $iFilteredTotal = $iTotal;
    }

    $output = array(
        "sEcho" => intval($_GET['sEcho']),
        "iTotalRecords" => $iTotal,
        "iTotalDisplayRecords" => $iFilteredTotal,
        "aaData" => array()
    );

    while ( $aRow = pg_fetch_array($rResult, null, PGSQL_ASSOC) )
    {
        $row = array();
        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
            if ( $aColumns[$i] == "version" )
            {
                /* Special output formatting for 'version' column */
                $row[] = ($aRow[ $aColumns[$i] ]=="0") ? '-' : $aRow[ $aColumns[$i] ];
            }
            else if ( $aColumns[$i] != ' ' )
            {
                /* General output */
                $row[] = $aRow[ $aColumns[$i] ];
            }
        }
        $output['aaData'][] = $row;
    }

     echo json_encode( $output );

    // Free resultset
    pg_free_result( $rResult );

    // Closing connection
    pg_close( $gaSql['link'] );

}

我眼中的阿贾克斯

$(document).ready(function() {
    $('#CustomerList').DataTable( {
        "processing": true,
        "serverSide": true,
        "ajax": "api/customer/all",
        "paging" : true,
        //"scrollY" : 400,
        "searching" : true,
        "ordering" :  true,
        //"pagingType" : "full_numbers" 
    } );
});

【问题讨论】:

标签: php jquery laravel datatables


【解决方案1】:

在 php.ini 中将 display_errors 设置为 Off 对我有帮助。我也建议尝试一下。

【讨论】:

  • 为什么我的答案被降级了?这个修复对我有用!
【解决方案2】:

您有一个 1.9.x 服务器端脚本,但使用的是 1.10.x 命名约定和选项布局。当你定义了

serverSide: true,
ajax: "api/customer/all",

等,您还指示 dataTables 以较新的格式将参数发送到服务器,即 search 而不是 sSearch 等等。解决此问题的最简单方法是将 dataTables AJAX 处理强制转换为 legacy mode

$.fn.dataTable.ext.legacy.ajax = true;

【讨论】:

  • 感谢您指出。但是 $.fn.dataTable.ext.legacy.ajax = true;会进入我的代码吗?
  • @JoeneFloresca - 作为单行,初始化数据表之前,即在$('#CustomerList').DataTable( {
  • 现在工作正常。非常感谢!
【解决方案3】:

替换

if ( $_GET['sSearch'] != "" )

if (isset($_GET['sSearch']) && !empty($_GET['search']))

你尝试调用一个可能不存在的变量。

【讨论】:

  • 嗨。我知道这一点,我编辑了我的问题。谢谢
  • 我认为Datatable会将其添加到网址中,这样您就不必这样做了。
猜你喜欢
  • 1970-01-01
  • 2018-08-02
  • 2012-08-24
  • 2015-03-23
  • 2016-03-30
  • 2015-09-27
  • 2016-01-24
  • 2015-04-06
  • 1970-01-01
相关资源
最近更新 更多