Giter Club home page Giter Club logo

springbootgenericpagingfilteringforprimengtable's Introduction

Spring Boot generic paging, sorting and filtering for PrimeNg tables (V 4.3)

Changes in version 4.3 (19/10/2022)

  • Maven Java version to 18
  • Removed the use of mysql from this demo application, instead it uses in memory H2 database now
  • Sample data is in resources/data.sql file
  • Spring Boot to 2.7.4 version
  • Angular to 14.2.0 version
  • Primeng to 14.1.2 version

Changes in version 4.2.1 (16/08/2022)

  • Processing of the boolean type filter
  • Changes in the test database and the test java model

fix

Changes in version 4.2 (19/06/2022)

  • Angular to 13.3.0 version
  • Primeng to 13.4.0 version

Changes in version 4.1 (20/04/2021)

  • Processing of the boolean type filter
  • Changes in the test database and the test java model

Changes in version 4.0

  • Primeng 11 came with many changes in the table component : a date filter was added and the text filter can now have multiple rules, this version take account of those changes
  • This project is compatible with the table component of both v10 and v11 of Primeng

Goal of the project

PrimeNg tables have a 'lazy' mode when displaying data, it sends all the requests of paging, sorting and filtering to the server to be processed. The goal of the this project is to make this server side processing the most generic possible (Spring boot).

Structure of the project

  • The 'ng' folder contains the sample front-end Angular 11 project
  • The Spring Boot project is a minimal showcase and can be used as a base for other projects, it contains : a sample entity, dao, controller and service and two core classes responsible of building queries

How it works

The idea is to make an utility class that parse and convert a PrimeNg json request to a paging and sorting query and build an RSQL query for the columns and general filters, this rsql query will then converted to Jpa specification with rsql-jpa-specification and executed against the dao.

Core classes and initial setup

The two most important java classes in this project are :

  • org.nd.primeng.search.PrimengRequestData : a bean to hold the data parsed from the PrimeNg table request
  • org.nd.primeng.search.SearchBuilder : responsible for parsing the PrimeNg table json request, generating the paging and sorting jpa query and building an Rsql query from the filters
  • Please refer to the class org.nd.primeng.services.UserService for an example to how to use those classes
  • Your repository class needs to extends JpaSpecificationExecutor and QuerydslPredicateExecutor, please refer to UsersDao class for an example

This project uses rsql-jpa-specification to work, please refer to its documentation to see how the intial setup is done.

Two possible methods of use

  • The simplest form : you can use the generated specification with your repository, example :
return usersRepository.findAll((Specification<User>) queries.getSpec(), queries.getPageQuery());
  • Advanced Method : if you need to add a condition to the RSQL query before execution you can use this form, example :
customQuery = queries.getRsqlQuery() + " and name=='John'"
return usersRepository.findAll(RSQLJPASupport.<User>toSpecification(customQuery).and(RSQLJPASupport.toSort(queries.getSortQuery())), queries.getPageQuery());

Very important notes about dates filtering

In order to properly filter against date columns, you need to do two things :

  • Never use java.util.Date or any other java date types as type in your entity classes, use only java.time.LocalDateTime

  • You need to properly setup your timezone in the jvm with the parameter -Duser.timezone, example :

    -Duser.timezone=Europe/Paris

Run the project

  • Create a database in mysql with name : app_db
  • Execute the sample data sql file /db/sample-data.sql againt the database
  • Set your timezone in pom.xml in order to the date filtering to work properly :
<configuration>
	    <jvmArguments>
		 -Duser.timezone=Europe/Paris
	 </jvmArguments>
</configuration>
  • Run the Spring boot project
  • Run the Angular 11 project

springbootgenericpagingfilteringforprimengtable's People

Contributors

nabildridi avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.