【问题标题】:Datatables load records per page in real time数据表实时加载每页记录
【发布时间】:2020-03-02 05:15:06
【问题描述】:

我有插件数据表 1.10,我正在尝试减少加载时间,我目前有超过 1000 条记录必须显示在表中,并且我已将其配置为分页。

问题是,当您加载页面时,您所做的是加载所有记录,一旦加载,它就会显示所有内容和页面,因为每页显示 6 条记录。

我想要或想要的是,当我加载页面时,我只需要加载当前页面的记录,如果我必须加载下一页,那么我会再次加载页面的记录。

我尝试使用“serverSide: true”,但它所做的是在不分页的情况下全部加载它们。

这实际上是我的代码:

var showcontent = baseurl+"transaction/showcontent";
var table = $('#datatablecontent').DataTable({
"responsive": true,
"processing": true,
    "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "Todos"]],
    "ajax": {
        "url": showcontent,
        "type": "POST"
},        
"autoWidth": false,
'columnDefs': [
  { 'width': 10, 'targets': 1 },
  { 'width': 5, 'targets': 2 },
  { 'width': 5, 'targets': 3 },
  { 'width': 5, 'targets': 4 },
  { 'width': 55, 'targets': 5 },
  { 'width': 30, 'targets': 6 },
  { 'width': 20, 'targets': 7 },
  { 'width': 20, 'targets': 8 , 'visible': false},
  { 'width': 40, 'targets': 9 },
  { 'width': 20, 'targets': 10 , 'visible': false},
  { 'width': 20, 'targets': 11 , 'visible': false},
  { 'width': 20, 'targets': 12 , 'visible': false},
  { 'width': 20, 'targets': 13 },
  { 'width': 20, 'targets': 14 },
  { 'width': 10, 'targets': 15 },
  { 'width': 20, 'targets': 16 , 'visible': false},
  { 'width': 10, 'targets': 17 },
  { 'width': 10, 'targets': 18 },
  { 'width': 10, 'targets': 19 },
  { 'width': 10, 'targets': 20 , 'visible': false},
  { 'width': 10, 'targets': 21 , 'visible': false},
  { 'width': 50, 'targets': 22 },
  { 'targets': 0,'searchable':false,'orderable':false,}
  //{ 'targets': 1,'searchable':false,'orderable':false,}
],
"fixedColumns": true,
"iDisplayLength": 5,
"order": [[ 2, "desc" ]]
});

File php/codeigniter: transaction(controller)
public function showcontent(){
...
//Here before is the code from send data json
$output = array(
              "data" => $data,
  );
echo json_encode($output);
}

【问题讨论】:

    标签: jquery datatables pagination


    【解决方案1】:

    除了使用serverSide: true 启用服务器端处理模式之外,您还需要使用对结果进行分页的服务器端脚本。

    使用 PHP,您可以使用 ssp.class.php,当您使用 download DataTables library 时,它在 /examples/server_side/scripts 文件夹中可用。

    /examples/server_side/scripts 文件夹中还有一个示例脚本 server_processing.php,说明了如何使用它。

    【讨论】:

    • 感谢您的帮助,但正如我在第一条消息中所说,我已经尝试输入“serverSide:true”,它一次显示所有内容并且不显示页面。跨度>
    • @Sylar,请重新阅读我的回答。除了使用 serverSide: true 之外,您还需要使用 PHP 在服务器端对结果进行分页。目前,您正在使用echo json_encode($output); 打印出所有数据。
    • 是的,我明白了,我已经下载了你评论的文件,但是我没有看到代码在哪里组装分页。奇怪的是,如果我不选择“serverSide:true”,分页确实会出现。在这个例子中,当滚动条移动时加载内容,我希望我做一些非常相似但有分页的事情,并在每个页面上单击时加载服务器的内容:datatables.net/extensions/scroller/examples/initialisation/…
    猜你喜欢
    • 2020-02-24
    • 2011-09-25
    • 2020-06-17
    • 1970-01-01
    • 2019-03-13
    • 2018-12-04
    • 1970-01-01
    • 2018-03-18
    • 2014-05-28
    相关资源
    最近更新 更多