Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of Specification<T> additionalSpecification #5

Closed
jcworklooks opened this issue Feb 24, 2016 · 2 comments
Closed

Example of Specification<T> additionalSpecification #5

jcworklooks opened this issue Feb 24, 2016 · 2 comments

Comments

@jcworklooks
Copy link

Hi,
First of all, thanks for wonderful spring based solution for datatables.

Can you please provide an example for below situation.
Suppose I have OnetoMany relation in Employer/Employee.
What if I want to show all employees related to one employer only using employer id?
How do I configure Specification additionalSpecification with Employer id as PathVariable/Get Param? for example /abc/employees/{employerId} or /abc/employees?id=12312
Can we set employer Id each time request is sent to server in case of search and pagination?
I have many such cases in which a datatable need to be shown on page load.

Thanks in advance.

@darrachequesne
Copy link
Owner

Hi! The simplest way to achieve what you describe would be (I think) to add a hidden column, something like that:

$('#example').dataTable( {
  'columns': [
    { 
       data: 'employer.id',
       name: 'employerId',
       visible: false
    },
    ...
  ]
} );

and then apply your filter on that hidden column. Of course, in that case, the client has the control over the employerId sent to the server. If you want to apply it on the server-side:

public static Specification<Employee> withEmployerId(Integer employerId) {
    return (root, query, criteriaBuilder) -> {
        return criteriaBuilder.equals(
            root.join(Employee_.employer).get(Employer_.id), 
            employerId);
    };
}
// not tested, but that should work!

@darrachequesne
Copy link
Owner

Closing this now, please reopen if it didn't solve your issue. 👼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants