RoR Grid Control Part II 6

Posted by sbecker Wed, 03 Aug 2005 08:52:00 GMT

My reusable “grid control” with paging and filtering is almost finished. I made some changes to the syntax, and I converted the whole thing to a component, so it can simply be dropped in the components directory of a rails app and used as is.

Defining the columns is very DRY, the minimum thing you have to specify for each column is a title. From this the component will guess the actual database column to be the same (with spaces converted to underscores). If your database column name is different, you can specify that.

I also added some display format options, including “email”, “website” and “money” and “date”. These will make the columns look and behave as you would expect.

Here’s what the code to call it looks like now:

  def my_controller_method
    grid_html = render_component_as_string(:controller => 'controls/grid', :action => 'grid',
      :params => {
        :model => Product,
        :obj => params[:obj],
        :columns => [
          { :title => "Category",
             :db_column => "category_name",
             :filter_type => :select,
             :filter_column => "category_id",
             :filter_options => Category.find(:all, :order => "name ASC").collect {|c| [ c.name, c.id ] }
          },
          { :title => "Name" },
          { :title => "Published",
             :filter_type => :select,
             :filter_options => Product.published_options
          },
          { :title => "Featured",
             :filter_type => :select,
             :filter_options => Product.featured_options
          },
          { :title => "Retail", 
             :db_column => "retail_price", 
             :format => :money, 
             :filter => :false 
          },
          { :title => "Stock", 
             :db_column => "in_stock", 
             :filter => :false }
        ],
        :joins => ["LEFT OUTER JOIN categories ON products.category_id = categories.id"],
        :alias_columns => [["categories.name", "category_name"]],
        :order_by => "category_name, products.name",
        :per_page => 25
      })
    render :text => grid_html, :layout => true
  end

Once I get everything reasonably ironed out and add a few more things like sorting, I plan to open source this code…

Again, here is what mine looks like. This could easily be customized to look any way you want, by simply modifying the view rhtml file:

Comments

Leave a response

  1. Avatar
    ntollervey Tue, 30 Aug 2005 21:40:39 GMT
    Just wondering how this project was progressing. It looks like its exactly what I need for a current RoR project. When (and where) will this code be released?
  2. Avatar
    Stuart Johnson Wed, 07 Sep 2005 22:31:50 GMT
    great effort! This sort of thing had been on my wishlist since I started looking at Rails seriously. A cool next step once the core functionality is in place would be to add Ajax for an edit in place data grid (!) ... ps - if you're still having problems with IIS and Rails, I've posted some possible fixes in a response to http://rubyforiis.sosukodo.org/rubyforiis/feedback/unabletoconnecttofastcgiserver
  3. Avatar
    scott Wed, 14 Sep 2005 13:39:10 GMT
    ntollervey - err, soon! i need to modify the paging buttons to pass along any filtering parameters, and then it should basically work 100%. as to where, here will be a good place to look im sure! Stuart - awesome, thanks for the IIS tips, I'll have to try that! yes, ajax for in place editing, i like that idea... hmm, shouldn't actually be too hard...
  4. Avatar
    Bryan Tue, 17 Jan 2006 16:17:36 GMT
    Just wondering where this project is at... I'd like to use it :)
  5. Avatar
    Adrian Madrid Tue, 07 Feb 2006 23:28:05 GMT
    Me too! Please let us know where you are at and if you are releasing it. Thanks! AEM
  6. Avatar
    Omer Raviv Wed, 08 Feb 2006 18:21:44 GMT
    Just another vote for releasing this to the public :) It looks spectecular. Open sourcing it and allowing people to offer patched\improvements\comments should make it even more so. Please, please, gimme! :)