• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Spring MVC Jquery datatable pagination

 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using this example to implement pagination in my project.
Pagination

In the example, data population of table is done using this line in the controller


Here I replace my code for fetching data from table. As the name of the method suggest, they have created pagination. But I don't understand how they do it. Can some one please explain?

 
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

It usually adapts to the database based on the jdbc driver used.

With mysql it will usually translate into a request that looks like this:

https://dev.mysql.com/doc/refman/5.0/en/select.html


The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15

To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

SELECT * FROM tbl LIMIT 95,18446744073709551615;

With one argument, the value specifies the number of rows to return from the beginning of the result set:

SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows

In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.



an additional:
select count(*) from tbl;

is usually also used to get the total number of result available.

I hope I understood your question correctly ;-)
 
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just gave a quick look to the example, but basically it's telling you how to do pagination on the client side, not how to retrieve paginated results from a database.
If I were you I would...
1- use Spring Boot instead of Spring
2- use thymeleaf rather than jsp
3- use Spring Data

When using Spring Data you can make your repositories support the Pageable class and then you just need to use it in your controllers. I don't have enough time to explain all of it, but i can show you a few fragments of my code and then you could study the docs.

Controller:


To make it short: Spring Data repositories give you out of the box support for the Pageable class, which tells them what kind of LIMIT clause to use with the database. They will also do a count for you to retrieve the total number of pages.
On the controller side, by putting Pageable in the method signature, Spring will search for HTTP params called "size" and "page".

For example: www.mysite.com/users?page=1&size=10

will tell Spring to fetch the second page (it's 0-based), considering 10 elements as page size

The UserRepository in my code returns a Page<User> object, which contains info about the total number of pages and a list of users from 11 to 20 in this case.

I can't show you any code for DataTables on the client side because I render the pagination server side with a templating engine, so it's up to you to make this work.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté and Martin Bechtle,
Thanks for the reply.

A.J. Côté wrote:
an additional:
select count(*) from tbl;

is usually also used to get the total number of result available.



I don't have problems in receiving the server side data. Just wanted to know how pagination is done here.

Martin Bechtle wrote:
Basically it's telling you how to do pagination on the client side.



That is what I don't understand in it. He just loops 5 data once and then at last he again loops data pageDisplayLength-5 times. How does it get paginated?

Martin Bechtle wrote:
If I were you I would...
1- use Spring Boot instead of Spring
2- use thymeleaf rather than jsp
3- use Spring Data



I am working on getting to know about Spring Boot and thymeleaf and in my actual project I am using Spring Data and I know about Page classes there. I am just wondering if its simple as looping will paginate your data, why should we use Spring Data and Page classes.
 
Martin Bechtle
Ranch Hand
Posts: 46
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:
That is what I don't understand in it. He just loops 5 data once and then at last he again loops data pageDisplayLength-5 times. How does it get paginated?



Well that's the point... it is not getting paginated in that method. He's just generating fake data to simulate a DB. You can also see that in the comments from other confused people. It is really paginated by jQuery on the client side, once the data is fetched from the server

Partheban Udayakumar wrote:
I am working on getting to know about Spring Boot and thymeleaf and in my actual project I am using Spring Data and I know about Page classes there. I am just wondering if its simple as looping will paginate your data, why should we use Spring Data and Page classes.



If you don't use the Page class, you need to manually add limit and offset to your queries. Should be quite easy with JPA. And you need to do a count query before the real one.
 
Partheban Udayakumar
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Bechtle,

Martin Bechtle wrote:It is really paginated by jQuery on the client side, once the data is fetched from the server



So either we will have to use, jquery to paginate the data or it should be paginated server side. Did I get that right?
 
Villains always have antidotes. They're funny that way. Here's an antidote disquised as a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!