0

我有一个代码:

function prepareListCustomer() {
    var req;
    req = new AjaxAdapter;
    req.dataType = 'json';
    return req.query('GET', LIST_CUSTOMER_URL, {rowsOnPage: k, page: l}, function(responseServer, status, xhr) {
        listCustomer = responseServer.dataListCustomer;
        l = l + 1;
      }, function(jqXHR, textStatus, errorThrown) {
          var exception;
          exception = jQuery.parseJSON(jqXHR.responseText);
          return showError(exception);
        });
}

function prepareDataTable() {
    $('#displayData').dataTable( {
        "bProcessing": true,
        "bServerSide": true,
        "sAjaxSource": LIST_CUSTOMER_URL
        "fnServerData": function ( sSource, aoData, fnCallback, oSettings ) {
          oSettings.jqXHR = $.ajax( {
            "dataType": 'json',
            "type": "GET",
            "url": LIST_CUSTOMER_URL,
            "data": {rows: k, pages: l},
            "success": prepareListCustomer
          } );
        }
      } );
}

函数 prepareListCustomer() 将数据写入 listCustomre。我想在prepareDataTable中显示这些数据,如何?我想使用服务器端处理。在 listCustomer 我有 JSON 像:

{
    "rowsPerPage": 10,
    "page": 1,
    "total": 100,
    "rows": [
        {
            "id": 1,
            "name": "nazwa1"
        },
        {
            "id": 2,
            "name": "nazwa2"
        },
        {
            "id": 3,
            "name": "nazwa3"
        }
    ]
}

我正在阅读http://datatables.net/examples/data_sources/server_side.html但我不知道如何实现我的代码?

4

1 回答 1

1

您的 JSON 看起来不像默认值。

如果您使用该 JSON,则需要在 sAjaxSource 属性之后像这样在 javascript 调用中声明每一列。

"aoColumns": [
    { "mDataProp": "DBColumn1" , "bSortable":true, "bVisible": false },
    { "mDataProp": "DBColumn2" , "bSortable":true, "bVisible": false },
    { "mDataProp": "DBColumn3" , "bSortable":true, "bVisible": false }
]

这部分 JSON 也不正确

"rowsPerPage": 10,
"page": 1,
"total": 100,
"rows":

它应该看起来像这样

"sEcho": 1,
"iTotalRecords": 100,
"iTotalDisplayRecords": 10,
"aaData":

如果您查看您发布的链接上的 PHP 示例,它会在此处生成此标题并将它们放入关联数组中。

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

然后开始将所有数据加载到该数组中,然后再编码为 JSON

while ( $aRow = mysql_fetch_array( $rResult ) )
{
    $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 );

你的 JSON 应该是这样的

{
   "sEcho":8,
   "iTotalRecords":1,
   "iTotalDisplayRecords":1,
   "aaData":[
      {
         "Entity_Id":840982,
         "External_Id":"1",
         "Family_Name":"A",
         "First_Name":"A",
         "Prefix_Title":null,
         "Suffix_Title":null
      }
   ]
}

DataTables.net上有完整的解释

于 2013-05-09T10:51:25.907 回答