Comments (4)
Hi, yeah thats a posibility but then you lost the purpose of global search, i ended up making a query to change the nls_date_format previous to the findAll query so the cast to varchar have the required format, i looked at the object sent by datatable frontend ajax and it doesnt contain the column type, so the library cant know the type of specific column to address this issue
from spring-data-jpa-datatables.
Hi! I think a custom specification should do the trick:
class DateSpecification implements Specification<MyEntity> {
private final LocalDate value;
DateSpecification(Column column) {
String value = column.getSearch().getValue();
column.setSearchable(false); // either here or in the table definition
this.value = parseValue(value);
}
private LocalDate parseValue(String value) {
if (hasText(value)) {
try {
return LocalDate.parse(value);
} catch (DateTimeParseException e) {
return null;
}
}
return null;
}
@Override
public Predicate toPredicate(Root<MyEntity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
Expression<LocalDate> expr = root.get("myColumn").as(LocalDate.class);
if (this.value != null) {
return criteriaBuilder.equal(expr, this.value);
} else {
return criteriaBuilder.conjunction();
}
}
}
And then:
@RestController
public class MyController {
@RequestMapping(value = "/entities", method = RequestMethod.GET)
public DataTablesOutput<MyEntity> list(@Valid DataTablesInput input) {
return myRepository.findAll(input, new DateSpecification(input.getColumn("myField")));
}
}
Reference: https://github.com/darrachequesne/spring-data-jpa-datatables/blob/main/README.md#specific-date
from spring-data-jpa-datatables.
Hi, I tried that, but it doesn't work (or I did something wrong) what I found is that custom specification are added after the ones added by global search input made by GlobalFilter class.
What I did was set searchable: false
in datatable initialization, then created the specification and pass it to findAll method but as I said early this specification is added in the second block of the generated query.
My datatable have only two columns, one is a string and the other one a Date, the generated query looked like this initially
SELECT COLUMN1, COLUMN2
FROM TABLE
WHERE
(
COLUMN1 CASTED TO VARCHAR2 LIKE FROM GLOBAL SEARCH VALUE
OR
COLUMN2(DATE) CASTED TO VARCHAR2 LIKE FROM GLOBAL SEARCH VALUE
)
AND
(
CUSTOM SPECIFICATION
)
So when i did what you suggested i removed the second like in the first block of query and added on the second one but since the first block of where is only 1 column and none match a date as string pattern (dd/MM/yyyy) the count query return 0 so my specification dont do anything
from spring-data-jpa-datatables.
Oh, I see, so the date in the global search field, right? In that case, you'll need to clear the global search value, and only apply the specification:
@RestController
public class MyController {
@RequestMapping(value = "/entities", method = RequestMethod.GET)
public DataTablesOutput<MyEntity> list(@Valid DataTablesInput input) {
LocalDate date = parseDate(input.getSearch().getValue());
if (date != null) {
input.getSearch().setValue("");
return myRepository.findAll(input, (root, query, criteriaBuilder) -> {
return criteriaBuilder.equal(root.get("myColumn").as(LocalDate.class), date);
});
}
return myRepository.findAll(input);
}
private LocalDate parseDate(String value) {
try {
return LocalDate.parse(value);
} catch (DateTimeParseException e) {
return null;
}
}
}
from spring-data-jpa-datatables.
Related Issues (20)
- How to Work with UserDTO HOT 2
- Can we search date in data table? HOT 2
- Duplicates items after filtering HOT 2
- Log Exceptions or they are silently lost HOT 4
- Add support for Spring Boot 3.0.0 HOT 3
- Is it possible to use specification for view? HOT 2
- Is it possible to do a regular expression search using ~ in PostgreSQL? HOT 2
- AddOrder doesnt seem to work HOT 2
- Filtering data sometimes throws ORM exception (Hibernate 6.1) HOT 10
- No property 'findAll' found for type 'Class' HOT 4
- Performance issue (suggestions on database indexes) HOT 7
- [question] specifications and counting total records HOT 4
- Show ManyToMany data HOT 1
- enable datatable repositories without using DatatablesJpaRepositoryBean HOT 1
- Order can be null HOT 4
- Searching for a record with special (national) character "ř" doesn't work HOT 5
- Search Panes Error in Relationship Table HOT 3
- Library Source does not match the bytecode HOT 2
- Extend JpaRepository instead of PagingAndSortingRepository HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spring-data-jpa-datatables.