Hi everyone, is there a ready to use solution for filtering a datagrid where the filter are in header of the datagrid? In the docs is mentioned to use <column property="test" editable="true" filterable="true" />
but i prefer a solution where there is already a text field (without click button ). Thanks
Hi!
You can combine the approach provided in the Jmix docs with the method provided in the Vaadin docs: Grid component | Vaadin components.
Possible implementation for standard User
entity:
@ViewComponent
private DataGrid<User> usersDataGrid;
@ViewComponent
private CollectionLoader<User> usersDl;
@Autowired
private UiComponents uiComponents;
@Subscribe
public void onInit(InitEvent event) {
HeaderRow headerRow = usersDataGrid.appendHeaderRow();
addPropertyFilterToColumn("username", headerRow);
addPropertyFilterToColumn("firstName", headerRow);
}
private void addPropertyFilterToColumn(String columnKey, HeaderRow headerRow) {
PropertyFilter<User> propertyFilter = uiComponents.create(PropertyFilter.class);
propertyFilter.setProperty(columnKey);
propertyFilter.setDataLoader(usersDl);
propertyFilter.setOperation(PropertyFilter.Operation.CONTAINS);
propertyFilter.setParameterName(PropertyConditionUtils.generateParameterName(columnKey));
propertyFilter.setOperationEditable(false);
propertyFilter.setOperationTextVisible(false);
propertyFilter.setLabelVisible(false);
headerRow.getCell(usersDataGrid.getColumnByKey(columnKey))
.setComponent(propertyFilter);
}
Best regards,
Dmitriy
Thanks for the reply, your example works well, documenting myself better in the documentation I saw an example https://demo.jmix.io/ui-samples/sample/property-filter-custom-field?tab=property-filter-custom-field.xml that allows you to customize the filter ui. Can it be done programmatically?
Of course, you can use propertyFilter.setValueComponent()
method for this.