Giter Club home page Giter Club logo

springbootgenericpagingfilteringforprimengtable's Introduction

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

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).


Flow of work

It's super easy to work with it :

  1. Collect the json sent by PrimeNg table in your controller
  2. Send this json to the SearchBuilder class
  3. With the object returned from SearchBuilder call you dao

Structure of the project

  • The 'ng' folder contains the sample front-end Angular 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, 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

  • 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 project

Versions

Changes in version 5.0 (23/01/2024)

  • Maven Java version to 21
  • Spring Boot to 3.2.2 version
  • Angular to 17.0.0 version
  • Primeng to 17.3.0 version

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

springbootgenericpagingfilteringforprimengtable's People

Contributors

nabildridi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

springbootgenericpagingfilteringforprimengtable's Issues

Exception when space characters are included

When searching for something with a space in it, the following Exception is thrown

2020-12-10 23:42:26.994 ERROR 30620 --- [nio-8000-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is cz.jirutka.rsql.parser.RSQLParserException: cz.jirutka.rsql.parser.ParseException: Encountered " <UNRESERVED_STR> "d* "" at line 1, column 20.
Was expecting one of:
    <EOF> 
    <AND> ...
    <OR> ...
    ] with root cause

cz.jirutka.rsql.parser.ParseException: Encountered " <UNRESERVED_STR> "d* "" at line 1, column 20.
Was expecting one of:
    <EOF> 
    <AND> ...
    <OR> ...
    
	at cz.jirutka.rsql.parser.Parser.generateParseException(Parser.java:384) ~[rsql-parser-2.1.0.jar:2.1.0]
	at cz.jirutka.rsql.parser.Parser.jj_consume_token(Parser.java:322) ~[rsql-parser-2.1.0.jar:2.1.0]
	at cz.jirutka.rsql.parser.Parser.Input(Parser.java:39) ~[rsql-parser-2.1.0.jar:2.1.0]
	at cz.jirutka.rsql.parser.RSQLParser.parse(RSQLParser.java:120) ~[rsql-parser-2.1.0.jar:2.1.0]
	at io.github.perplexhub.rsql.RSQLJPASupport$1.toPredicate(RSQLJPASupport.java:89) ~[rsql-jpa-5.0.6.jar:na]
	at org.springframework.data.jpa.domain.SpecificationComposition.toPredicate(SpecificationComposition.java:60) ~[spring-data-jpa-2.3.3.RELEASE.jar:2.3.3.RELEASE]
	at ........

Returns empty array when using p-multiSelect

When using p-multiselect from primeng for filtering it sends values as an array. Because of that, it returns an empty array.

sending: "field":{"value":["value1","value2","value3"],"matchMode":"equals"}
returns: []

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.