Jalal Sordo
Jalal Sordo

Reputation: 1685

How to set <p:column filterOptions> programmatically

Using JSF 2.2 and PrimeFaces 4.0.

Basically, I'm trying to set a select options (dropdown) as a filter for a <p:column> in <p:dataTable> programmatically. The problem is when I set it using the code below, it renders only a simple input text although it does fire the client side filtering action. How do I get the dropdown to show?

Code of populating the list of options.

private SelectItem[] createFilterOptions() {

    List<SelectItem> options = new ArrayList<SelectItem>();

    options.add(new SelectItem("", "Tout"));
    options.add(new SelectItem(0, "Pas encore"));
    options.add(new SelectItem(1, "Patient en retard"));
    options.add(new SelectItem(2, "Patient arrivé"));
    options.add(new SelectItem(3, "Trop attendu"));
    options.add(new SelectItem(4, "Patient préparé"));
    options.add(new SelectItem(5, "Examen en cours"));
    options.add(new SelectItem(6, "Examen terminé"));
    options.add(new SelectItem(7, "Résultats remis"));
    options.add(new SelectItem(8, "Examen annulé"));

    return options;
}

This is how I set the value of column's filterOptions property:

SelectItem[] etatExamOptions = createFilterOptions();
Column patSate = (Column) application.createComponent(Column.COMPONENT_TYPE);
patSate.setHeaderText("Etat");
patSate.setWidth("10");
patSate.setId("etatCol");
patSate.setFilterBy("examen.studyPatientState");
patSate.setFilterOptions(etatExamOptions);
patSate.setFilterMatchMode("exact");
patSate.setFilterStyle("dispo");
patSate.setSortBy("examen.studyPatientState");
patSate.setRendered(true);  

Upvotes: 1

Views: 2238

Answers (2)

BalusC
BalusC

Reputation: 1109072

I peeked around in the DataTableRenderer source code. It appears that it's only rendered as a dropdown list when the filterOptions attribute is a value expression.

468    if(column.getValueExpression("filterOptions") == null) {
...        // ...            
471        writer.startElement("input", null);
...        // ...            
490    }
491    else {
...        // ...            
494        writer.startElement("select", null);
...        // ...            
514    }

which is somewhat strange at first glance, but makes sense considering the way how filterOptions is usually used in the XHTML side. They could have done a better job here, e.g. instanceof on the evaluated value and check if it's an array or collection of SelectItem, but you'll have to work with what's you been provided.

You need to set it as a value expression referring a bean property instead of as a "hardcoded" List<SelectItem>. Create an application scoped bean class Data with that list as property so that it's available by #{data.filterOptions} and finally create a ValueExpression around it and set it as filter options:

patSate.setFilterOptions(createValueExpression("#{data.filterOptions}", List.class));

Upvotes: 1

worcin
worcin

Reputation: 129

You can set the filteroption in the bean an pass it from there to the xhtml

<p:datatable ... filtermode="#{bean.filtemode}"....
...
public class Bean{

  public getFiltermode(){
      return "filtermode";
  }

}

Upvotes: -2

Related Questions