List in omnifaces.ListConverter is not set - jsf

I'm developing a JSF Application with PrimeFaces.
For a Picklist I'm trying to use the omnifaces.ListConverter but the list property in this converter is not set.
reporting-edit.xhtml
<p:pickList id="picklist_columns" value="#{reportingEditView.columns}" var="repcolumn"
itemLabel="#{repcolumn.column.name}" itemValue="#{repcolumn}" responsive="true"
showSourceFilter="true" filterMatchMode="contains">
<o:converter converterId="benni.ListConverter" list="#{reportingEditView.columns.source}"/>
<f:facet name="targetCaption">Spalten</f:facet>
<p:ajax event="transfer" listener="#{reportingEditView.handleColumnTransfer}"/>
reportingEditView.java
#ManagedBean
#SessionScope
public class ReportingEditView
{
private DualListModel<RepColumn> columns;
public void initPickList()
{
List<RepColumn> availableColumns = new ArrayList<>();
List<RepColumn> selectedColumns = new ArrayList<>();
... populate availableColumns and selectedColumns ...
this.columns = new DualListModel<RepColumn>();
this.columns.setSource(availableColumns);
this.columns.setTarget(selectedColumns);
}
}
All the columns are displayed in my PickList but when I put one column from the source list into the target list a NullPointer is thrown because the list in the converter is not set.
Also the listener Method is not called when transferring an element in the PickList.
Any ideas what I am doing wrong?

Shame on me!
I fixed that issue. I don't know how but there was xmlns:o="http://java.sun.com/jsf/core" in my reporting-edit.xhtml. After replacing that with xmlns:o="http://omnifaces.org/ui" everything works like a charm.

Related

Primefaces datatable's p:columns not iterating over Map

I am having a two level map Map<String,HashMap<String,String>> which i need to display using a <p:dataTable>. The code of managed bean is as follows:
#ManagedBean(name="MyBean")
public class MyBean{
private Map<String,HashMap<String,String>> twoDimentionalMap;
public void getMapData(){
twoDimentionalMap=getDataFromDataStore();
}
}
Now I am using this map in my view.xhtml file as follows:
<p:dataTable var="entrySet1" value="#{MyBean.twoDimentionalMap.entrySet()}">
<p:columns var="entrySet2" value="#{entrySet1.getValue()}">
#{entrySet2.getKey()} - #{entrySet2.getValue()}
</p:columns>
</p:dataTable>
I also tried using
<p:dataTable var="entrySet1" value="#{MyBean.twoDimentionalMap.entrySet()}">
<p:columns var="entrySet2" value="#{MyBean.twoDimentionalMap.get(entrySet1.getKey()).getValue()}">
#{entrySet2.getKey()} - #{entrySet2.getValue()}
</p:columns>
</p:dataTable>
I even tried converting the outer map to a list:
List<HashMap<String,String>> twoDimentionalMap;
However nothing is displayed on datatable. The execution shows no error but there is nothing displayed on the page.
Kindly suggest if I am doing something wrong or if <p:columns> is having any issue handling maps.
Thanks
since the keys of outer map do not have a meaning, converting the outer map to a list is correct.
but your approach to retrieve column names from xhtml does not seem valid. you need to get them independently from the current iteration variable entrySet1, otherwise you add a third dimension to the operation, which data table cannot handle.
we need to assume that all keys are same across the listed maps.
here is the code for xhtml:
<p:dataTable var="entrySet1" value="#{testMB.twoDimensionalMap}">
<p:columns var="keySet2" value="#{testMB.columnNames}">
#{keySet2} - #{entrySet1[keySet2]}
</p:columns>
</p:dataTable>
and for the bean:
#Named
#ViewScoped
public class TestMB implements Serializable {
private List<HashMap<String,String>> twoDimensionalMap;
public TestMB()
{
getMapData();
}
private void getMapData(){
//twoDimentionalMap=getDataFromDataStore();
twoDimensionalMap = new ArrayList<HashMap<String,String>>();
twoDimensionalMap.add(new HashMap<String,String>());
twoDimensionalMap.get(0).put("key0", "value00");
twoDimensionalMap.get(0).put("key1", "value01");
twoDimensionalMap.add(new HashMap<String,String>());
twoDimensionalMap.get(1).put("key0", "value10");
twoDimensionalMap.get(1).put("key1", "value11");
}
public Set<String> getColumnNames()
{
return twoDimensionalMap.size() > 0 ? twoDimensionalMap.get(0).keySet() : new HashSet<String>();
}
public List<HashMap<String, String>> getTwoDimensionalMap() {
return twoDimensionalMap;
}
}

Auto-updating filter values on DataTable

I'm currently using filters with options list on my lazy loaded DataTables. Everything works just fine except I'd like to be able to reload my filter options depending on currently selected filters.
For example, my DataTable has two fields country and region, and I want to filter
DataTable snippet :
<p:dataTable var="d" widgetVar="personneContactTable" value="#{bean.dataModel}" id="myDataTable" lazy="true">
<p:column sortBy="country" filterBy="country" filterOptions="#{bean.getCountryOptions()}">
[...]
</p:column>
<p:column sortBy="region" filterBy="region" filterOptions="#{bean.getRegionOptions()}">
[...]
</p:column>
</p:dataTable>
And my bean :
#ManagedBean(name = "bean")
#SessionScoped
public class MyBean implements Serializable {
LazyDataModel<MyStuff> dataModel;
String country;
String region;
public SelectItem[] getCountryOptions() {
return service.someMagic();
}
public SelectItem[] getRegionOptions() {
return service.someMoreMagic(country);
}
// + getters, setters, etc.
}
I tried using properties instead of methods, adding <p:ajax event="filter"> tags to try reloading part of the DataTable, but nothing worked. I found http://forum.primefaces.org/viewtopic.php?f=3&t=38087 too, but again not workable solution emerged.
How can I automaticaly refresh my filterOptions? (this is using Primefaces 4 ELITE branch)

p:dataTable initialization

I have a code in which I add elements to a class type list from a method. But whenever Im trying to generate a p:dataTable the corresponding get method of the list can't find out the previous values which I have already added. My code is as below-
List<LastToleranceExceed> excptn = new ArrayList<LastToleranceExceed>();
public void ret_approve(FlPendingList fl)throws SQLException{
String q = "----My Query----";
PreparedStatement pq=cn.prepareStatement(q);
ResultSet rsq=pq.executeQuery();
while(rsq.next()){
excptn.add(new LastToleranceExceed(unit_name,energy_name,
consump_id,date_list,
shift_list,
Edit_Quantity,
Quantity,
uom_list,
violation));
}
public List<LastToleranceExceed> getExcptn() throws SQLException{
return excptn;
}
In Xhtml :
<p:dataTable value="#{flApproval.excptn}"
var="vio"
id="dt2"
rendered="#{flApproval.dt_show}">
<p:column>
<f:facet name="header">
Date
</f:facet>
#{vio.activitydate}
</p:column>
</p:dataTable>
What is the problem of this code? how I can get the values which I have added? Plz help
When is ret_approve method called, probably it's after the data table initializes.
You can try applying #PostConstruct annotation on ret_approve.

datatables under tree structure in jsf core/ primefaces

I am stuck in a rather weird situation.
I have a requirement in which i need a tree structure to define a list of elements. And when we click on any of those elements in a tree. it should expand the section below it to show 2 side by side datatables which are linked to that element.
I can use JSF 2.0 core or even primefaces 3.1.
Is it possible ??
Could anyone please help ... any suggestions would be appreciated.
Updated
My model is something like :
class Shop{
boolean isoperational;
String name;
List<Item> items;
List<boolean> itempresent;
List<Employee> employees;
}
I need the name of the shop on the tree node along with the isoperational checkbox;
And when we click on that node it should open 2 datatables.
One containing the List of items along with itempresent checkbox.
Other containing the List of employees.
Thanks
Edited per your response (if i understand you correctly). Note that this is written from head so some attribute names might differ. The point is to set an active collection from your treetable depending on which row is clicked. An ID is set via the actionListener, then the action sets mycontroller.activeCollection to the list you want to show. Then the datatable is rerendered via normal post or ajax with values depending on which row you clicked.
Markup
<p:treeTable value="#{myController.treenode}" var="item">
<p:column>
<h:commandLink action="#{myController.setActiveShop}" value="set">
<f:setPropertyActionListener target="#{myController.shipName}" value="#{item.shop.name}"/>
</h:commandButton>
</p:column>
</p:treeTable>
<p:dataTable value="#{myController.activeShop.items" rendered="#{!empty myController.activeShop}" var="item">
<p:column>#{item.name}</p:column>
</p:datatable>
<p:dataTable value="#{myController.activeShop.employees" rendered="#{!empty myController.activeShop}" var="item">
<p:column>#{item.name}</p:column>
</p:datatable>
Controller
#ManagedBean
#ViewScoped // Or #SessionScoped... something that can cache lists, not sure how this would be done with requestscope
public class MyController{
private List<Shop> shopList; // Set this from a datasource. Or use only shopTree below to store your shops..
private Shop activeShop;
private String shopName;
private TreeNode shopTree; // Generate this from shopList
// insert getters and setters
private setActiveShop(){
for(Shop s : shipList)
if(s.getName().equals(shopName)
activeShop = s;
}
}

jsf datatable row selection problem

I have a problem with selecting rows in the Primefaces Datatable. I use dynamic columns, so the standard row selection mechanism is not usable here, I implement checkbox selection myself.
To help, here's s simplified version of what I have in my xhtml:
<h:form>
<p:dataTable id="table"
var="result"
value="#{tableBean.results}">
<p:columns value="#{tableBean.columnNames}" var="column" columnIndexVar="colIndex">
<f:facet name="header">
#{column}
</f:facet>
<h:panelGroup rendered="#{colIndex==0}">
<h:outputLabel>#{rowIndex}</h:outputLabel>
<h:selectBooleanCheckbox value="#{tableBean.selectedRows[result[0]]}"/>
</h:panelGroup>
</p:columns>
</p:dataTable>
<h:commandButton value="Submit"></h:commandButton>
</h:form>
And here's what I have in the managed bean to select the checkboxes:
package testpackage;
import java.util.*;
import javax.faces.bean.*;
#ManagedBean
#SessionScoped
public class TableBean
{
private Map<String, Boolean> selectedRows = new HashMap<String, Boolean>();
List<List<String>> results = new LinkedList<List<String>>();
public TableBean()
{
List<String> row1 = new LinkedList<String>();
List<String> row2 = new LinkedList<String>();
row1.add("row1.ref");
row1.add("row1.id");
row1.add("row1.status");
row2.add("row2.ref");
row2.add("row2.id");
row2.add("row2.status");
results.add(row1);
results.add(row2);
//selectedRows.put("row2.ref", true);
}
public Map<String, Boolean> getSelectedRows()
{
return selectedRows;
}
public String submit()
{
List<List<String>> selectedResults = new ArrayList<List<String>>();
for (List<String> result : results)
{
if (selectedRows.get(result.get(0)) != null)
{
selectedResults.add(result);
selectedRows.remove(result.get(0));
}
}
return null;
}
public List<List<String>> getResults()
{
return results;
}
public List<String> getColumnNames()
{
List<String> columnNames = new LinkedList<String>();
columnNames.add("");
columnNames.add("REF");
columnNames.add("ID");
columnNames.add("STATUS");
return columnNames;
}
}
The getSelectedRows method works great, but the problem is that the setSelectedRows method is never called, so I don't know which checkboxes the user has selected. Maybe I overlook something very trivial, but cannot find the solution.
Any ideas on this? I would be very glad if you helped, or give any other row selection solution for the dynamic columns.
Thx in advance,
Levi
To me it looks you are rendering the wrong field in selectBooleanCheckBox.
You should be using variable or field from the result variable.
My solution:
In your situation you are rendering an object from List as a form of table row so if you want to make some changes and retrieve the status of that row then you should be using the variable from that object only.
I understand you are submitting the whole form and want to pickup all updated rows, in that case you will have to loop through the whole List and find all the rows which have been updated by checking the status in Request Handler(Action) bean.
Hope that helps.
The setter is never called for nested objects. You're the one who's responsible for creating them, not JSF. JSF just gets the nested object and then calls the setter on it (which is the put() method in case of a Map). You just need to determine the selected rows in the action method. Add an action method to the commandbutton:
<h:commandButton value="Submit" action="#{bean.submit}"></h:commandButton>
which is definied like follows (guessing/assuming that var="result" is in essence an Object[]):
public String submit() {
List<Object[]> selectedResults = new ArrayList<Object[]>();
for (Object[] result : results) {
if (selectedRows.get((String) result[0])) {
selectedResults.add(result);
selectedRows.remove(result[0]); // Reset.
}
}
// Now selectedResults contains all selected results.
}

Resources