Skip to content

nabildridi/PrimengTableSpringFilter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project is a rewrite of my other archived project 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

Step 1 : Add the dependency to your project

    <dependency>    
	    <groupId>io.github.nabildridi</groupId>    
	    <artifactId>PrimengTableSpringFilter</artifactId>    
	    <version>1.3</version>    
    </dependency>

Step 2 : Configure your Primeng table to work in 'lazy' mode

How-to documentation

Step 3 : Send the Primeng request to the server

      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 :

    @Repository
    public  interface UsersRepository extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

Step 5 : Write your Spring boot controller like this :

    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<User> paginate(@Filter Specification<User> spec, @Pagination Pageable page) {  
		    return  usersRepository.findAll(spec, page);    
	    }
    }

The Specification and Pageable objects are automatically generated by 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 :

    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

@EnableSpringDataWebSupport(pageSerializationMode = PageSerializationMode.VIA_DTO)

That annotation generates a problem in the sort parameter of 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

About

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

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages