mercredi 10 juin 2015

Ajax event modifies injected class but not controller class (or that is what I think...)

I have a problem with an ajax event. This is just a combobox to select an item and make a filter on a table, this table is just a list of "employees".

<rich:select id="countryFilter" styleClass="registerRounded, large" style="margin-top:5px; float:top;" value="#{allEmployeesController.filterCountry}" defaultLabel="#{msg.common_select_one}"> <f:selectItem noSelectionOption="true" /> <f:selectItems var="item" itemLabel="#{item.name}" itemValue="#{item}" value="#{countries}" /> <f:converter binding="#{countryEnumConverter}" /> <a4j:ajax event="selectitem" render="listEmployeesView"-------AJAX EVENT listener="#{allEmployeesController.filterEmployee()}" /> <f:param name="cid" value="#{javax.enterprise.context.conversation.id}" /> </rich:select>

This works perfectly, but the problem is when I do any other operations from the web view to the table, the list of elements on the controller has not been updated, it makes whatever I want but to the list without the filter. I suppose that the injected class and the actual class have different values.

This is the method called on the controller class:

@Model
@Named
@ConversationScoped
@SuppressWarnings(SERIAL)
public class AllEmployeesController extends Controller implements Serializable {
@Inject
//lot of stuff here
private List<Employee> employees;
//lot of stuff here
//method to filter employee
public void filterEmployee() {
    StringBuffer filterExpression = new StringBuffer();
    List<Object> parameters = new ArrayList<Object>();

    if (filterCountry != null) {
        filterExpression.append(" and (x.country) like ?");
        parameters.add(filterCountry);
    }
    if (!StringUtils.isEmpty(filterName)) {
        filterExpression.append(" and LOWER(x.lastName) like ?");
        parameters.add("%" + filterName.toLowerCase() + "%");
    }
    if (!StringUtils.isEmpty(filterVorName)) {
        filterExpression.append(" and LOWER(x.firstName) like ?");
        parameters.add("%" + filterVorName.toLowerCase() + "%");
    }
    if (!StringUtils.isEmpty(filterLoginName)) {
        filterExpression.append(" and LOWER(x.userName) like ?");
        parameters.add("%" + filterLoginName.toLowerCase() + "%");
    }
    employees = employeeRepository.findByFilterExpression(
            filterExpression.toString(), parameters);

    initValuesLastMonthPropertyEmployee();
}
}

I have debugged and after calling the method from the ajax event, it actually modifies the list in class perfectly on the controller, but when I do any other stuff from a button, when it takes me again to the controller class, the list is the one loaded for first time.

This only happens with this ajax event. There are other fields to filter but they are just text views and the filter is made from a button (normal event, not ajax).

Any help is welcome. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire