0
votes

In my JSF 2.2, Primefaces 5.3 web-application, I want the user to visit a view with predefined filter state (e.g. depending on his user account)

To achieve this, I tried to programmatically apply a filter to a dataTable (i.e. from the backing bean).

In my backing bean I tried:

@ManagedBean
@ViewScoped
public class MyCointroller
private DataTable dataTable = new DataTable();

@PostConstruct
public void init() {
    Map<String, Object> myFilter = new HashMap<String, Object>();
    myFilter.put("userName", "Peter");
    dataTable.setFilters(myFilter);
}

Where I use "binding" with the dataTable in my facelet.

    <p:dataTable var=user"
        value="#{myController.users}"
        binding="#{myController.dataTable}">

        <p:column headerText="Bezeichnung" 
           filterMatchMode="contains" filterBy="#{user.name}">
            <h:outputText value="#{user.name}" />
        </p:column>

The binding itself works, I can read the applied filters from my backing bean, but I fail to apply myFilter to the dataTable (the table doesn't get filtered in the view).

1
What type is myController.user?Johnny Willer
sorry, was a typo, should be "users". myController.users is an ArrayList<User>Raphael Roth

1 Answers

2
votes

You should trigger filter action in javascript to your table on page load. Try to add widgetVar attribute for your dataTable widgetVar="userTable" and add this javascript code to your xhtml page:

<script>
        $(document).ready(function () {
            PF('userTable').filter();
        })
</script>