The datalist_for plugin 10

Posted by brian Tuesday, May 02, 2006 19:39:00 GMT

Following in the Rails tradition, I created a simple solution to a problem I needed to solve, and I thought I’d extract it and share.

Here’s the scenario. You’ve got some simple tabular data. Some examples: order line items, flight status info, widget inventory, players on your corporate softball team. And you need a quick and dirty form to CRUD that data.

The datalist\_for plugin helps you do just that. Inspired by the excellent invoice line-item thingy in Blinksale, datalist\_for creates a table of data, complete with inline editing, and ajaxy create and delete. The resulting markup is very simple, and can be easily styled to suit your needs.

Usage

After installing the plugin, just include DatalistFor in your controller, and drop the datalist\_for tag into your form. So, your complete form might look like:

<%= form_tag "save" %>
  <%= datalist_for :monkeys, Monkey.find(:all) %>
  <%= submit_tag "Save" %>
<%= end_form_tag %>

This will spit out a fully editable page of your favorite pet monkeys (and who doesn’t have pet monkeys). Then, in the target action of your form, do a save\_datalist to save the data. An example:

def save
  save_datalist
  flash[:notice] = "Your monkeys have been saved." 
  redirect_to :action => 'whatever'
end

You can also specify a subset of your model data:

<%= datalist_for :monkeys, Monkey.find_all_by_user_id(1), :create_with => { :user_id => 1 } %>

This will give you only monkeys for User 1. And any new monkeys will belong to User 1.

Other options:

<%= datalist_for :monkeys, Monkey.find_all_by_user_id(1), { :create_with => { :user_id => 1 },
                                    :show_header => false,
                                    :add_text => "Add a new monkey",
                                    :exclude => [:likes_bananas, nickname],
                                    :delete_content => image_tag "trash" } %>

This means:

  • Don’t show the header row
  • Use “Add a new monkey” as the link text for adding a new record (defaults to “Add modelname“)
  • Exclude the :likes_bananas and :nickname columns
  • Will use “trash.png” for row deletion instead of the default “Delete” text
###Demo Here's the obligatory <a href="http://luckymonk.com/monkey">demo page</a>. ###Installation Installing is straightforward:
ruby script\plugin install http://luckymonk.com/svn/repos/datalist_for

Exercises for the reader

datalist\_for suits my limited needs for the time being, but it’s far from perfect. Any ideas or suggestions are welcome. If you would like to help me write some of this stuff, even better. Here is my to-do/wishlist/known issues:
  • Boolean types will generate a checkbox. A text type will create a text area. Everything else is a text field. There are no validations on say, dates, numeric fields, or any other types for that matter.
  • It only shows your content\_columns. A select tag for joins would be nice.
  • You can’t have more than one datalist in a form. Don’t try it. You’ll be sad.
  • Paging. There isn’t any.

Comments

Leave a response

  1. DeLynn Berry   May 03, 2006 @ 03:40 AM

    Brian, this looks excellent. I can't wait to give this a shot in my side project tomorrow!

  2. Oli Young   May 05, 2006 @ 09:04 AM

    looks great for a quick fix for a database interface ..

    but the Erb examples aren't displaying in Safari (might need to use the entities instead of the less-than/greater-than)

  3. Brian   May 08, 2006 @ 02:56 PM

    Sorry about that. Should be OK now. (Sadly, no Mac to test it on... yet)

  4. Scott   May 29, 2006 @ 03:21 PM

    Perhaps you could explain this:

    "After installing the plugin, just include DatalistFor in your controller..."

    a little better since there is NO documentation that I can find anywhere on what the heck this means. When I type "include DatalistFor" in my controller class just after the declaration, I get the error."uninitialized constant DatalistFor".

    Nothing on the wiki that points out HOW to use plugins, just how to write them

  5. Scott   May 29, 2006 @ 04:01 PM

    Never mind, the problem was head slappingly obvious. Had to restart the server after I installed the plugin. (slap)

    Maybe there needs to be a wiki entry with some troubleshooting tips for newbies to plugins.

  6. Marius   October 08, 2006 @ 11:06 AM

    http://luckymonk.com/svn/repos/datalistfor says "The requested URL /svn/repos/datalistfor was not found on this server."

  7. Brian   October 11, 2006 @ 06:05 PM

    Sorry about that Marius. It seems that our svn got corrupted somehow. We working on fixing it... will let you know when done.

  8. Brian   October 12, 2006 @ 01:31 PM

    Ok, everything is back to normal. Sorry for the inconvenience.

  9. Marius   October 14, 2006 @ 11:42 PM

    It works now. Thanks.

  10. Matthew F   November 27, 2007 @ 04:47 PM

    Hello...

    Three comments:

    1. thanks for the code!
    2. cannot disable checkboxes (as the value isn't included in the POST) - not sure how to solve this
    3. there's a bug in function "save_existing":

    if params[:datalistupdate].haskey?( "#{col.name}_#{id}" ) should be if params[:datalistupdate].haskey?( "#{col.name}_#{obj.id}" )

    Cheers, Matthew

Comment


(won't be published)