Forum: Ruby on Rails Selection Lists and sorting data grids

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
7db3b1680807d393d40260591b4ae5e0?d=identicon&s=25 sa 125 (sa125)
on 2008-12-24 17:19
Hello - In my app I'm displaying many data grids, and I'm trying to add
multiple selection lists and a search box to each grid to allow
filtering the rows. Since I'm very new to this, I'm trying to understand
how to do things right from the start. Say I have product and vendor
models like so:

./script/generate model Product name:string, vendor_id:integer,
./script/generate model Vendor name:string, location:string

of course, the product belongs_to :vendor and the vendor has_many
:products. So far so good.

My app displays a grid of products, and I'd like to stack filter them
using a selection list of vendors, and a list of in_stock. My first
question is how to filter the grid with each selection. Say these are
the drop down lists:

<% selection :product, :vendor_id, (Vendor.find(:all, :order =>
"name").collect {|v| [,] }).insert(0, ['--All--','all']) %>

# will generate a list of 'All', 'Vendor 1', 'Vendor 2', etc.
# <select id="product_vendor_id" name="product[vendor_id]">
#  <option value="all">--All--</option>
#  <option value="1">Vendor 1</option>
#  <option value="2">Vendor 2</option>
#  ...
# </select>

<% selection :product, :in_use, [['--All--','all'],['YES',
'true'],['NO', 'false']] %>

# will generate a list of 'All', 'YES', 'NO'
# <select id="product_in_stock" name="product[in_stock]">
#  <option value="all">--All--</option>
#  <option value="true">YES</option>
#  <option value="false">NO</option>
# </select>

1. How do I sort the table when the user selects something? I'm assuming
this should be done with a call to the controller or helper, but not
sure how.
2. How do I use ajax to sort without postback?
3. How do I throw in a free text search box in the mix?

I realize that's a lot of ground to cover, but even a some helpful link
will be great - thanks :)
7db3b1680807d393d40260591b4ae5e0?d=identicon&s=25 sa 125 (sa125)
on 2008-12-24 17:28
On the same note, how would I go about turning the column headers to
links that'll sort the table by the column clicked? Such that if the
user click the Product Name header, the table sorts alphabetically by
product name.

Thanks again.
This topic is locked and can not be replied to.