Change default filter condition from controler

Hello,

I want to be able to change a default condition from the controtoler since I am reciving the parameter from the URL(this way I can re-use the screen). Fisrt i see the way of set in the view a condition y from the view, but i would like alse do this by code and not have the need o have this on the db. But then i would like to chage the default value in the request so I can have diferents calls.

Is this even posible?

Hello,

take a look at FIlter#setParamValue(). It will set the value to the filter parameter if it exists.

Note, as JavaDoc says do not use it in InitEvent because the filter is not initialized by that time. ParamName can be found in the filter editor window (documentation).

So the code will look like:

@Inject
private Filter filter;

@Subscribe
public void onBeforeShow(BeforeShowEvent event) {
    filter.setParamValue("count28519", "my_count_value");
}

Thanks, I will try that, but there is posible create this filter condition and show it by defautl usign code?.

Unfortunately, the only way to create conditions for Filter from the controller is by creating FilterEntity. It should contain conditions presented by XML. For instance:

@Inject
private Metadata metadata;
@Inject
private Filter filter;

@Subscribe
public void onBeforeShow(BeforeShowEvent event) {
    FilterEntity filterEntity = metadata.create(FilterEntity.class);
    filterEntity.setComponentId(getId() + "." + filter.getId());
    filterEntity.setXml("" +
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "\n" +
            "<filter>\n" +
            "  <and>\n" +
            "    <c name=\"count\" class=\"java.lang.String\" operatorType=\"CONTAINS\" width=\"1\" type=\"PROPERTY\"><![CDATA[e.count like :component$filter.count28519 ESCAPE '\\' ]]>\n" +
            "      <param name=\"component$filter.count28519\" javaClass=\"java.lang.String\">NULL</param>\n" +
            "    </c>\n" +
            "  </and>\n" +
            "</filter>");
    filterEntity.setGlobalDefault(false);
    filterEntity.setName("My filter name");
    filter.setFilterEntity(filterEntity);
}

Element <param> contains default value, for filter above it is NULL. You can create a few “demo” filters and see in the EntityInspector how to configure XML conditions.
image