Best
Best

Reputation: 3

Adding p:column programatically with filterBy and sortBy attributes

I'm trying to create datatable column pragmatically ,using primeface 5.2 ,wildfly ,Mojarra 2.2.6

I have inherited from DataTable, and creating my own Columns,i want to set the sortBy,filterBy attributes using value expressions using the below code

public void AddCoulumn(String colName, String dbFieldName,
        boolean Searchable) {
    ValueExpression valExpr = null;
    Column column = new Column();
    valExpr = createValueExpression("#{obj." + dbFieldName + "}");
    column.setValueExpression("filterBy", valExpr);
    column.setValueExpression("sortBy", valExpr);
    column.setWidth("100px");
    column.setHeaderText(colName);
    HtmlOutputText out = new HtmlOutputText();
    out.setValueExpression("value", valExpr);
    out.setStyle("color:red");
    column.setField(dbFieldName);
    this.getChildren().add(column);

}

Unfortunately when trying to filter results nothing occur and where trying to sort using any column i got this exception

Caused by: java.lang.NullPointerException at org.primefaces.component.datatable.DataTable.findColumnInGroup(DataTable.java:905) [primefaces-5.2.jar:5.2] at org.primefaces.component.datatable.DataTable.findColumn(DataTable.java:896) [primefaces-5.2.jar:5.2] at org.primefaces.component.datatable.feature.SortFeature.decode(SortFeature.java:86) [primefaces-5.2.jar:5.2] at org.primefaces.component.datatable.DataTableRenderer.decode(DataTableRenderer.java:62) [primefaces-5.2.jar:5.2] at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:831) [jboss-jsf-api_2.2_spec-2.2.6.jar:2.2.6]

thanks in advance

Upvotes: 0

Views: 1280

Answers (1)

Ahmed Gamal
Ahmed Gamal

Reputation: 1696

I fixed this problem after debugging the PrimeFaces datatable. I found that if you try to sort or filter using specific column the datatable calls a findcolumn function which take string client id as a paramter: findcolumn(string client id). Inside this function a function called getcolumns() retrieves the table columns... If the client id is found in the list it returns the column but in this case it never finds a column because PrimeFaces/JSF changes the columns id on the postback since the whole view is restored. So the column will not be found anymore

To solve this problem explicitly set the column id

column.setId(colName);

It works fine now

Upvotes: 2

Related Questions