p:column in p:dataTable, "sortBy" descending on the first click - jsf

The sortBy attribute of <p:column> in <p:dataTable> lets the user to sort a column ascending or descending on each click, is there anyway to sort column descending on the first click of column header. It is ascending by default.
<p:column sortBy="#{bean.col1Value}" headerText="Col Header">
#{bean.col1Value}
</p:column>
Is there any possibility to override this <p:dataTable>'s default setting?

I think you can use the attribute sortFunction. I quote from the Primefaces 5.1 User Guide page 153-154
Instead of using the default sorting algorithm which uses a java
comparator, you can plug-in your own sort method as well
public int sortByModel(Object car1, Object car2) {
car2.compareTo(car1);
}
And then in your html file
<p:dataTable var="car" value="#{carBean.cars}">
<p:column sortBy="#{car.model}" sortFunction="#{carBean.sortByModel}"
headerText="Model">
<h:outputText value="#{car.model}" />
</p:column>
...more columns
</p:dataTable>

You need to use sortFunction="#{testBean.customSort}" and you can customized your sorting.

Related

How to reset the sorting on dynamic colums at PrimeFaces?

I have dynamic columns which are sorted as well as filtered. I managed to clear the filters, but according to Primefaces there is no function to clear the sorting for the client API.
My .xhtml:
<p:dataTable id="tableId" value="#{statisticCreateBean.tableData}"
var="data" widgetVar="dataTbl"
rendered="#{not empty statisticCreateBean.tableData}">
<p:columns id="columnId" value="#{statisticCreateBean.columnModel}"
var="column" sortBy="#{data[column.fieldName]}"
filterBy="#{data[column.fieldName]}">
<f:facet name="header">
#{column.displayName}
</f:facet>
<h:outputText value="#{data[column.fieldName]}" />
</p:columns>
</p:dataTable>
My Java-code:
Columns columns = (Columns)FacesContext.getCurrentInstance().getViewRoot().findComponent("statisticForm:tableId:columnId");
columns.setSortBy(null);
PrimeFaces.current().executeScript("PF('dataTbl').clearFilters();");
Is there a way to clear the sorting on dynamic columns?
To reset sorts and filters properly get the Datatable and call reset.
DataTable datatable= (DataTable )FacesContext.getCurrentInstance().getViewRoot().findComponent("statisticForm:tableId");
datatable.reset();

Filter p:datatable by boolean column (checkbox)

I am trying to filter my primefaces datatable by a boolean column using a checkbox filter but unfortunately filtering in primefaces datatable seems it does not work with any type other than String, but there should be a workaround for this case.
datatable column
<p:column headerText="A_boolean_column" filterBy="#{myBean.myBoolean}" filterMatchMode="exact">
<f:facet name="filter">
<p:selectCheckboxMenu label="BooleanFilter" onchange="PF('mydatatable').filter()" styleClass="custom-filter">
<f:selectItems value="#{myBean.possibleAnswers}" />
<p:ajax update="#form" oncomplete="PF('mydatatable').filter();"/>
</p:selectCheckboxMenu>
</f:facet>
<h:outputText value="#{myBean.myBoolean}"/>
</p:column>
where possibleAnswers variable is a list that has been initialized on init method of myBean with true && false values
#PostConstruct
public void init(){
this.possibleAnswers= new ArrayList<>();
possibleAnswers.add(true);
possibleAnswers.add(false);
}
I have similar working examples in my datatable with text values and are working perfectly. Of course I could do a workaround to fix my issue by converting the values from boolean ( true / false) into String ("true" / "false") ( or even write a custom function to check for equality)but I don't really like this solution and I would prefer any other out of the box solution ( perhaps a different filterMatchMode ? ).
I am using primefaces 7.0
Normally an input component has a 'value' attribute that is bound to a field (getter/setter) in a backing bean. The type of this field can be used to automatically convert the technical string of the http request to the correct java type. For a datatable filter this cannot be automatically done since there is no value attribute. Giving all components knowledge about all possible containers they can be used in is bad design. So the only and corrrect solution is to use an explicit converter.
Have a Look at the Implementation of the Status-Column in the PrimeFaces datatable filter showcase, as far as I see it it is exactly what you need
for Reference:
<p:column filterBy="#{myBean.myBoolean}" filterMatchMode="in">
<f:facet name="filter">
<p:selectCheckboxMenu label="BooleanFilter"
onchange="PF('mydatatable').filter()" styleClass="custom-filter">
<f:converter converterId="javax.faces.Boolean" />
<f:selectItems value="#{myBean.possibleAnswers}" />
<p:ajax update="#form" oncomplete="PF('mydatatable').filter();"/>
</p:selectCheckboxMenu>
</f:facet>
</p:column>

Filter boolean values in primefaces datatable

I have a Primefaces datatable in a web application. One column contains a boolean (represented as selectBooleanCheckbox). Now I'd like to filter the table with this column. When I add the filterBy and filterMatchMode attributes the filtering header appears, but I have to filter using true or false.
This is the definition of the column:
<p:column headerText="A Bool" sortBy="someBool" width="20"
filterBy="someBool" filterMatchMode="exact">
<h:selectBooleanCheckbox value="#{row.someBool}" disabled="true" />
</p:column>
The display of the column with a checkbox is correct. Only the header is displayed as a text field.
I'm using Primefaces 4. How can I get a check box in the header to filter the data?
From Primefaces 4.0 User Guider, page 141:
If you’d like to use a dropdown instead of an input field to only allow
predefined filter values use filterOptions attribute and a collection/array of selectitems as value
This way you could filter by selecting true or false on the list. But you want a checkbox on the header, you could try this (also from the user guide, page 142):
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Search all fields:" />
<h:inputText id="globalFilter" onkeyup="PF('carsTable').filter()" />
</p:outputPanel>
</facet>
I hope this can help you go in the right direction.

Primefaces dataTable multiple selection same object

I'm having a problem with the dataTable multiple selection with checkbox (like the second one: http://primefaces.org/showcase/ui/datatableRowSelectionRadioCheckbox.jsf).
When I try to execute a method in my backingBean, the selected items always comes with the right size, but, with the same object.
Example: I select in my dataTable three Messages: Message 1, Message 2 and Message 3. When I execute my method in the JSF page my selected items comes like this:
Messages[0] = Message 1
Messages[1] = Message 1
Messages[2] = Message 1
Here is my JSF Page:
<p:dataTable id="mensagensLidas" var="msg" value="#{mensagensBean.msgGrupoModel}"
paginator="true" rows="10" selection="#{mensagensBean.selectedMsgsGrupo}">
<p:column selectionMode="multiple" style="width:2%"/>
<p:column headerText="Titulo:" style="width:49%">
#{msg.titulo}
</p:column>
<f:facet name="footer">
<p:commandButton id="multiViewButton" value="#{msgs.delete}" icon="ui-icon-trash" actionListener="#{mensagensBean.excluirMsgsGrupo}" update=":tabMain" ajax="true"/>
</f:facet>
</p:dataTable>
Your above code is working fine for me except that I'm using a rowKey.
Try using rowKey attribute in p:dataTable
<p:dataTable id="mensagensLidas" var="msg" value="#{mensagensBean.msgGrupoModel}"
paginator="true" rows="10" selection="#{mensagensBean.selectedMsgsGrupo}"
rowKey="#{msg.titulo}">
....
</p:dataTable>
Using rowKey is must and best practice when you have selection in Datables, it helps to map the selected <tr> to the POJOs that you're filling up with.
Usually its even better if your rowKey is unique propety, you can even put an integer variable in your #{msg} POJO as best practice.
I discovered the problem. My DataModel was with the getRowData returning always the same object. I was doing a wrong comparison. Thank you all!

Value of <h:selectBooleanCheckbox> inside <p:dataTable> remains false on submit

I have a primefaces datatable and inside the primefaces datatable, I have a column, which contains the . The issue is,I have set the default value for the as false. When I click/check the , its still retrieving the value as false. I tried it multiple times but not sure why its returning false. Please find the sample code below.
<p:dataTable id="review-table" var="item" value="#{demandBean.filterVOList}">
<p:column id="SelectallID" style="text-align: left; width:40px;" rendered="#{demandBean.screeRenderVo.selectAllRenderer}">
<f:facet name="header" >
<h:outputText id="selectId" value="#{demandBean.dmdScreenLabelVO.selectAll}" />
<div></div>
<h:selectBooleanCheckbox id="checkbox1" value="Select All" onclick="checkAll(this)"/>
</f:facet>
<h:selectBooleanCheckbox id="checkbox2" value="#{item.selected}"/>
</p:column>
Im getting the value as false, when I check the and click on the save button. I have written an Action listerner, below is the code corresponding to the actionListener
public void saveData(ActionEvent event)
{
System.out.println("Entering the Save :");
selected = isSelected();
System.out.println("value of Selected"+selected);
}
I have tried debugging the code as well, but not sure why the value for is getting displayed as false. Please Assist. Thanks in Advance
You seem to be binding the value of all checkboxes in the column to the one and same bean property. This way the value will ultimately end up to be the one of the last row in the column.
This is not how it's supposed to be used.
You basically need to bind the value of the checkbox to the property of the currently iterated row object (the one behind the var attribute of the datatable).
<p:dataTable value="#{bean.items}" var="item">
<p:column>
<p:selectBooleanCheckbox value="#{item.selected}" />
Alternatively, you could use the <p:column selectionMode="multiple" /> to use builtin multiple selection support of the PrimeFaces datatable (see also the showcase example).
<p:dataTable value="#{bean.items}" var="item" rowKey="#{item.id}" selection="#{bean.selectedItems}">
<p:column selectionMode="multiple" />

Resources