An open API service indexing awesome lists of open source software.

https://github.com/nabildridi/primengtablespringfilter

Convert Primeng table json request (pagination, sorting and filtering) to a JPA specification
https://github.com/nabildridi/primengtablespringfilter

java primeng primeng-table spring-boot spring-data-jpa spring-data-jpa-specification spring-jpa springfilter turkraft

Last synced: 4 months ago
JSON representation

Convert Primeng table json request (pagination, sorting and filtering) to a JPA specification

Awesome Lists containing this project

README

          

> ## This project is a rewrite of my other archived project [SpringBootGenericPagingFilteringForPrimengTable](https://github.com/nabildridi/SpringBootGenericPagingFilteringForPrimengTable) to make it simple and easy to install
>
## Spring Boot filter to convert PrimeNg table json request to JPA specification (paging, sorting and filtering for PrimeNg tables)

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

[![Sonatype Central](https://maven-badges.sml.io/sonatype-central/io.github.nabildridi/PrimengTableSpringFilter/badge.svg?subject=Maven%20Central&color=blue)](https://mvnrepository.com/artifact/io.github.nabildridi/PrimengTableSpringFilter/1.3)

### Step 1 : Add the dependency to your project
```xml

io.github.nabildridi
PrimengTableSpringFilter
1.3

```

### Step 2 : Configure your Primeng table to work in 'lazy' mode
[How-to documentation](https://primeng.org/table#virtual-scroll-lazy)

### Step 3 : Send the Primeng request to the server
```javascript
loadUsersFromServer(event: TableLazyLoadEvent) {
event.globalFilter = ['username', 'lastname'];
this.http.post('http://dev.local/paginateUsers', event).subscribe({
next: (json: any) => {
if (json) {
this.data.set(json['content']);
this.totalRecords = json['totalElements'];
}
},
error: (e) => {
console.log('error');
},
});
}
```
### Step 4 : Add JpaSpecificationExecutor to your repository:

Example :
```java
@Repository
public interface UsersRepository extends JpaRepository, JpaSpecificationExecutor {
```

### Step 5 : Write your Spring boot controller like this :
```java
import org.nd.primeng.model.User;
import org.nd.primeng.repositories.UsersRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import com.turkraft.springfilter.boot.Filter;
import com.turkraft.springfilter.boot.Pagination;

@RestController
public class UsersController {
@Autowired
private UsersRepository usersRepository;

@PostMapping(value = "/paginateUsers")
public Page paginate(@Filter Specification spec, @Pagination Pageable page) {
return usersRepository.findAll(spec, page);
}
}
```
The *Specification* and *Pageable* objects are automatically generated by [Turkraft SpringFilter](https://github.com/turkraft/springfilter) from the data provided by this project after parsing the Primeng json request.

The only thing that you need to do is to call **findAll** function of your target respository.

### What is supported by this project
All the functions of Primeng table are supported :

- Pagination
- Sorting (single sort and multiple sort)
- All types of column filters (multiSelect, number, string, date, boolean and decimal)
- 'filterGlobal' input mode

### How to specify the fields that the global filter will look into?

See step 3, if you want to use the global filter, you need to specify what columns in your JPA entity to look into, for example, if you specify :
```javascript
event.globalFilter = ['username', 'lastname'];
```
Then when the user types 'Fred' in the global filter input, Spring will looks for values containing 'Fred' in **username** and **lastname** columns.

> [!CAUTION]
> ### Important note :
> **Don't** add this annotation to your spring boot application
>
> ```java
> @EnableSpringDataWebSupport(pageSerializationMode = PageSerializationMode.VIA_DTO)
> ```
>
> That annotation generates a problem in the sort parameter of [Turkraft SpringFilter](https://github.com/turkraft/springfilter)

### Does this project work in Spring boot 4.x?
No, it doesn't work because its main dependency doesn't support Spring boot 4.x, as soon as the situation changes then I will update the project.

### Minimum required Java version :
Java 17

### Tested on :

- Spring boot version 3.5.10
- Angular version 21.1.0
- Primeng version 21.0.4