Giter Club home page Giter Club logo

Comments (4)

perfectime94 avatar perfectime94 commented on September 20, 2024 1

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.

darrachequesne avatar darrachequesne commented on September 20, 2024

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.

perfectime94 avatar perfectime94 commented on September 20, 2024

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.

darrachequesne avatar darrachequesne commented on September 20, 2024

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)

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.