Created
April 25, 2012 21:03
-
-
Save anithri/2493365 to your computer and use it in GitHub Desktop.
Configuration piece for jqgrid_rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#app/grids/addendum_grid.yml | |
--- | |
:grid: | |
:dom_id: "addendums_grid" | |
:caption: "Addendums" | |
:pager_options: | |
:core: | |
:edit: true | |
:search: false | |
:add: true | |
:del: false | |
:columns: | |
:proposed_date: | |
:type: :date | |
:proposed_amount: | |
:label: "Proposed Amt" | |
:type: :currency | |
:accepted_date: | |
:type: :date | |
:accepted_amount: | |
:type: :currency | |
:notes: | |
:type: :text_area |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#app/controllers/addendums_controller.rb | |
class AddendumsController < ApplicationController | |
respond_to :html, :json | |
authorize_resource | |
before_filter :declare_grid | |
def declare_grid | |
@addendum_grid_def ||= ApplicationGrid.new(:addendum) | |
end | |
def index | |
respond_to do |format| | |
format.html do | |
@grid = @addendum_grid_def.mk_grid(projects_addendums_path(params[:project_id],format: :json)) | |
end | |
format.json do | |
render json: grid_response(Addendum.where(project_id: params[:project_id]), params, @addendum_grid_def.column_list) | |
end | |
end | |
end | |
#SNIP | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rails_javascript_helpers' | |
class ApplicationGrid | |
attr_reader :options, :dom_id, :col_models, :model | |
CONFIG_DATA = {} | |
(Rails.root + "app/grids").each_child(:with_dir) do |file| | |
next unless file.to_s.end_with?("_grid.yml") | |
grid_name = file.basename.to_s.gsub(/_grid\.yml$/, "").singularize.intern | |
CONFIG_DATA[grid_name] = YAML.load_file(file) | |
end | |
def initialize(model_sym) | |
unless CONFIG_DATA.has_key?(:application) | |
raise StandardError, "No grid defaults exist. Check app/grids/application_grid.yml" | |
end | |
raise ArgumentError unless CONFIG_DATA.has_key?(model_sym) | |
@model = model_sym | |
@options = CONFIG_DATA[:application].deep_dup.deep_merge(CONFIG_DATA[@model]) | |
@dom_id = @model.to_s + "s_grid" | |
@col_models = {} | |
logger.warn @options.inspect | |
column_list.each do |column| | |
logger.warn column.inspect | |
raise StandardError, "No type def for #{model_sym}##{columns}" unless options[:columns][column].has_key?(:type) | |
type = options[:columns][column][:type] | |
@col_models[column] = options[:column_types][type].deep_dup.deep_merge(options[:columns][column]) | |
@col_models[column][:label] ||= column.to_s.titleize | |
@col_models[column][:name] ||= column.to_s | |
end | |
end | |
def grid_options | |
options[:grid] | |
end | |
def pager_options | |
options[:pager_options] | |
end | |
def column_list | |
options[:columns].keys | |
end | |
def mk_grid(url_base) | |
grid_options[:url] = url_base | |
grid = JqGridRails::JqGrid.new(dom_id, grid_options) | |
col_models.each do |column, col_opts| | |
grid.add_column col_opts[:label], col_opts[:name],col_opts | |
end | |
grid.pager_options.deep_merge!(pager_options) | |
return grid | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#app/grids/application_grid.yml | |
--- | |
:grid: | |
:height: '100%' | |
:pager: true | |
:row_list: [10,25,50] | |
:sortname: :id | |
:row_num: 25 | |
:shrink_to_fit: false | |
:force_fit: true | |
:pager_options: | |
:core: | |
:edit: true | |
:search: true | |
:add: true | |
:del: false | |
:edit: | |
:mtype: "PUT" | |
:modal: true | |
:view_pager_buttons: false | |
:close_on_escape: true | |
:close_after_edit: true | |
:add: | |
:mtype: "PUT" | |
:modal: true | |
:view_pager_buttons: false | |
:close_on_escape: true | |
:close_after_edit: true | |
:del: | |
:mtype: "DELETE" | |
:reload_after_submit: true | |
:close_on_escape: true | |
:search: | |
:multiple_search: true | |
:columns: | |
:id: | |
:label: "ID" | |
:type: :integer | |
:align: :right | |
:width: 60 | |
:editable: false | |
editoptions: | |
:readonly: true | |
:column_types: | |
:string: | |
:editoptions: | |
:size: 25 | |
:width: 100 | |
:edititable: true | |
:boolean: | |
:align: :center | |
:edititable: true | |
:edittype: "checkbox" | |
:editoptions: | |
:value: "true:Yes;false:No" | |
:stype: "select" | |
:searchoptions: | |
:sopt: ['eq'] | |
:value: ":All;1:Yes;0:No" | |
:formatter: 'select' | |
:width: 100 | |
:integer: | |
:align: :right | |
:width: 100 | |
:edititable: true | |
:date: | |
:align: :center | |
:width: 100 | |
:edititable: true | |
:currency: | |
:align: :right | |
:formatter: "currency" | |
:classes: "currency" | |
:edititable: true | |
:text_area: | |
:edittype: "textarea" | |
:editoptions: | |
:rows: 6 | |
:cols: 40 | |
:width: 250 | |
:edititable: true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment