How to use pagination in rails 4.2.5 and also have the ability to reorder the list with ajax? -


i'm working on forum-type app in rails v 4.2.5. index page list of questions being discussed in application , default sorted created_at date. using kaminari gem paginate of questions (25 per page). had app set this:

questions controller:

def index   @questions = question.order(:created_at).page params[:page] end 

index view:

# render partial iterates through questions list display  # title of questions, include paginate code below.  <div class="pagination">   <%= paginate @questions  %> </div> 

i decided wanted users able sort questions different criteria (e.g., total amount of upvotes, total amount of responses question, , asked questions). right now, can click link corresponding type of sort want , ajax new sorted list (a partial) onto page. however, when this, pagination not work , when click see second page of results, becomes unsorted.

index view sort links:

<div class="sort_selection">   <h3> sort by: </h3>     <%= link_to "by upvotes", "/questions/top?sort=votes", class:     "question_sort_link" %>     <%= link_to "answers provided", "/questions/top?sort=answers", class: "question_sort_link" %>     <%= link_to "recently asked", "/questions/top?sort=recent", class:     "question_sort_link" %> </div> 

index controller:

def top     case params[:sort]     when "votes"       @questions = question.sort_by_votes #sort_by_votes method in question model performs sql query     when "answers"       @questions = question.where.not(answers_count: nil).order(answers_count: :desc).limit(25)     when "recent"       @questions = question.order(created_at: :desc).limit(25)     end     render partial: 'questions_list', layout: false end 

javascript ajax

$(document).on("click", ".question_sort_link", function(event){ event.preventdefault();   $.ajax({     method: "get",     url: $(this).attr("href")   }).done(function(sorted){     $('.questions_show_sorted').replacewith(sorted);   }); }); 

i fooled around placement of <%= paginate @questions %> in view, removed 25 limit in controller , added .page params[:page] after of queries in top route still cannot pagination work after i've ajax'ed sorted list onto page. have suggestions?

when switching pages data sorting lost, because reloading site different parameters. can either try pass data (the column going sort, , info asc or desc) new page, , sort before loading, or paginate using ajax (but means loading @ first load). can't tell "pagination not work problem", because don't know mean.

in general, thing trying rather complicated, , there no simple solution that. there library js called datatables (in theory) makes easier. there library called jq-bootgrid, , ruby gem called "smart listing".


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -