Table pagination with AngularJS and Rails - ruby-on-rails

I have a Rails application which uses AngularJS for displaying and asynchronously updating a list of objects (configured as AngularJS resources). Is there a simple way to server-side paginate this table?

The will_paginate gem offers a way to paginate queries in Rails controllers and models. From the github README:
## perform a paginated query:
#posts = Post.paginate(:page => params[:page])
# or, use an explicit "per page" limit:
Post.paginate(:page => params[:page], :per_page => 30)
## render page links in the view:
<%= will_paginate #posts %>
You can customize the default page contents:
# for the Post model
class Post
self.per_page = 10
end
# set per_page globally
WillPaginate.per_page = 10
And with Active Record 3 you can do things like:
# paginate in Active Record now returns a Relation
Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')
# the new, shorter page() method
Post.page(params[:page]).order('created_at DESC')

Try doing pagination server side with #ngTasty
git : https://github.com/zizzamia/ng-tasty
docs : http://zizzamia.com/ng-tasty/directive/table-server-side

Related

Rendering paginated pages in JBuilder views

I am currently paginating the return of a query attendees that has over 9000 items. My pages and routing work fine but I would like them to appear at the bottom of the page as clickable links to that page of the results. I am relatively new at using JBuilder I am using the Kaminari gem as well as the API-Pagination gem and would like to know how to I add visible/clickable page numbers to a JBuilder view according to Kaminari Docs <%= paginate #attendees %> is all that is needed. But as far as I understand JBuilder does not work or interpret that logic as its purely manufacturing JSON objects? Any advice is appreciated as well as a better explanation of what JBuilder is doing.
Controller
module Reports
class ConferencesController < ::ApplicationController
def attendees
#conference = Conference.find(attendee_params[:conference_id])
#attendees = #conference.attendees
paginate json: #attendees, per_page: 500
end
private
def attendee_params
params.permit(:conference_id)
end
end
end
View
json.conference #conference, partial: 'conference', as: :conference
json.attendees #attendees, partial: 'attendee', as: :attendee
<%= paginate #attendees %>
Kaminari works great of the box for HTML partials, but there are some additional things you need to do to set it up for other response formats. You can remove the paginate json: #attendees, per_page: 500 line from your controller in favor of something like
#attendees = #conference.attendees.page(params[:page]).per(500)
Additionally you will need to provide additional information to your jbuilder partial to render this information.
Something like this:
json.meta do
json.total_pages #attendees.total_pages
json.total_count #attendees.total_count
end
# See the Kaminari docs for more methods available https://github.com/kaminari/kaminari#the-page-scope

How can I send paginated requests to third party API in rails

Im building a web application in rails to fetch records from a third party API. This third party API accepts page parameter. For eg: GET http://thirdpartyapi.com/records?page=2
How can I build my html in paginated format, so that when user clicks on number 2, it should send page=2 and when user clicks on number 4, it should send page=4 in the requests. Is there any gem for that?
class DemoController < ApplicationController
def index
response = HTTP.get('http://thirdparty.com/records', {query: {page: params[:page]}}) # it will return 30 items by default
#items = response['items'].paginate(page: params[:page], per_page: 10)
end
end
This is my views
<%= will_paginate %>
If you want to do this manually it's more work than just using the will_paginate gem
First you would need to get a count of records so that you know how many pages (aka how many links at the bottom you will have for pages)
#num_of_records = Object.count / per_page
Then you will need to handle that through JS depending on the number of links you want to display.
Once a user clicks on yourlink.com/?page=2 it should load with the correct data, or if you choose to you can remove elements from the div/table and insert new ones by returning them if you do an AJAX call.
Your controller would look something like this:
def index
page = params[:page] || 1
per_page = 10
#num_of_records = Object.count / per_page
objects_to_append = Object.paginate(:page => page, :per_page => perPage)
render json: { success: true, objects_to_append: objects_to_append }
end
I highly recommend to use kaminari instead. they have a way to do this easily.
https://github.com/kaminari/kaminari#paginating-a-generic-array-object

Tire + Will_Paginate not taking Tire.options

I'm trying to integrate Tire into my site and I'm having difficulty with pagination. I've tried paginating the results outside of the context of Tire and will_paginate is working on that Array. However, when I try will_paginate within the context of Tire I'm having one large problem.
Will_Paginate will display the correct number of pages with consideration of :per_page but when I click on that page the results are not loaded, rather they are the same as on the first page. The page number is highlighted in the will_paginate navigation.
#results.inspect yields this:
#<Tire::Search::Search:0x007f88ab9153d0 #indices=["deja-set-development"], #types=[], #options={:load=>true, :page=>1, :per_page=>2}, #path="/deja-set-development/_search", #query=#<Tire::Search::Query:0x007f88ab915088 #value={:query_string=>{:query=>"oh"}}>, #facets={"type"=>{:terms=>{:field=>:_type, :size=>10, :all_terms=>false}}}>
Here is where I call will_paginate:
= will_paginate #search_results.results, params
Here is where I iterate through the results
#search_results.results.each
Does anyone have any thoughts?
Edit ---
I'm not sure what is going on, but I did this and it is working.
#search_results = #search_results.paginate(:page => params[:page], :per_page => 5)
Please see the integration test in Tire, and make sure you're passing all options properly.
So to clarify, I've attached my github correspondence with #karmi here.
https://github.com/karmi/tire/issues/627#issuecomment-13449368
I was using Tire.search as opposed to searching by model. As #karmi notes, at the moment :per_page and :page are not supported with Tire.
Here is how I solved this:
#search_results = Tire.search [:index1, :index2, :index3], :load => true, :from => from, :size => size do
query do
string q, :default_operator => 'AND', :fields => [:name1, :name2]
end
end
I ended up having to spin my own small pagination system to increment 'size' and 'from'. Here's the elasticsearch link on the topic.
http://www.elasticsearch.org/guide/reference/api/search/from-size.html
You're able to still access
= #search_results.results.total_entries/next_page/previous_page
which helps with pagination.
Thank you again #karmi.

How to use pagination in rails 3.2.3?

I'm new in RoR, and I want to include pagination in my application.
Which is the best way to perform this task? Please suggest me. Is it possible to use will_paginate gem in rails 3.2.3 ?
If yes, in which method I should include:
Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')
Post.page(params[:page]).order('created_at DESC')
Yes, it is possible to use will_paginate gem in rails 3.2.3 applications.
And the code will depends on what each action will perform.
The code below usually goes on a #index method, in your PostsController.
#posts = Post.where(:published => true).paginate(:page => params[:page]).order('id DESC')
So, in your posts/index.html.erb view file you can use the following code to display the pagination links:
<%= will_paginate #posts %>

Pagination and table sorting in Rails

I am using will_paginate and sort helper for pagination and sorting resp..
But im facing one problem while sorting that when im on page 2 or 3 or any other page than first page.
It redirects to the first page and sorts the first page only.
please help me how to sort all the records and go back to page where I was.
What's the reason you need to go back to the page (2 or 3)? Records will change position and probabily page so you will not find them at the same place.
so why you don't change only the :order value (ex. from 'firstname DESC' to 'lastname DESC) when you call the action again?
# people_controller.rb
def index
#people = People.search(params[:my_order], params[:page])
end
# models/people.rb
def self.search(my_order, page)
paginate :per_page => 10, :page => page,
:conditions => ['job like ?', "Driver"],
:order => "%#{my_order}%"
end
I have more powerful solution for this: https://github.com/mynameisrufus/sorted

Resources