Rails: will_paginate "Show Next 10" Button?

I use the will_paginate stone in the rails 3 application. Right now it is paginated correctly and shows all the page numbers below for the list, as well as the previous and next buttons.

Instead of showing page numbers, I want the pagination to behave like a Facebook news feed. That is, if I am currently displaying 10 elements on a page and the user clicks the "Next" button, the page should go down and display 20 elements in total. I am looking for a way to change the will_paginate gem or use AJAX to accomplish this.

+4
source share
1 answer

Follow them to show more results at the click of a button:

  • Do not use the will_paginate tag in views
  • Use AJAX to get the following set of results when clicking the following link

The following code will delete the page numbers and simply show the next and previous buttons:

 <%= will_paginate @posts, :page_links => false %> 

You can find more pagination styles here.

Edited using an AJAX sample :

1.Create a form with a page number as a hidden field

 <%= form_tag url, :id => :filter, :method => :get do%> <%= hidden_field_tag :page, page %> <% end %> 

2. Add a space for β€œMore”

 <span class='more-results' >More</span> 

3.Javascript functions for binding clicks and submitting an AJAX form

 function bindMoreResults() { jQuery("span.more-results").unbind("click"); jQuery("span.more-results").click( function(e){ e.preventDefault(); getMoreResults(); }); } function getMoreResults() { jQuery('span.more-results').hide(); var page_value = parseInt(jQuery("form#filter #page").val()) + 1; jQuery("form#filter #page").val(page_value); var form = jQuery("form#filter"); jQuery.getJSON( form.attr("action") + ".json", form.serialize(), appendData ); } function appendData(data) { jQuery('span.more-results').show(); //parse the data and append it } 

4.Modify appendData accordingly. More on getJSON: http://api.jquery.com/jQuery.getJSON/

+8
source

Source: https://habr.com/ru/post/1368927/


All Articles