Chris
Chris

Reputation: 748

Any good Rails example/framework for sorting, filtering, pagination with Ajax

Is there any open source (or example) code for Ruby on Rails which can filter, sort, and paginate a certain model? Also, it would be great if the results could come back via Ajax. A good example of what I'm looking for can be seen on this Trulia web page

http://www.trulia.com/for_sale/30000-1000000_price/10001_zip/

(Note that as filters are checked off, the results are updated without a page reload.)

These kinds of operations (filter, sort, paginate) are so common that someone must have written something for this. I could figure it out myself, but am hoping there is either example code or a gem that provides the functions I would need. And again, I'm hoping it can be done with Ajax using either jQuery or prototype.

Thanks.

Upvotes: 15

Views: 8472

Answers (3)

lfx
lfx

Reputation: 111

You should definitely checkout smart_listing gem (https://github.com/Sology/smart_listing).

It uses kaminari for pagination and apart from sorting & filtering, smart_listing supports also in-place editing.

Here's a demo.

Upvotes: 9

Robin
Robin

Reputation: 21884

For all the ajax stuff, you can use jquery and just add events to checkboxes for instance:

$(":checkbox").change(function() {
    var form = $(this).closest("form");

    form.submit() // if you use the jquery form plugin http://jquery.malsup.com/form/

    //or
    $.ajax({
        url: form.attr("action"),
        type: "POST",
        dataType: "script",
        data: form.serialize()

    })
})

Filtering and sorting can be done easily with a query based on the parameters received from the form

Model.where(...).order(...).paginate(:per_page => 1, :page => params[:page])

and you can use will_paginate (https://github.com/mislav/will_paginate) for pagination. It's a great gem.

You would either update the page in the .js.erb file matching the controller action, or in the success callback of the ajax call if you used dataType: "html".

Upvotes: 4

Related Questions