Primefaces datatable dynamic object[] - jsf

I want to create a dynamic Primefaces datatable from the result of an sql query that gives me a List<Object[]> as result. The corresponding column names are stored in a List<String>.
The columns and the length of the Object[] field should be dynamic.
How to deal with the List<Object[]> in the Datatable?

Use <p:columns>. See also PrimeFaces <p:dataTable> showcase - dymamic columns.
Provided that you actually mean that you've the data in a List<Object[]> and the columnNames in the same order in List<String>, then this should do:
<p:dataTable value="#{bean.data}" var="item">
<p:columns value="#{bean.columnNames}" var="columnName" columnIndexVar="i">
<f:facet name="header">#{columnName}</f:facet>
#{item[i]}
</p:columns>
</p:dataTable>

Related

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>

PrimeFaces datatable fails sorting with active filter

PrimeFaces Version: 6.0
In my data table I can sort and filter columns but not both at the same time.
Following scenario:
Column1 Column2
http://someurl.com 1
http://anotherurl.com 5
When I enter a value into the Column1 filter. Only the filtered rows are visible. Now when I click on sort of Column1 I get the following result:
Column1 Column2
0
0
Column2 is mapped to a primitive value, that explains the zero value.
As a workaround I have added:
<p:ajax event="sort" onstart="PF('mytableWidget').filter();"/>
But I can still see the above empty result for some milliseconds, before the filter() function is executed.
<p:dataTable id="mytable" widgetVar="mytableWidget" value="#{bean.data}" var="item">
<p:column headerText="Column1" sortBy="#{item.column1}" filterBy="#{item.column1}" filterMatchMode="contains">
#{item.column1}
</p:column>
<p:column headerText="Column2" sortBy="#{item.column2}">
#{item.column2}
</p:column>
bean.data is mapped to an ArrayList<MyDataHolder>.
import lombok.Data;
import lombok.EqualsAndHashCode;
#Data
#EqualsAndHashCode(of = "column1")
public class MyDataHolder {
String column1;
int column2;
}
It seems to me you must make MyDataHolder serializable;
public class MyDataHolder implements Serializable
My best bet for an elaboration is the same as here (the same applies to ViewScoped beans). ViewScoped (and broader scoped) beans must be serializable, and therefore all their attributes must be the same.

PrimeFaces dataTable convert values for showing in a view

I have a question based in the following situation.
I have a backing bean that create a matrix with ints, in rows, columns and cells (tablaBean). I have no problem displaying it in a jsf view. Each one of those ints, it´s an ID of differents objects. How can I do, if I want to show in my JSF view, instead of ints, some attributes of the objects?
This is my dataTable:
<p:dataTable var="rowName" value="#{tablaBean.rowNames}" rowIndexVar="rowIdx">
<p:column headerText="Alumnos:">
<h:outputText value="#{rowName}"/>
</p:column>
<p:columns var="colName" value="#{tablaBean.colNames}"
headerText="#{colName}" columnIndexVar="colIdx">
<p:panel>
<h:outputText value="#{tablaBean.data2D[rowIdx][colIdx]}"/>
</p:panel>
</p:columns>
</p:dataTable>
So for example:
tablaBean.data2D[1][1]=3
But I don´t want to show the number "3" in my view.
I want to take that number and show the Person.name of my object Person.id=3;
Hope my question is clear enough.
Create in your backing bean method returning person name:
public String getPersonNameById(int idPerson) {
return myDao.findPersonById(idPerson).getName();
}
Use it in xhtml:
<h:outputText value="#{tableBean.getPersonNameById(tablaBean.data2D[rowIdx][colIdx])}"/>

How to get selected datatable row using icefaces?

I want to get selected row data from ice:datatable ? Here is me code , please give me your suggestion to get selected row data using rowselector.
Backingbean:
public void rowSelectionListener(RowSelectorEvent event) {
System.out.println(event.getRow());
}
jspx code:
<ice:rowSelector id="selected" selectionListener="#{inventoryList.rowSelectionListener}" multiple="false" selectedClass="tableRowSelected" mouseOverClass="tableRowMouseOver" />
<f:facet name="header">
<ice:outputText value="Stock #"/>
</f:facet>
<ice:outputText value="#{item.stock}"/>
</ice:column>
Now I am able to get row index, but I need to get selected row value. How can I do that?
You can get the desired row by binding the table(HtmlDataTable) in backing bean, and on action or some other event you can get the selected row by tableBinding.getRowData() returning the object from the list that was used in table.

JSF datatable row selection

How to select multiple rows in a JSF datatable?
You can either place a checkbox on each row giving it a value of #{currentRowItem.selected} or use <rich:extendedDataTable>
Create a Map<Integer, Boolean> selectMap in the backing bean. (If the key is not model's id and key is a string, create a Map with String, Boolean pair ..)
For every row provide a checkbox #{backingBean.selectMap[rowvar.selectedId]}
You must be able to find the selected values in the map after each selection.
you can use primefaces as follow:
<p:dataTable id="tableId" var="data" value="#{myBean.myList}"
selection="#{myBean.selectedDTOs}"
rowKey="#{data.objectId}">
<p:column selectionMode="multiple" />
</p:dataTable>

Resources