2

我打算使用 tablesorter 的 Ajax 服务器端分页功能,但有几个关于它如何工作的问题。2.10.0 的文档说:

ajaxProcessing 函数已更新为只需要返回总行数,而不是返回表行数组,您可以自己构建表并返回包含这些行的 jQuery 对象

如果您自己构建表格(我假设这意味着下一组行的完整 HTML,包括单元格值),ajaxUrl 是否仍需要返回一组行的 JSON 信息,还是应该返回表格的 HTML?如果要返回JSON信息,表格如何输入到jQuery中?

ajaxProcessing 可以只返回总行数和 $rows 作为对象或数组,也可以返回标题:什么决定了这些选项中的最佳选择?$rows 对象是如何创建的?一般来说,应该如何更改代码以影响这些不同的选项?

抱歉,如果我对此有点慢,并感谢您的任何指点。

问候,

林登


在这里继续我的问题,详细介绍我感到困惑的一件事:

我的 ajaxUrl 将获得下一组作为 HTML 的行,并且分页的正常语法(即,没有表格排序器)是:

databasequery.php?offset=0&numrows=25

Offset 和 numrows 是定义为 php 调用的一部分的变量。我不清楚如何将其size={size}&page={page}与表分拣机一起使用以使其正常工作。

感谢任何提示。谢谢,

4

1 回答 1

6

The original ajaxProcessing option required that you returned:

  • total rows (required),
  • rows; within an array of arrays
  • optional header cell text

in this strict format:

// [ total_rows (number), rows (array of arrays), headers (array; optional) ]
return [
  100,  // total rows
  [
    [ "row1cell1", "row1cell2", ... "row1cellN" ],
    [ "row2cell1", "row2cell2", ... "row2cellN" ],
    ...
    [ "rowNcell1", "rowNcell2", ... "rowNcellN" ]
  ],
  [ "header1", "header2", ... "headerN" ] // optional
]

Now, the rows can be returned as a jQuery object of the rows, which the addon then adds and updates (this is just an example):

var i, j, rows = '';
for (i = 0; i < data.rows.length; i++) {
  rows += '<tr>';
  for (j = 0; j < data.rows[i].cells.length; j++) {
    rows += '<td>' + data.rows[i].cells[j] + '</td>';
  }
  rows += '</tr>';
}
// [ total rows, $rows (jQuery object; optional), headers (optional) ]
return [ data.total, $(rows), data.headers ];

Lastly, you can add the rows yourself and just return the total rows:

var i, j, rows = '';
for (i = 0; i < data.rows.length; i++) {
  rows += '<tr>';
  for (j = 0; j < data.rows[i].cells.length; j++) {
    rows += '<td>' + data.rows[i].cells[j] + '</td>';
  }
  rows += '</tr>';
}
$('#mytable').find('tbody').html( rows );
// [ total rows, headers (optional) ]
return [ data.total ];

Similar examples of the above are also included in the documentation of the ajaxProcessing option

So, to answer your questions

does the ajaxUrl still need to return JSON information for the set of rows or should it return the HTML for the table? If the JSON information is to be returned, how is the table input into the jQuery?

the ajaxUrl option doesn't need to return anything. It is merely used as a method to get the next set of data by sending the appropriate page number, filter queries and column sort directions to the server. It shouldn't be returning any HTML. The pager addon will update the table after it has been processed.

How is the $rows object created? In general how should the code be changed to effect these different options?

Hopefully the example I shared above answers that question. But as stated, the above code is just an example as the ajax returned can be in pretty much any format. It is just no longer required that you have to build the array of arrays to return to the pager addon.

what determines which is the best choice of these options?

The best choice is left to you. The pager addon was written to give you more choices as to how you want to apply the ajax data. You could actually completely bypass the pager plugin and do the ajax processing yourself, then just update the pager. That's what makes the system flexible and hopefully work in many different situations.


In answer to your continuation try using this as your ajaxUrl string:

databasequery.php?offset={page}&numrows={size}

If numrows is fixed at 25 rows, then you can remove the {size}, as I would guess your server side script would always return at most 25 rows worth of data:

databasequery.php?offset={page}
于 2013-05-28T14:41:32.863 回答