开发者

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

开发者 https://www.devze.com 2023-02-11 04:58 出处:网络
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

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.


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.


http://railscasts.com/episodes/240-search-sort-paginate-with-ajax


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".

0

精彩评论

暂无评论...
验证码 换一张
取 消