1

我已经使用 jquery-datatables-rails 为我的索引提供一些信息,并在之后添加了过滤器。但是,数据表似乎忽略了过滤器,并且它们没有显示为获取请求中请求的 json 的一部分。

我已经附上了我的索引方法、数据表类、我的索引视图和我正在运行的咖啡脚本。

指数法

  def index
    @sbcons = Subcontractor.scoped
    if filters = params[:filter]
      @sbcons = @sbcons.where(sbcon_type: filters[:type]) unless filters[:type].blank?
      @sbcons = @sbcons.where(cscs_card: filters[:cscs]) unless filters[:cscs].blank?
      @sbcons = @sbcons.where(approved_status: filters[:approved]) unless filters[:approved].blank?
    end
    respond_to do |format|
      format.html
      format.json do
        render json: SubcontractorsDatatable.new(view_context, @sbcons)
      end
    end
  end

索引视图

<% provide(:title, 'All Subcontractors') %>
<h1>Subcontractors List</h1>
<div class="filter">
  <%= form_tag(method: :get, id: "filter_form") do %>
    <%= label_tag :sbcon_type, "Type" %>
    <%= select_tag "filter[type]", options_for_select([[],["Labour Only"], ["Specialist"], ["Both"]]) %>
    <%= label_tag :cscs_card, "CSCS" %>
    <%= select_tag "filter[cscs]", options_for_select([[],["Yes"], ["No"]]) %>
    <%= label_tag :approved_status, "Approved Status" %>
    <%= select_tag "filter[approved]", options_for_select([[],["Approved"], ["Not Approved"]]) %> <br>
    <%= link_to "Filter", '#', id: "filterbutton", class: "btn btn-mini" %>
  <% end %>
  <br>
</div>
<table id="subcontractors" class="table table-condensed table-hover display" data-source="<%= subcontractors_url(format: "json") %>">
  <thead>
    <tr>
      <th>Name</th>
      <th>Contact Number</th>
      <th>CSCS</th>
      <th>Type</th>
      <th>Scotland</th>
      <th>NE England</th>
      <th>NW England</th>
      <th>Midlands</th>
      <th>SE England</th>
      <th>SW England</th>
      <th>London</th>
      <th>Wales</th>
      <th>Operatives</th>
      <th>Product Liability</th>
      <th>Employer Liability</th>
      <th>Public Liability</th>
      <th>Contractors All Risk</th>
      <th>Status</th>
    </tr>
  </thead>
  <tbody>
  </tbody>
</table>

<%= javascript_tag do %> 
  $('#filterbutton').click(function (){
    $('#subcontractors').dataTable().fnDraw();
  });
<% end %>

数据表类

class SubcontractorsDatatable
  delegate :params, :h, :link_to, to: :@view

  def initialize(view, sbcons)
    @view = view
    @sbcons = sbcons
  end

  def as_json(option = {})
    {
      sEcho: params[:sEcho].to_i,
      iTotalRecords: Subcontractor.count,
      iTotalDisplayRecords: subcontractors.total_entries,
      aaData: data
    }
  end

private
  def data
    subcontractors.map do |subcontractor|
      [
        link_to(subcontractor.sbcon_name, subcontractor),
        h(subcontractor.con_tel_num),
        h(subcontractor.cscs_card),
        h(subcontractor.sbcon_type),
        h(subcontractor.scot),
        h(subcontractor.ne_eng),
        h(subcontractor.nw_eng),
        h(subcontractor.mid),
        h(subcontractor.se_eng),
        h(subcontractor.sw_eng),
        h(subcontractor.ldn),
        h(subcontractor.wales),
        h(subcontractor.op_avail),
        h(subcontractor.ins_prod),
        h(subcontractor.ins_emp),
        h(subcontractor.ins_pub),
        h(subcontractor.ins_con_all),
        h(subcontractor.approved_status)
      ]
    end
  end

  def subcontractors
    @subcontractors ||= fetch_subcontractors
  end

  def fetch_subcontractors
    subcontractors = Subcontractor.order("#{sort_column} #{sort_direction}")
    subcontractors = subcontractors.page(page).per_page(per_page)
    if params[:sSearch].present?
      subcontractors = subcontractors.where("sbcon_name like :search or con_tel_num like :search", search: "%#{params[:sSearch]}%")
    end
    subcontractors
  end

  def page
    params[:iDisplayStart].to_i/per_page + 1
  end

  def per_page
    params[:iDisplayLength].to_i > 0 ? params[:iDisplayLength].to_i : 10
  end

  def sort_column
    columns = %w[sbcon_name con_tel_num cscs_card sbcon_type scot ne_eng nw_eng mid se_eng sw_eng lon wales op_avail ins_prod ins_emp ins_pub ins_con_all approved_status]
    columns[params[:iSortCol_0].to_i]
  end

  def sort_direction
    params[:sSortDir_0] == "desc" ? "desc" : "asc"
  end
end

咖啡脚本

jQuery ->
        $('#subcontractors').dataTable
          sPaginationType: "bootstrap"
          sdom: "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>"
          bProcessing: true
          bServerSide: true
          sAjaxSource: $('#subcontractors').data('source')
          fnServerParams: (aoData) ->
              for form_element in $('#filter_form').serializeArray()
                aoData.push
                  name: form_element.name
                  value: form_element.value
4

1 回答 1

1

在艾布拉姆的建议下,我查看了过滤器是否部分并发现它从未被调用过。我通过如下修改 form_tag 来解决这个问题:

<%= form_tag('#', method: :get, id: "filter_form") do %>

从这里我发现过滤器正在通过但被忽略。Abram 建议将逻辑移至 datatables 类中的 fetch_subcontractors 方法,同时更改 initalize 方法以反映这一点

def initialize(view, subcontractors)
    @view = view
  end
def fetch_subcontractors
    subcontractors = Subcontractor.order("#{sort_column} #{sort_direction}")
    subcontractors = subcontractors.page(page).per_page(per_page)
    if params[:sSearch].present?
      subcontractors = subcontractors.where("sbcon_name like :search or con_tel_num like :search", search: "%#{params[:sSearch]}%")
    end
    if filters = params[:filter]
      binding.pry
      subcontractors = subcontractors.where(sbcon_type: filters[:type]) unless filters[:type].blank?
      subcontractors = subcontractors.where(cscs_card: filters[:cscs]) unless filters[:cscs].blank?
      subcontractors = subcontractors.where(approved: filters[:approved]) unless filters[:approved].blank?
    end
    subcontractors
  end

通过这些更正,过滤器可以正常工作并显示正确的结果。

于 2013-06-07T11:18:03.970 回答