Changing standard behaviour of filter component

Hello,

Is it possible to access properties or methods of subcomponents contained in the standard cuba filter component?
E.g. I have the following request: when a user changes the show-rows dropdown (from 50 to 100 rows let’s assume) the table should refresh instantly, avoiding to press search button.
I tried to debug and I managed to view the components, eventually I saw the weblookupfield with the dropdown. But I failed when trying to access the subcomponent in the code. I hoped I could add a ValueChangedListener to it…
Any solution, if it’s not too complicated?

Thanks.

I think I found it, it wasn’t easy :). On the init method of my screen:


        // filter1 is my filter component
        CubaCssActionsLayout cubaCssActionsLayout = (CubaCssActionsLayout) WebComponentsHelper.unwrap(filter1);
        CubaGroupBox cubaGroupBox = (CubaGroupBox)cubaCssActionsLayout.getComponent(0);
        CubaVerticalActionsLayout cubaVerticalActionsLayout = (CubaVerticalActionsLayout)cubaGroupBox.getContent();
        CubaVerticalActionsLayout cubaVerticalActionsLayout1 =(CubaVerticalActionsLayout)cubaVerticalActionsLayout.getComponent(0);
        CubaHorizontalActionsLayout cubaHorizontalActionsLayout = (CubaHorizontalActionsLayout)cubaVerticalActionsLayout1.getComponent(1);
        CubaHorizontalActionsLayout cubaHorizontalActionsLayout1 = (CubaHorizontalActionsLayout)cubaHorizontalActionsLayout.getComponent(4);
        Component comp = cubaHorizontalActionsLayout1.getComponent(1);

      comp.addListener(new Component.Listener() {
          @Override
          public void componentEvent(Component.Event event) {
              if (event.getClass().getName() == "com.vaadin.ui.Field$ValueChangeEvent" && event.getSource().getClass().getName() == "com.haulmont.cuba.web.gui.components.WebLookupField$2") {
                  myTableDatasourceDs.refresh();
              }
          }
      });

It works.

You solution may stop working if we do any minor modification in filter component internal structure. Your task can be solved in a different way. There is a demo project attached - take a look.

  1. Override the MaxResultsFieldHelper bean. The new version will set an explicit id to the required combo box.

public class ExtMaxResultsFieldHelper extends MaxResultsFieldHelper {
    @Override
    public LookupField createMaxResultsLookupField() {
        LookupField maxResultsLookupField = super.createMaxResultsLookupField();
        CubaComboBox cubaComboBox = (CubaComboBox) WebComponentsHelper.unwrap(maxResultsLookupField);
        cubaComboBox.setId("maxResultsLookupField");
        return maxResultsLookupField;
    }
}
  1. Register the new bean in the web-spring.xml

<bean id="cuba_MaxResultsFieldHelper" class="com.company.demo.web.filter.ExtMaxResultsFieldHelper"/>
  1. In the screen controller find the comboBox with required id

public class BookBrowse extends AbstractLookup {
    @Inject
    private Filter filter;

    @Inject
    private GroupDatasource<Book, UUID> booksDs;

    @Override
    public void ready() {
        super.ready();
        HasComponents composition = (HasComponents) WebComponentsHelper.getComposition(filter);
        CubaComboBox maxResultsComboBox = WebComponentsHelper.getComponents(composition, CubaComboBox.class)
                .stream()
                .filter(cubaComboBox -> "maxResultsLookupField".equals(cubaComboBox.getId()))
                .findAny()
                .orElse(null);

        if (maxResultsComboBox != null) {
            maxResultsComboBox.addValueChangeListener(event -> {
                booksDs.refresh();
            });
        }
    }
}

filter-max-results-listener-demo.zip (34.4K)

Thanks,

It’s working with a minor update:
Before

booksDs.refresh();

we have to write:

booksDs.setMaxResults((Integer)maxResultsComboBox.getValue());