Post Meta

Bookmarks

  • Delicious
  • Digg
  • Reddit
  • Magnolia
  • Newsvine
  • Furl
  • Facebook
  • Technorati

Users will be able to customize columns like in this example (click the “Customize” button)

  1. Create a custom action link in the controller’s ActiveScaffold configuration section
  2. 1
    
    config.action_links.add 'column_configurator',:label => l(:active_scaffold, :config)
  3. Create an AJAX form for all controller columns as checkboxes
    • In the controller:
    • 1
      2
      3
      4
      5
      6
      7
      
      # Configs which columns to show
        def column_configurator
          @cols = active_scaffold_config.columns
          @active_cols = active_scaffold_config.list.columns.map {|c| c.name}
          @form_name = "custom_columns"
          @checkboxes = generate_column_checkboxes(@cols, @active_cols, @form_name)
        end
    • In the helper
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      
      # Generate checkboxes for defining AS columns by user 
        def generate_column_checkboxes(list, checklist, form_name)
          ret = ""    
          for l in list
            ret += "<span class='as_column_config_chekckbox'>" 
            ret += "<input id='#{form_name}_#{l.name}' name='#{form_name}[#{l.name}]' type='checkbox'" 
            ret += checklist.include?(l.name) ? " checked='checked' />" : "/>" 
            ret += l.label + "</span>"
          end
          ret
        end
    • In the view
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      
       
      <h3><%=h l(:active_scaffold, :custom_columns) %></h3>
      <% remote_form_for @form_name, 
      	:url => {:action => "column_configurator_form"}, 
      	:update => "departments-active-scaffold",
      	:after => "$('departments-table-loading-indicator').style.visibility = 'visible'",
      	:complete => "$('departments-table-loading-indicator').style.visibility = 'hidden'" do |f| %>	
      	<br/><br/>
      	<%= @checkboxes %>	
      	<div id="clear"></div>
      	<br/>
      	<%= submit_tag l(:active_scaffold, :custom_columns_save)%>
      <% end %>
  4. Filter the controller / ActiveScaffold to show the columns
  5. Back in the controller add the following method:

    1
    2
    3
    4
    5
    
     
    # update AS config
      def column_configurator_form   
        redirect_to( :params => {:action => 'index', :controller => 'departments', :method => 'get', :c_columns => params[:custom_columns]} ) 
      end
  6. And finally add the filter for AS in the controller
  7. 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    before_filter :update_as_config
     
    ...
     
    private 
     
      # configure AS based on parameters
      def update_as_config
        cc = params[:c_columns]
        cc.each do |c|
          c[1] == "0" ? active_scaffold_config.list.columns.exclude(c[0]) : active_scaffold_config.list.columns.add(c[0])
        end unless cc.nil?
      end
  8. This style looks nice on our checkboxes
  9. 1
    2
    3
    4
    5
    
    .as_column_config_chekckbox {
    float: left;
    min-width: 20%;
    margin-bottom: .25em;
    }


Related posts

Leave A Comment

+ -