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.
One solution is available at http://jsfiddle.net/CrossEye/eKApZ/.
The basic idea is this:
You could then create a pagination function like this:
And call it for, say, page 3, with page size 50, like this:
to get a result that looks like this:
which you could clearly use to generate your AJAX calls.
If you wanted to, it would be trivial to move the
pageSizeparameter into the initial call. That’s less flexible, but might be slightly easier to use. To change this, just move thepageSizeparameter from this line:to this one:
(It should probably be the first parameter here, as it might get lost in the shuffle if you pass an object literal for
itemCountsthe way my sample does.)The other thing to note is that the function returns a signal object named
OUT_OF_BOUNDSto 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
maputility function. You could use any decentmapimplementation, including the native one in recent browsers.)Best of luck!