Пытаюсь создать форму фильтров:
Предпоследняя - строка в фильтрации input , если в нее вписать "true" , и отправить запрос, то таблица будет содержать только проданные машины, если false то наоборот, если пусто - то все машины.
Пытаюсь создать форму с заранее выбранными опциями (Последняя строка в фильтрации) Но не врубаюсь как...
Вот код :
<div class="container">
<div class="row">
<div class="col-sm-4">
<br>
<h3>Filtration</h3>
<form th:action="@{/management}" th:object="${param.sold}" method="get">
<div class="form-group">
<input th:value="${param.brand != null} ? ${param.brand}" name="brand" placeholder="Brand"
class="form-control form-control-sm mr-sm-2">
</div>
<div class="form-group">
<input th:value="${param.model != null} ? ${param.model}" name="model" placeholder="Model"
class="form-control form-control-sm mr-sm-2">
</div>
<div class="form-group">
<input th:value="${param.minYear != null} ? ${param.minYear}" name="minYear"
placeholder="Min Year"
class="form-control form-control-sm mr-sm-2">
</div>
<div class="form-group">
<input th:value="${param.maxYear != null} ? ${param.maxYear}" name="maxYear"
placeholder="Max Year"
class="form-control form-control-sm mr-sm-2">
</div>
<div class="form-group">
<input th:value="${param.minMileage != null} ? ${param.minMileage}" name="minMileage"
placeholder="Min Mileage"
class="form-control form-control-sm mr-sm-2">
</div>
<div class="form-group">
<input th:value="${param.maxMileage != null} ? ${param.maxMileage}" name="maxMileage"
placeholder="Max Mileage"
class="form-control form-control-sm mr-sm-2">
</div>
<div class="form-group">
<input th:value="${param.minPrice != null} ? ${param.minPrice}" name="minPrice"
placeholder="Min Price"
class="form-control form-control-sm mr-sm-2">
</div>
<div class="form-group">
<input th:value="${param.maxPrice != null} ? ${param.maxPrice}" name="maxPrice"
placeholder="Max Price"
class="form-control form-control-sm mr-sm-2">
</div>
<div class="form-group">
<input th:value="${param.sold != null} ? ${param.sold}" name="sold"
placeholder="Sold"
class="form-control form-control-sm mr-sm-2">
</div>
<div class="form-group">
<select th:field="${param.sold}" >
<option th:value="''">All</option>
<option th:value="true">Sold</option>
<option th:value="false">No sold</option>
</select>
</div>
<button type="submit" class="btn btn-outline-success">Filter</button>
</form>
</div>
Также добавлю остальной код, вдруг поможет :
Контроллер:
@GetMapping()
public String index(Model model
, @AuthenticationPrincipal SecurityUser user
, @RequestParam Map<String, String> requestParams
) {
Integer pageNumber = Integer.parseInt(requestParams.getOrDefault("page", "1"));
CarFilter carFilter = new CarFilter(requestParams);
model.addAttribute("cars", carService.getCarWitPagingAndFiltering(carFilter.getSpec(), pageNumber));
model.addAttribute("filterDef", carFilter.getFilterDefinition().toString());
model.addAttribute("user", user.toString());
return "management/index";
}
CarSpec.java
public class CarSpec {
public static Specification<Car> brandContains(String filterBrand) {
return (root, criteriaQuery, criteriaBuilder)
-> criteriaBuilder.like(root.get("brand"), "%" + filterBrand + "%");
}
public static Specification<Car> modelContains(String filterModel) {
return (root, criteriaQuery, criteriaBuilder)
-> criteriaBuilder.like(root.get("model"), "%" + filterModel + "%");
}
public static Specification<Car> mileageLesserThanOrEq(Integer value) {
return (root, criteriaQuery, criteriaBuilder)
-> criteriaBuilder.lessThanOrEqualTo(root.get("mileage"), value);
}
public static Specification<Car> mileageGreaterThanOrEq(Integer value) {
return (root, criteriaQuery, criteriaBuilder)
-> criteriaBuilder.greaterThanOrEqualTo(root.get("mileage"), value);
}
public static Specification<Car> yearLesserThanOrEq(Integer value) {
return (root, criteriaQuery, criteriaBuilder)
-> criteriaBuilder.lessThanOrEqualTo(root.get("year"), value);
}
public static Specification<Car> yearGreaterThanOrEq(Integer value) {
return (root, criteriaQuery, criteriaBuilder)
-> criteriaBuilder.greaterThanOrEqualTo(root.get("year"), value);
}
public static Specification<Car> priceLesserThanOrEq(Integer value) {
return (root, criteriaQuery, criteriaBuilder)
-> criteriaBuilder.lessThanOrEqualTo(root.get("price"), value);
}
public static Specification<Car> priceGreaterThanOrEq(Integer value) {
return (root, criteriaQuery, criteriaBuilder)
-> criteriaBuilder.greaterThanOrEqualTo(root.get("price"), value);
}
public static Specification<Car> soldContains(String filterSold) {
Boolean value = Boolean.parseBoolean(filterSold);
return (root, criteriaQuery, criteriaBuilder)
-> criteriaBuilder.equal(root.get("sold"), value);
}
}
CarServiceImpl.java
@Override
public Page<Car> getCarWitPagingAndFiltering(Specification<Car> specification, Integer page) {
if (page < 1) {
page = 1;
}
return carRepository.findAll(specification, PageRequest.of(page - 1, 10));
}
CarFilter.java
@Getter
public class CarFilter {
private Specification<Car> spec;
private StringBuilder filterDefinition;
public CarFilter(Map<String, String> map) {
this.spec = Specification.where(null);
this.filterDefinition = new StringBuilder();
if (map.containsKey("brand") && !map.get("brand").isEmpty()) {
String brand = map.get("brand");
spec = spec.and((CarSpec.brandContains(brand)));
filterDefinition.append("&brand=").append(brand);
}
if (map.containsKey("model") && !map.get("model").isEmpty()) {
String model = map.get("model");
spec = spec.and((CarSpec.modelContains(model)));
filterDefinition.append("&model=").append(model);
}
if (map.containsKey("minMileage") && !map.get("minMileage").isEmpty()) {
int minMileage = Integer.parseInt(map.get("minMileage"));
spec = spec.and(CarSpec.mileageGreaterThanOrEq(minMileage));
filterDefinition.append("&minMileage=").append(minMileage);
}
if (map.containsKey("maxMileage") && !map.get("maxMileage").isEmpty()) {
int maxMileage = Integer.parseInt(map.get("maxMileage"));
spec = spec.and(CarSpec.mileageLesserThanOrEq(maxMileage));
filterDefinition.append("&maxMileage=").append(maxMileage);
}
if (map.containsKey("minYear") && !map.get("minYear").isEmpty()) {
int minYear = Integer.parseInt(map.get("minYear"));
spec = spec.and(CarSpec.yearGreaterThanOrEq(minYear));
filterDefinition.append("&minYear=").append(minYear);
}
if (map.containsKey("maxYear") && !map.get("maxYear").isEmpty()) {
int maxYear = Integer.parseInt(map.get("maxYear"));
spec = spec.and(CarSpec.yearLesserThanOrEq(maxYear));
filterDefinition.append("&maxYear=").append(maxYear);
}
if (map.containsKey("minPrice") && !map.get("minPrice").isEmpty()) {
int minPrice = Integer.parseInt(map.get("minPrice"));
spec = spec.and(CarSpec.priceGreaterThanOrEq(minPrice));
filterDefinition.append("&minPrice=").append(minPrice);
}
if (map.containsKey("maxPrice") && !map.get("maxPrice").isEmpty()) {
int maxPrice = Integer.parseInt(map.get("maxPrice"));
spec = spec.and(CarSpec.priceLesserThanOrEq(maxPrice));
filterDefinition.append("&maxPrice=").append(maxPrice);
}
if (map.containsKey("sold") && !map.get("sold").isEmpty()) {
String sold = map.get("sold");
spec = spec.and((CarSpec.soldContains(sold)));
filterDefinition.append("&sold=").append(sold);
}
}
}
Помогите)