views:

267

answers:

2

I am looking for a tutorial or documentation on how to create sortable table with filters in Rails application without using any jQuery or rails plugins.

I want to use jQuery as javascript library and not prototype.

All what I found in the internet is using one or the other jQuery plugin.

+3  A: 

If you are using Rails 2.3.x, the searchlogic gem may do the trick.

A good starting tutorial is Ryan Bates's tutorial on searchlogic from railscasts.

Thenm you should read this tutorial (ordering, searching and pagination with searchlogic). Take look also on the searchlogic_example source code (where you have 3 examples: no AJAX, AJAX using Rails helpers, AJAX using jquery).

Vlad Zloteanu
Hi,searchLogic seems promising to me, unfortunately the examples don't work. Looks like they are outdated. I also didn't find any recent example of searchlogic using jquery. Can you suggest some ?
rangalo
A: 

You need to create a server side call (GET) to provide a portion of the table. It needs to understand (1) the sort order (2) page that is requested and page size. You can accomplish this will an index method that looks for parameters and something like will_paginate (although there are many plugins to help with this).

On the client side, with jQuery, you simply need to handle all the clicks the user might make. For example, if they can sort by clicking on a column label, attach a click handler to it and make a new server request for the data. You can write a single Javascript method to do this, but you need to figure out how you represent page number, sort etc. in Javascriptland.

Implementing "filters" is just an extension to this. I don't know exactly how you want the UI to work, but it is another input into your server-side call.

Ryan Bates has a few railscasts that will help with all aspects of this, but I don't know of one that combines it all.

Just take it one step at a time...

ndp
ndp is giving the correct direction here. "Sortable" is clear to me, clicking on a table column header, probably an alphabetic sort, but it depends on the data in your table too. "ajax filters" is vague. Are these keywords/tags that must be present in the table data? If so, then send these strings to the server, parse them in a Rails controller action, and perform the query. The Rails controller action could render a partial, perhaps looping over an array of results via an instance variable. Your controller can pass query results to the partial via this instance variable.
Andy Atkinson
Unfortunately, once you are at a multiple pages, sorting will almost always need to go to the server (to be accurate). Although this isn't crazy to implement, it does take time and care.It's also reasonable to push back on requirements a bit: what do the really need to sort on? Is more than one page of results that valuable? What is the user trying to find? etc. I've implemented this full list component countless times in my career, but think it was only the right UI in a couple cases... just a thought.
ndp