dasago
dasago

Reputation: 71

p:dataTable initial SortBy with dynamic columns

I want to sort a table by a default column. I found following example in the documentation:

<p:dataTable var="car" value="#{carBean.cars}" sortBy="model">
    <p:column sortBy="model" headerText=”Model”>
        <h:outputText value="#{car.model}" />
    </p:column>
    <p:column sortBy="year" headerText="Year">
        <h:outputText value="#{car.year}" />
    </p:column>
    ...more columns
</p:dataTable>

This works fine for static columns. By default it is sorted by column 'model '. But how does this work with dynamic columns (p:columns)?

If I implement it the same way:

<p:dataTable id="cars" var="car" value="#{dtColumnsView.cars}"
    widgetVar="carsTable"
    filteredValue="#{dtColumnsView.filteredCars}"
    sortBy="#{car.model}">

    <p:columns value="#{dtColumnsView.columns}" var="column"
        columnIndexVar="colIndex" sortBy="#{car[column.property]}">

        <f:facet name="header">
            <h:outputText value="#{column.header}" />
        </f:facet>

        <h:outputText value="#{car[column.property]}" />
    </p:columns>
</p:dataTable>

It's sorted correctly, but the style for the sorted column header is not set! if I use sortBy="#{car[column.property]}", the style for all columns are set and the sort doesn't work. I think the problem is in DataTableRenderer resolveDefaultSortIcon method. The property tableSortByExpression is not the same as columnSortByExpression.

Is this a bug? Any Help or workaround? I am using PrimeFaces 4.0. But I also tested it with 5.1.

Upvotes: 4

Views: 4446

Answers (1)

sasha
sasha

Reputation: 63

I had similar question and asked in primefaces forum

Here the answer

http://forum.primefaces.org/viewtopic.php?f=3&t=41301&p=130146#p130146

so working code should from your example should be

<p:dataTable id="cars" var="car" value="#{dtColumnsView.cars}"
widgetVar="carsTable"
filteredValue="#{dtColumnsView.filteredCars}"
sortBy="#{false}" sortField="model">

    <p:columns value="#{dtColumnsView.columns}" var="column"  
               columnIndexVar="colIndex" sortBy="#{car[column.property]}"  
                                              field="#{column.property}">

        <f:facet name="header">
            <h:outputText value="#{column.header}" />
        </f:facet>

        <h:outputText value="#{car[column.property]}" />
    </p:columns>
</p:dataTable>

Upvotes: 1

Related Questions