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.
Related
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.
i have a datatable with dynamic columns, defined by a columnModel. The String property links to the correct field (used for value output). The sort String equals the property, but some columns should not be sortable, sort is null or emtpy (tried both) there:
public class ColumnModel {
private String property;
private String sort;
private int width;
//GETTER
...
}
I use a List of that models to create my dynamic columns. The use of the width is working well:
<p:dataTable value="#{bean.items}" var="item" ... >
<p:columns value="#{bean.columnModel}" var="column" sortBy="#{column.sort}" width="#{column.width}">
...
</p:columns>
</p:dataTable>
My Question:
sortBy does not allow a null or an emtpy value. Otherwise i get a parse exception where it says, it cannot parse #{item.}. primefaces seems to add 'item' (my var of the datatable) automatically before the given sortfield.
How can some columns be ignored?
Thanks for your answeres!
Using primefaces 5.0.9 with Wildfly 9.0.2
Primefaces added new attributes for p:column(s) in version 5.1.3 and 5.2.0:
sortable
filterable
Here is the link to the solved Issue:
https://code.google.com/archive/p/primefaces/issues/5021
Example depending on my code above:
public class ColumnModel {
private String property;
private boolean sortable;
private int width;
//GETTER
...
}
With a given List<ColumnModel> columnModel in bean:
<p:dataTable value="#{bean.items}" var="item" ... >
<p:columns value="#{bean.columnModel}" var="column" sortBy="#{item[column.property]}" field="#{column.property}" sortable="#{column.sortable}" width="#{column.width}">
...
</p:columns>
</p:dataTable>
What am I not doing right?
#Named("utilityController")
#RequestScoped
public class UtilityController {
public DataModel<Result> getResultSample() {
Result[] resultSample = new Result[11];
//Populate the array
return new ArrayDataModel<>(resultSample);
}
}
In The JSF:
<h:dataTable id="sampleResult" value="#{utilityController.resultSample}" var="item" styleClass="table table-bordered table-striped table-hover table-condensed" >
<h:column>
<f:facet name="header">SN</f:facet>
#{utilityController.resultSample.rowIndex}
</h:column>
<h:column>
<f:facet name="header">Subject</f:facet>
#{item.subject.name}
</h:column>
....
</h:dataTable>
The rowIndex always returns 0 as can be seen above. Please can some help me pin point what I am doing wrongly
What am I not doing right?
Creating the model in a getter method. Never do that. All getter methods should look like this:
public DataModel<Result> getResultSample() {
return resultSample;
}
The getter method is invoked on every iteration round. You're basically clearing out the model from the previous iteration round and returning a brand new one, with all state (such as current row index) reset to default value.
Move that job to bean's #PostConstruct method.
private DataModel<Result> resultSample;
#PostConstruct
public void init() {
Result[] results = new Result[11];
// ...
resultSample = new ArrayDataModel<Result>(results);
}
public DataModel<Result> getResultSample() {
return resultSample;
}
As to your concrete functional requirement, you can also just reference UIData#getRowIndex() without wrapping the value in a DataModel.
public Result[] getResults() { // Consider List<Result> instead.
return results;
}
<h:dataTable binding="#{table}" value="#{bean.results}" var="result">
<h:column>#{table.rowIndex + 1}</h:column>
<h:column>#{result.subject.name}</h:column>
</h:dataTable>
Note that I incremented it with 1 as it's 0-based while humans expect an 1-based index.
See also:
How and when should I load the model from database for h:dataTable
Why JSF calls getters multiple times
How does the 'binding' attribute work in JSF? When and how should it be used?
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;
}
}
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)