1、安装gem "wice_grid",
2、运行rails g wice_grid:install生成
create config/initializers/wice_grid_config.rb
create config/locales/wice_grid.yml
create app/assets/stylesheets/wice_grid.css.scss
此时查看application中是否有//= require wice_grid,如果没有,则添加,该句意思是引入该插件的js文件
基本使用@tasks_grid = initialize_grid(Task)
加入一些条件@tasks_grid = initialize_grid(Task.where(:active => true))
4、view层使用
<%= grid(@tasks_grid) do |g| g.column do |task| task.id end g.column do |task| task.title end g.column do |task| task.description end g.column do |task| task.archived? ? 'Yes' : 'No' end g.column do |task| link_to('Edit', edit_task_path(task)) end end -%>也可以写成下面的形式
<%= grid(@tasks_grid) do |g| g.column :name => 'ID', :attribute => 'id' do |task| task.id end g.column :name => 'Title', :attribute => 'title' do |task| task.title end g.column :name => 'Description', :attribute => 'description' do |task| task.description end g.column :name => 'Archived', :attribute => 'archived' do |task| task.archived? ? 'Yes' : 'No' end g.column do |task| link_to('Edit', edit_task_path(task)) end end -%>5、条件查询。一下是该插件在controller层使用可传入的参数
@tasks_grid = initialize_grid(Task, :order => 'tasks.title', :order_direction => 'desc' )
@options = {
:conditions => nil,
:csv_file_name => nil,
:csv_field_separator => Defaults::CSV_FIELD_SEPARATOR,
:custom_order => {},
:enable_export_to_csv => Defaults::ENABLE_EXPORT_TO_CSV,
:group => nil,
:include => nil,
:joins => nil,
:name => Defaults::GRID_NAME,
:order => nil,
:order_direction => Defaults::ORDER_DIRECTION,
:page => 1,
:per_page => Defaults::PER_PAGE,
:saved_query => nil,
:total_entries => nil,
:with_paginated_resultset => nil,
:with_resultset => nil
}
6、关联表的显示
Model:
class Project < ActiveRecord::Base belongs_to :customer, :class_name => 'Company' belongs_to :supplier, :class_name => 'Company' end
Controller:
@projects_grid = initialize_grid(Project, :include => [:customer, :supplier] )
View:
<%= grid(@projects_grid, :show_filters => :always) do |g| g.column :name => 'Project Name', :attribute => 'name' g.column :name => 'Customer company', :model => 'Company', :attribute => 'name' do |task| task.customer.name if task.customer end g.column :name => 'Supplier company', :model => 'Company', :attribute => 'name', :table_alias => ' suppliers_projects' do |task| task.supplier.name if task.supplier end end -%>