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

Date input search with problem #59

Closed
MatheusCaiser opened this issue Jul 8, 2017 · 5 comments
Closed

Date input search with problem #59

MatheusCaiser opened this issue Jul 8, 2017 · 5 comments

Comments

@MatheusCaiser
Copy link

Hi,
firstly I congrates you for a great job that you make, I search something like this.

But when I search on the function column(0).search(valueDate).draw() the Query search parameter is a varchar type on the date attribute. This result in nothing.

In debug log of hibernate the query is setting like this:
from movimento movimento0_ where 1=1 and (lower(cast(movimento0_.data as varchar(255))) like ? escape ?) order by movimento0_.data asc limit ?

the parameter are:
binding parameter [1] as [VARCHAR] - [%02/07/2017%]
binding parameter [2] as [CHAR] - []

Do you have searched a date type already?

@darrachequesne
Copy link
Owner

To handle a date, you'll have to provide your own specification (since the library cannot know whether a field contains a date or not):

public DataTablesOutput<User> getUsers(@Valid DataTablesInput input) {
  ColumnParameter parameter0 = input.getColumns().get(0);
  LocalDate date = LocalDate.parse(parameter0.getSearch().getValue());
  Specification additionalSpecification = getSpecificationForDate(date);
  parameter0.getSearch().setValue("");
  return userRepository.findAll(input, additionalSpecification);
}

@MatheusCaiser
Copy link
Author

MatheusCaiser commented Jul 10, 2017

I figure out how to make a date search, I'm using Postgres and the date type is stored like '2017-01-01 00:00:00', so what I made was to parse date to a date string that have the same format, but in JavaScript. Finally my JS function ended up like this:
$("#dataFilter").datepicker({ format : "dd/mm/yyyy", language : "pt-BR", todayHighlight : true }) .on("changeDate", function(e) { table.column(0).search(e.date.toISOString().substr(0, 10)).draw(); });

And Worked 👍

@Mike-the-one
Copy link

Sorry to bring the old thread back. I am having a similar problem. Setting the date between returns empty table.

Here is the code

predicateDate = builder.between(root.<Date>get("orderDateTime"), d1, d2);

@hendisantika
Copy link

Hi @darrachequesne , where is ColumnParameter class imported?

@JsonView(DataTablesOutput.View.class)
	@RequestMapping(value = "/data/users", method = RequestMethod.GET)
	public DataTablesOutput<User> getUsers(@Valid DataTablesInput input) {
		for (ColumnParameter parameter : input.getColumns()) {
			if (!parameter.getData().equalsIgnoreCase("likes"))
				continue;
			// create a specification based on the search value
			val additionalSpecification = getLikesSpecification(parameter
					.getSearch().getValue());
			// remove the search value
			parameter.getSearch().setValue("");
			return userRepository.findAll(input, additionalSpecification);
		}
		return userRepository.findAll(input);
	}

Thanks

@carlosluisbm
Copy link

Hi @darrachequesne , where is ColumnParameter class imported?

@JsonView(DataTablesOutput.View.class)
	@RequestMapping(value = "/data/users", method = RequestMethod.GET)
	public DataTablesOutput<User> getUsers(@Valid DataTablesInput input) {
		for (ColumnParameter parameter : input.getColumns()) {
			if (!parameter.getData().equalsIgnoreCase("likes"))
				continue;
			// create a specification based on the search value
			val additionalSpecification = getLikesSpecification(parameter
					.getSearch().getValue());
			// remove the search value
			parameter.getSearch().setValue("");
			return userRepository.findAll(input, additionalSpecification);
		}
		return userRepository.findAll(input);
	}

Thanks

I think the class Column works.

Hi @darrachequesne, I have this same problem. A column in BD that is a Date that cant be search by the value of the input.

Im trying to work on the code for the Specification to solve this, but I cant.

Can you or anybody else write or explain the code inside the method getSpecificationForDate(date)?

Thanks.

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

5 participants