kaminari ajax pagination not updating the paginate

2019-06-24 05:42发布

问题:

I'm implementing pagination in rails3 using the kaminari gem.

I've been following this code from github https://github.com/amatsuda/kaminari_example/commits/ajax

unfortunately, the following code

jQuery('#paginator').html('<%= escape_javascript(paginate(@recipes,:remote=>true).to_s) %>');

does not seem to be working.

my javascript for what to do what a user selects a page is

  jQuery('a','nav.pagination').click(function(){
         // get the page value from href
          var page = jQuery(this).attr('href').replace('/home/index?page=','');
          jQuery.getJSON('?page='+page, function(data){
                 for (var r in data){
                   results.push(data[r]);
          });
              showResults(results,(page-1)*10);
              jQuery('#paginator').html('<%= escape_javascript(paginate(@recipes,:remote=>true).to_s) %>');
  });

the showResults function runs through the JSON and creates a bunch of html elements. Everything is working great, except that the pagination isn't updating to show the current page when the results are reloaded.

I don't get any errors in firebug, i'm using Rails 3 with ruby 192.

回答1:

Are you sure you have '#paginator' element for the paginator in you HTML? I mean, does the following code return the element on Firebug?

jQuery('#paginator')


回答2:

In order to update result and paginate you need to update both at a time. Paginate AJAX sends a request as JS so to handle them you should have action.js.erb file inside view folder. Perform two things here.

1) Update result

$("#issues").html("<%= j render(:partial => 'recipes') %>");

2) Update Paginate

$('.pagination').html('<%= escape_javascript(paginate(@recipes, :remote => true).to_s) %>');

Find respective page attribute to update paginate. This will update result and paginate run time.