way(client side or server side) to go for pagination /sortable columns?

前端 未结 4 1078
梦如初夏
梦如初夏 2021-02-02 01:34

I have 3000 records in an employee table which I have fetched from my database with a single query. I can show 20 records per page. So there will be 150 pages for with each page

相关标签:
4条回答
  • 2021-02-02 02:00

    Sending data to your client is almost certainly going to your bottleneck (especially for mobile clients), so you should always strive to send as little data as possible. With that said, it is almost definitely better to do your pagination on the server side. This is a much more scalable solution. It is likely that the amount of data will grow, so it's a safer bet for the future to just do the pagination on the server.

    Also, remember that it is fairly unlikely that any user will actually bother looking through hundreds of result pages, so transferring all the data is likely wasteful as well. This may be a relevant read for you.

    0 讨论(0)
  • 2021-02-02 02:04

    USE SERVER PAGINATION!

    Sure, you could probably get away with sending down a JSON array of 3000 elements and using JavaScript to page/sort on the client. But a good web programmer should know how to page and sort records on the server. (They should really know a couple ways). So, think of it as good practice :)

    If you want a slick user interface, consider using a JavaScript grid component that uses AJAX to fetch data. Typically, these components pass back the following parameters (or some variant of them):

    • Start Record Index
    • Number of Records to Return
    • Sort Column
    • Sort Direction
    • Columns to Fetch (sometimes)

    It is up to the developer to implement a handler or interface that returns a result set based on these input parameters.

    0 讨论(0)
  • 2021-02-02 02:15

    I know this doesn't directly answer the client vs server side pagination, but I would recommend using DataTables.net to both display and paginate your data. It provides a very nice display, allows for sorting and pagination, built in search function, and a lot more. The first time I used it was for the first web project I worked on, and I, as a complete noobie, was able to get it to work. The forums also provide very good information/help, and the creator will answer your questions. DataTables can be used both client-side and server-side, and can support thousands of rows. As for speed, I only had a few hundred rows, but used the client-side processing and never noticed a delay.

    0 讨论(0)
  • 2021-02-02 02:17

    I assume you have a bean class representing records in this table, with instances loaded from whatever ORM you have in place.

    If you haven't already, you should implement caching of these beans in your application. This can be done locally, perhaps using Guava's CacheBuilder, or remotely using calls to Memcached for example (the latter would be necessary for multiple app servers/load balancing). The cache for these beans should be keyed on a unique id, most likely the mapping to the primary key column of the corresponding table.

    Getting to the pagination: simply write your queries to return only IDs of the selected records. Include LIMIT and OFFSET or your DB language's equivalent to paginate. The caller of the query can also filter/sort at will using WHERE, ORDER BY etc.

    Back in the Java layer, iterate through the resulting IDs of these queries and build your List of beans by calling the cache. If the cache misses, it will call your ORM to individually query and load that bean. Once the List is built, it can be processed/serialized and sent to the UI.

    0 讨论(0)
提交回复
热议问题