【发布时间】:2013-06-03 12:17:27
【问题描述】:
我已使用 jquery-datatables-rails 为我的索引提供一些信息,并在 this 之后添加了过滤器。但是,数据表似乎忽略了过滤器,并且它们没有显示为 get 请求中请求的 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
【问题讨论】:
-
嘿 Ryan,你是在使用 pry 来调试这个吗?
-
不,我什至不确定从哪里开始寻找,我无法弄清楚它为什么会跳过过滤步骤。
-
好吧,在这种情况下,我通常会将 binding.pry 扔到各个地方,看看它没有被触发的地方
-
签出github.com/nixme/pry-debugger ...添加gem“pry”和gem“pry-debugger”..然后捆绑..然后重新启动服务器..然后在代码中的任何位置添加binding.pry以及何时服务器点击 binding.pry 它将停止,您可以与环境中的 irb 控制台交互...尝试在您希望调用它的地方添加它,您可能会发现您的问题。重新加载页面并触发 binding.pry 后,不要忘记检查服务器控制台是否有 pry irb
-
例如,您可以测试 if filters = params[:filter] 是否为真,方法是在 if filters = params[:filter] 下方放置 'binding.pry' 并查看您的页面加载是否挂起。如果是,则已排序.. 检查您的服务器控制台.. 如果不是.. 好吧,如果 filters = params[:filter] 为假
标签: javascript ruby-on-rails ruby-on-rails-3 coffeescript