Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8696417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:16:07+00:00 2026-06-13T01:16:07+00:00

Okay, So I will get a little more specific (since my last question). Basically,

  • 0

Okay, So I will get a little more specific (since my last question).

Basically, I have a pagination page – and there are three display categories BUT only 50 items display per pagination page. So, you could get all of a single category taking up a page, or a combination of different categories – but they can’t exceed 50 items per page. Once a new category is reached, a header displays, then the associated items. I am having trouble figuring out the math to append to the ajax call to request each of the categories. For example, lets say you search for linen. And lets say you get back a total of 329 matches spread across the three categories as shown below. Now, I will be handed back some data so I will know certain things up front even though I will only query 50 items at a time.

A. Search for linen :

Base data

Total Items: 129
Category1 has 111 items
Category2 has 101 items
Category3 has 17 items

so, on page 1 you would see:

CATEGORY 1 HEADER
... list of the 50 items ...

B. User clicks “next”
(since we are still in the category 1 threshold), we’d get the second set of 50.

Page 2:

CATEGORY 1 HEADER
List of next 50 items

C. USER clicks “next”
(we broke category 1 total, so now we reach into category 2).

Page 3:

CATEGORY 1 HEADER
List of 11 items

CATEGORY 2 HEADER
List of 39 category 2 items

etc..

The issue I am having, that without doing a ton of if/else/||/&& etc.. is there a nifty algorithm that will give me ranges so on the “next” page click I can have ajax params request ranged for the backend.

ie.. so for the third next page click – (has both category1 and category 2, as shown above).
In a perfect world my Ajax would fire two calls, one for category1 and one for category 2, like so. Of course, if the list of 50 spanned all three cats in full, then three calls would be made.

/category1?rows=11&start=101
/category2?rows=39&start=1

something like that. So I know I want 11 items from first ajax call with the start of 101. The backend, I assume, will know the range is 101-111 etc..

I hope I am succinct enough here to supply my needs. The solution seems very mathish.

I was working along the lines of, just penciling things…

(category1 – (max x (currPage – 1)) / max >= 1 –> request 50 category 1 if true;

(category1 – (max x (currPage – 1)) / max < 1 –> request the Modulus amount and then start into category2

We use jquery.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:16:07+00:00Added an answer on June 13, 2026 at 1:16 am

    One solution is available at http://jsfiddle.net/CrossEye/eKApZ/.

    The basic idea is this:

    var createCategoryRangesForPage = (function() {
        var ranges = function(itemCounts) {
            var total = 0;
            var indices = [0].concat(jQuery.map(itemCounts, function(item) {
                return total = total + item.count;
            }));
            var result = function(pageNbr, pageSize) {
                var first = pageSize * (pageNbr - 1),
                    last = Math.min(first + pageSize, total),
                    firstIdx = indices.length - 1,
                    lastIdx = indices.length - 1;
                if (first >= total) {return ranges.OUT_OF_BOUNDS;}
                while (indices[firstIdx] > first) {firstIdx--;}
                while (indices[lastIdx] >= last) {lastIdx--;}
                return jQuery.map(new Array(lastIdx - firstIdx + 1), function(item, idx) {
                    var index = idx + firstIdx;
                    return {
                        name: itemCounts[index].name,
                        start: Math.max(first, indices[index]) - indices[index] + 1,
                        // end: Math.min(last, indices[index + 1]) - indices[index],
                        count: Math.min(last, indices[index + 1]) - Math.max(first, indices[index])
                    };
                });
            };
            result.OUT_OF_BOUNDS = ranges.OUT_OF_BOUNDS;
            return result;
        };
        ranges.OUT_OF_BOUNDS = {message: "Out of bounds", toString:function(){return this.message;}};
        return ranges;
    }());
    

    You could then create a pagination function like this:

    var catRangesForPage = createCategoryRangesForPage([
        {name:"CATEGORY 1", count: 111},
        {name:"CATEGORY 2", count: 101},
        {name:"CATEGORY 3", count: 17}
    ]);
    

    And call it for, say, page 3, with page size 50, like this:

    catRangesForPage(3, 50)
    

    to get a result that looks like this:

    [
        {
            "name": "CATEGORY 1",
            "start": 101,
            "count": 11
        },
        {
            "name": "CATEGORY 2",
            "start": 1,
            "count": 39
        }
    ]
    

    which you could clearly use to generate your AJAX calls.

    If you wanted to, it would be trivial to move the pageSize parameter into the initial call. That’s less flexible, but might be slightly easier to use. To change this, just move the pageSize parameter from this line:

            var result = function(pageNbr, pageSize) {
    

    to this one:

        var ranges = function(itemCounts) {
    

    (It should probably be the first parameter here, as it might get lost in the shuffle if you pass an object literal for itemCounts the way my sample does.)

    The other thing to note is that the function returns a signal object named OUT_OF_BOUNDS to let you know you’ve tried to access a page out of range. That object is namespaced to both the generator function and the function it returns, whichever you find more convenient.

    This has not been tested outside the bounds of your initial problem, and I wouldn’t be terribly surprised to find boundary issues somewhere with it.

    (This does use jQuery, but only for its map utility function. You could use any decent map implementation, including the native one in recent browsers.)

    Best of luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay this question is very simple: I have a facebook page, and a website.
Okay so I'm trying to make a little gag program that will run away
Okay, this scenario is a little complicated, but I will try to explain it
Okay, here's my short question: I know that === and !== operators will compare
Okay, next PHPExcel question. I have an HTML form that users fill out and
I'm working on a little project, basically I have some text on my PHP/HTML
Okay, this will probably get closed or get some negative replies, but I've been
Okay so I'm make a program where I will be universally accepting cocoa trackpad
Okay, so I'm developing an application that will allow users to select file objects
Okay.. I am making a web base application,that will be connected to a sms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.