【问题标题】:Enable pagination with deferLoading使用 deferLoading 启用分页
【发布时间】:2015-06-25 20:21:01
【问题描述】:

我在 Rails 应用程序中工作,并在 jQuery DataTables 上使用 deferLoading: true 选项,以便将第一个 DataTables 的加载传递给 Rails 控制器。

我让数据表按我想要的方式加载,在控制器中加载初始表可以消除初始 html 加载时的 Ajax 延迟,但是,数据表信息部分不会显示分页结果。

代码和图片如下所示。

除了表格底部的分页之外,一切都正常工作,我只是无法让它应用与 Ajax 调用数据表相同的细节。对此问题的任何想法或方向将不胜感激!

index.html.erb:

<div class="row">
  <div class="col-xs-12 table-wrapper">
    <div class="inner-wrapper">
      <p class="quick-app">
        <a class="custom-btn accent-inverse-btn add-user" href="<%=  calculator_path%>">Quick Application</a>
      </p>
      <table class="table table-striped table-scroll cms-table-width dataTable" id="customer_deals_datatable" data-source="<%= dealer_customer_deals_url(:include_archived => params[:include_archived].present?)  %>" >
        <div>
          <thead>
            <tr>
              <th>ID/Calculator</th>
              <th>Applicant/Co-Applicant</th>
              <th>Year</th>
              <th>Model</th>
              <th>App Status</th>
              <th>Tier Number</th>
              <th>Docs Status</th>
              <th>Submitted On</th>
              <th>Days Remaining</th>
              <th>Chrome Decision</th>
              <th>Updated At</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody>
          <% @datatable.data.each do |datum| %>
            <tr>
              <% datum[0] = datum[0].join('') %>
              <%= (datum.map {|content| "<td>#{content}</td>"}.join('')).html_safe %>
            </tr>
          <% end %>
          </tbody>
        </div>
      </table>
    </div>
  </div> <!--   </div>#content -->
</div>

controller

def index
  respond_to do |format|
    format.html do
      params.merge!({"iDisplayLength"=>"10","iSortCol_0"=>"10","sSortDir_0"=>"desc"})
      @datatable = CustomerDeals::CustomerDealsDataTable.new(view_context, @dealer)
    end
    format.json { render json: CustomerDeals::CustomerDealsDataTable.new(view_context, @dealer) }
  end
end

这是项目中数据表类的部分代码:

module CustomerDeals
  class CustomerDealsDataTable

    def fetch_deal_searches
      return @deal_searches if @deal_searches.present?

      deal_searches = CustomerDeals::CustomerDealSearch.where(dealership_id: @dealer )

      if is_submitted_on_sort?
        deal_searches = deal_searches.where('deal_dated_calculator_value != ?', 'calculator')
      end
      if params[:sSearch].present?
        deal_searches = deal_searches.containing(params[:sSearch])
      end
      deal_searches = deal_searches.order(order_query)

      @deal_searches = deal_searches
    end

    def is_submitted_on_sort? 
      SORT_COLUMNS[params[:iSortCol_0].to_i] == 'deal_submitted_on'
    end

    def lookup_sort_column
      SORT_COLUMNS[params[:iSortCol_0].to_i]
    end

    def order_query
      "#{lookup_sort_column} #{params[:sSortDir_0]}"
    end

    def paged_deal_searches
      fetch_deal_searches.page(current_page_number).per(params[:iDisplayLength])
    end

    def current_page_number
      params[:iDisplayLength].to_i == 0 ? 1 : params[:iDisplayStart].to_i/params[:iDisplayLength].to_i + 1
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails ajax datatables


    【解决方案1】:

    你在正确的轨道上,deferLoading也可以分配整数或两个整数的数组来指定表中有多少条记录用于分页上班。

    来自manual:

    deferLoading用于表示需要延迟加载,但也用于告诉DataTables全表有多少条记录(让信息元素和分页正确显示)。

    在初始加载时对表应用过滤的情况下,这可以通过将参数作为数组来表示,其中第一个元素是过滤后可用的记录数,第二个元素是没有过滤的记录(允许正确显示表格信息元素)。

    示例:

    表中有 57 条记录,未应用过滤:

    $('#example').dataTable( {
      "serverSide": true,
      "ajax": "scripts/server_processing.php",
      "deferLoading": 57
    } );
    

    过滤后 57 条记录,未过滤 100 条记录(应用了初始过滤器):

    $('#example').dataTable( {
      "serverSide": true,
      "ajax": "scripts/server_processing.php",
      "deferLoading": [ 57, 100 ],
      "search": {
        "search": "my_filter"
      }
    } );
    

    【讨论】:

    • 感谢 Gyrocode,我们添加了类似 'deferLoading': $('#customer_deals_datatable').data('length') ] 的内容,它似乎解决了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-09
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 2013-04-20
    • 1970-01-01
    相关资源
    最近更新 更多