javascriptangularjssortingpaginationodata

How to best perform paging and sorting from multiple sources in JavaScript mashup


I am in the process of building an Angular application that is fetching data with similar schema from multiple sources. The actual fetching of the data and presenting it is simple enough. I build all of the requests into an array, feed the array to $q and then resolve the promises returning all of the data.

My issue is with server-based paging and sorting. Right now I am just making the page size 5 * n where n is simply the number of data sources. But if someone then sorts by Created or something similar I can sometimes end up with unexpected results as a source with only 5 items will all appear on the first page regardless of the date on which they were created. So the first page could be 20 items from January, 2016 and then the final 5 items all being from May, 2015. If I then advance to the next page, I'd have expected results until I get into the entries for May 2015 for the larger data sources. Then those May items from the smaller list will not be shown.

Ideas? Suggestions? If the answer to this is that I am being stupid and should do this in another way, I am open to that.

Currently all the data sources are relatively small so I simply return all the items and do all of my paging and sorting in the UI. This will not likely be sustainable in the future as they grow.

For clarity, this is the SharePoint 2010 ListData.svc odata service. But this is not really a SharePoint specific question.


Solution

  • If you have multiple data sources, it is not possible to do paging with sorting without fetching all data up-to the point where you page, for example

    Source 1
    Name 
    A
    B
    C
    D
    E
    F
    
    Source 2
    Name
    P
    Q
    R
    S
    T
    U
    

    Now, if you want to get the second page (size of 2 * 2) while sorted by Name, the expected result is,

    E
    F
    P
    Q
    

    With your approach you ends up with

    C
    D
    R
    S
    

    Only way you can guarantee to get proper sorting is to fetch all data up to the page you are showing for all sources, combine all, sort and get the required page in your client.