Calculate total items per page (pagination logic) - ruby

I need to check the total items of every page on pagination.
For example, I have 165 posts and a pagination with 4 links:
1,2,3,4
How can I discover the total items per page based on the number of pages generated?

I don't know what you're using for pagination, but the count for each page is generally set by a variable, then the remainder fills up the last page. It doesn't just output a random amount of the total on each page.
If using Kaminari, for example, there is a variable config.default_per_page.
Something like this should provide the answer.
full_pages = total_posts / default_per_page
spill_over = total_posts - (default_per_page * full_pages)

Related

REST API - Retrieve previous query in dynamoDB

I have 100 rows of data in DynamoDB and a api with path api/get/{number}
Now when I say number=1 api should return me first 10 values. when I say number=2 it should return next 10 values. I did something like this with query, lastEvaluatedKey and sort by on createdOn . Now the use case is if the user passes number=10 after number=2 the lastEvaluatedKey is still that of page 2 and the result would be data of page 3. How can I get data directly. Also if the user goes from number=3 to number=1 still the data will not be of page 1.
I am using this to make API call based of pagination on HTML.
I am using java 1.8 and aws-java-sdk-dynamodb.
Non-sequential pagination in DynamoDB is tough - you have to design your data model around it, if it's an operation that needs to be efficient at all times. For a recommendation in your specific case I'd need more details about the data and access patterns.
In general you have the option of setting the ExclusiveStartKey attribute in the query call, which is similar to an offset in relational databases, but only similar and not identical. The ExclusiveStartKey is the key after which the query will continue, meaning data from your table and not just a number.
That means you usually can't guess it, unless it's a sequential number - which isn't ideal.
For sequential pagination, i.e. the user goes from page 1 to page 2, page 2 to page 3 etc. you can pass that along in the request as a token, but that won't work if the user moves in the other direction page 3 to page 2 or just randomly navigates to page 14.
In your case you only have a limited amount of data - 100 items, so my solution for your specific case would be to query all items and limit the amount of items in the response to n * 10, where n is the result page. Then you return the last 10 items from that result to your client.
This is a solution that would get expensive at scale (time + cost) though, fortunately not many people will use the pagination to go to page 7 or 8 though (you could bury a body on page 2 of the google search results).
Yan Cui has written an interesting post on this problem on Hackernoon, you might want to check it out.

Get count of records matching filters Laravel pagination

I have few filters on the view page and want to show the count that matches the given conditions. Like on the product search result I have Free Shipping filter. I need the count of records matching my filters in the total record not just in the current page.
Explanation:
Suppose In the result set I have 100 records with pagination 20/page, 40 of them has free_shipping = 1. I want to show like Free Shipping(40). Currently, I do $products->where('free_shipping', 1)->count() but this gives me the result matching only in viewed records.
How can I get the total number of matching records regardless of pagination?
Thanks in advance
if Laravel Pagination is used then you can use the total() method.
Example :
$products->total();

How to combine multiple page records?

I have a model called DemoModel and contains 1000 records in DB. So i am paginating using paginator in Django(assume that per page 15 records, so i have 67 pages).
So i want to get the records of 3,4 and 5 pages and i have to append the records into list.
So can i get the objects_list based on page range or anything else i want to do?
Example:
records.page(1)
Here i am getting only one page records at a time, but how can i get multiple page records i.e; from fist page to third page
Assuming you are asking about the API request to get the paginated resources, and you are using the default pagination class: rest_framework.pagination.LimitOffsetPagination, then you can make an request as such:
https://api.example.org/accounts/?limit=30&offset=15
which in turns give you the 2nd and 3rd "page".
The limit indicates the maximum number of items to return, and is equivalent to the page_size in other styles. The offset indicates the starting position of the query in relation to the complete set of unpaginated items. doc link

Dynamic navigation shows incorrect result count

When we filter result by Duplicate Snippet filter (filter=p) we receive incorrect result count in dynamic navigation.
E.g.
Picture with issue
Actual result count = 5
Result are shown for end-user = 3 (Two pages was filtered)
Dynamic navigation by content format(Web Pages) = 5
As I know GSA has some problems with determinating actual count for a large number of results.
Is it possible to set filtered result count in dynamic navigation (Web Pages)?
GSA System Version: 7.2.0.G.252

How to get the total count after Kaminari pagination

I am using rails 3.2. I am paginating my results using .page(1).per_page(10)
like
#users = User.method().page(1).per_page(10)
Now how to find the total count of the users from the pagination
As because #users.count gives 10 from the first page and not the total count
How to get the total count of the users even after pagination
EDIT : #users.total_count gives the whole paginated count
As mentioned in the question, you can get the total count of the records with
#users.total_count
I've added this as an answer for the sake of completeness.
You can use #users.total_count.
However, if you're trying to get the total count for each page, i.e. something like:
20 of 135 results
then total_count will just return 135, not the number of results on the page.
If you want to handle this case as well as the case where the number of results is less than the pagination result number, then I'd go with something like this:
(per_size > #users.total_count) ? #users.total_count : per_size
where per_size is the value you are setting for your per scope (docs here).
User.count would give you the count but it would hit the db. If you are using mongodb #user.length would give you the total count

Resources