对表格列进行排序并使用复选框过滤结果

时间:2013-02-03 19:14:14

标签: ruby-on-rails-3

我很难弄清楚如何在第一次加载页面时显示所有选中的复选框。

我设法对列进行排序和过滤,但是当第一次呈现页面时,它会显示整个表但不会选中复选框。

如何通过检查参数?

产品控制器

  def index
    @products = Product.order(sort_column + " " + sort_direction).filter(filter_selection)
    @all_ptypes = Movie.all_ptypes
    @selected_types = (params[:ptypes].present? ? params[:ptypes] : [])
  end
end

是helper_method

  def sortlist (column, title = nil, filter, css_class)
    title ||= column.titleize
    css_id = column == :sort_column ? "current" : "#{column}_header"
    direction = column == :sort_column && :sort_direction == "asc" ? "desc" : "asc"
    link_to title, {:sort => column, :direction => direction, :ptypes => filter}, {:id => css_id}
  end
  def th_class (column)
    css_class = column == params[:sort] ? "current" : nil
  end

产品类

  def self.all_ptypes
    self.select(:ptype).map(&:ptype).uniq
  end

  def self.filter(selection)
    begin
      self.where(:ptype => selection.keys)
    rescue
      self.where(:ptype => selection)
    end

索引

= form_tag movies_path, :method => :get do
  Include:
  - @all_ptypes.each do |ptype|
    = type
    = check_box_tag( "types[#{ptype}]", " ", @selected_ptypes.include?(ptype))
  = submit_tag 'Refresh', :id => "ptypes_submit"

大多数代码都基于Ryan Bates的截屏视频#sortable table columns,第228集。

0 个答案:

没有答案