# Selection Lists and sorting data grids

**URL:** <https://www.ruby-forum.com/t/selection-lists-and-sorting-data-grids/156332>\
**Category:** Rails\
**Created:** [December 24, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/selection-lists-and-sorting-data-grids/156332 "2008-12-24T17:19:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [December 24, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/selection-lists-and-sorting-data-grids/156332/1 "2008-12-24T17:19:12Z")

</div>

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,  
in\_stock:boolean  
./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| [v.name, v.id] }).insert(0, [’–All–’,‘all’]) %\>

# will generate a list of ‘All’, ‘Vendor 1’, ‘Vendor 2’, etc.

# 

# –All–

# Vendor 1

# Vendor 2

# …

# 

\<% selection :product, :in\_use, [[’–All–’,‘all’],[‘YES’,  
‘true’],[‘NO’, ‘false’]] %\>

# will generate a list of ‘All’, ‘YES’, ‘NO’

# 

# –All–

# YES

# NO

# 

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 🙂

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [December 24, 2008, 5:28pm UTC](https://www.ruby-forum.com/t/selection-lists-and-sorting-data-grids/156332/2 "2008-12-24T17:28:14Z")

</div>

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.
