在 ruby 应用程序中使用 JQuery 服务器端数据表,并且分页在第一页之后显示空白页

Using JQuery Server side datatables in ruby app and pagination shows blank page after 1st page

提问人:NothingToSeeHere 提问时间:4/13/2018 最后编辑:safinehNothingToSeeHere 更新时间:11/21/2022 访问量:1222

问:

我正在使用 Jquery Server 端数据表,但无法让分页在页面 2+ 上工作。

我有 200 条记录。第一页显示 50。当我单击页面底部的分页编号时,它会导航到第 2 页,但第 2 页是空的。

我在同一个应用程序(但另一个表)中使用非服务器端数据表,它工作正常。

此外,选择要每页显示 #of 条记录的选择菜单不起作用,并且列排序不起作用。

搜索正在工作

我正在使用: "1.12.4" 轨道 5 数据表 1.10.16

index.json.jbuilder

json.draw params[:draw]
json.recordsTotal @cooks.length
json.recordsFiltered @cooks.length
json.data do
  json.array! @paginated do |cook|
    json.DT_RowClass "js-cook-row js-cook-row-#{donor.id}"
    if browser.device.mobile?
      json.partial! 'cooks/mobile_data', cook: cook, project: @project
    else
      json.partial! 'cooks/desktop_data', cook: cook, project: @project
    end
  end
end

带表格的页面(超薄)

table.table.table-striped.table-bordered.table-hover.js-cook-table.cooks-table.dataTables-example.small-font.kitchen-table id="#{table_id}" data-toggle="table" width="100%"
  thead
    tr
      - if source == RouteSourceConsts::WIZARD
        th.updated-th data-field="cook-checkbox"
      th.cook-category data-field="given" data-sortable="true" = t('cooks.table_title.given')
      th.cook-vendor data-field="cook_name" data-sortable="true" = t('cooks.table_title.name')
      th data-field="recipes" = t('cooks.table_title.recipes')
      th data-field="helper" data-sortable="true" = t('cooks.table_title.helper')
      th.status-info-th data-field="status" data-sortable="true" = t('cooks.table_title.status')
      - if source == RouteSourceConsts::WIZARD
        th data-field="contact_info" data-sortable="false" Info
        th.updated-th  data-field="updated" data-sortable="true" = t('cooks.table_title.updated')

  tbody.js-assignable-table-body

  javascript:
  $(document).ready(function () {
    var default_columns = [
      {data: "given", className: "cook-value-received"},
      {data: "cook_name", className: "cook-name"},
      {data: "recipes", className: "recipes-list", orderable: false},
      {data: "helper", className: "assignment-info.doer-info", orderable: true},
      {data: "status", className: "cook-status"}
    ];

    if (#{source == RouteSourceConsts::WIZARD}) {
      var columns = default_columns.concat([ {data: "contact_info", className: "cook-info"}, {data: "updated", className: "updated-at"}]);
      columns.unshift({data: "cook_checkbox"})
    } else {
      var columns = default_columns
    }
    if (#{source == RouteSourceConsts::WIZARD}) {
      var custom_options = {
        lengthMenu: [50, 100, 150],
      };
    } else {
      var custom_options = {lengthMenu: [50, 100, 150]};
    }
    var default_table_options = {
      autoWidth: false,
      serverSide: true,
      searching: true,
      searchDelay: 1000,
      paging: true,
      responsive: true,
      dom: '<"pull-left"l><"pull-right"f>t<"bottom"p><"clear">',
      pagingType: "full_numbers",
      language: {emptyTable: '', zeroRecords: ''},
      ajax: {
        url: "#{project_cooks_path(project, source: source, type: type)}",
        type: 'GET',
        data: function (data) {
          data.type_filter = $('select.js-cook-type-filter').val()
        }
      },
      drawCallback: function (_settings) {
        initializecookStatusSelect();
        var pagination = $(this).closest('.js-donation-tabs, .js-cooks-main-container').find('.dataTables_paginate');
        pagination.toggle(this.api().page.info().pages > 1);
      },
      headerCallback: function (_thead, _data, _start, _end, _display) {
        cells = $('##{table_id} th');
        $(cells[0]).removeClass('cook-value-received');
        $(cells[1]).removeClass('cook-name');
        $(cells[2]).removeClass('recipes-list');
        $(cells[3]).removeClass('assignment-info.doer-info');
        $(cells[4]).removeClass('cook-status');
        $(cells[5]).removeClass('cook-info');
        $(cells[6]).removeClass('updated-at');
      },
      columns: columns,
      order: []
    };
    $("##{table_id}").dataTable($.extend({}, default_table_options, custom_options));

    channel = pusher.subscribe('cooks-#{project.id}');
    channel.bind('update', function (_data) {
      $('##{table_id}').DataTable().rows().invalidate('data').draw(false);
    });

    channel = pusher.subscribe('cooks-#{project.id}');
    channel.bind('imported', function (_data) {
      $('##{table_id}').DataTable().rows().invalidate('data').draw(false);
    });
  });
jQuery 数据表 ruby-on-rails-5 服务器端

评论

0赞 colin0117 4/18/2018
这可能与阿贾克斯的回应有关。你能在这里发布它吗,请。看看这个页面,datatables.net/examples/server_side/simple.html,转到 Ajax 选项卡,这是预期的响应格式。
3赞 colin0117 4/18/2018
这个将是一个更好的例子:datatables.net/examples/server_side/object_data.html - 因为它使用与您相同的对象。
0赞 NothingToSeeHere 4/20/2018
{“draw”:“3”,“recordsTotal”:200,“recordsFiltered”:200,“data”:[]} 这是第 3 页上的调用@colin0117
0赞 NothingToSeeHere 4/20/2018
我最近从 rails 4.2 升级到 rails 5.1
0赞 colin0117 4/21/2018
你的数据真的是一个空数组吗?如果是这样,那就意味着没有什么可显示的。draw:3

答: 暂无答案