I have a primeface table that shows data from an database stored in a List called notificationList based on a search from. I want the table to be displayed after the submit button is clicked however after I add the rendered attribute the table not displaying at all. Can someone tell what I'm doing wrong and how to fix it? I've tried two methods to do this.
Using rendered=#{not empty notificationSearchBean.results.notificationList} which didn't work even though the list isn't empty (I know bc I printed out the results in the console)
Creating a boolean called visible like in this post "Want to show a data table populated with data after a button click". This also isn't displaying the table regardless of whether visible is set to true or false. What's weird is that I've tried initializing visible with true and the table is still not being rendered.
This is my code for the table:
<h:panelGroup id = "table-wrapper" styleClass="searchResults">
<h:form id="result" >
<p:dataTable id="notTable" var="notifications" value="#{notificationSearchBean.results.notificationList}" row="15">
<p:column headerText="Notification No.">
<h:outputText value="#{notifications.notificationNo}"/>
</p:column>
<p:column headerText="Service">
<h:outputText value="#{notifications.srvce}"/>
</p:column>
<p:column headerText="Operation">
<h:outputText value="#{notifications.oprtn}"/>
</p:column>
<p:column headerText="Service Version">
<h:outputText value="#{notifications.srvceVrsn}"/>
</p:column>
<p:column headerText="Event Date">
<h:outputText value="#{notifications.evntDt}" >
<f:convertDateTime pattern="dd-MMM-yyyy 'at' HH:mm:ss.SSS" />
</h:outputText>
</p:column>
</p:dataTable>
</h:form>
</h:panelGroup>
And code for the submit button:
<h:panelGrid align="center" columns="2">
<p:commandButton value="Start Search" action="#{notificationSearchBean.getAllNotificationsForQuery}" update="table-wrapper"/>
</h:panelGrid>
And the NotificationSearchBean:
#ManagedBean(name = "notificationSearchBean" )
#SessionScoped
public class NotificationSearchBean implements Serializable {
private static final long serialVersionUID = 1L;
private TransactionsDao transactionsDao = (TransactionsDao)((Login)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("login")).getTransDao();
#Inject
private SearchCriteria search;
#Inject
private ResultSet results;
#PostConstruct
public void initialize() {
//setting attributes for search form
}
//getters and setters for search
public ResultSet getResults() {
return results;
}
public void setResults(ResultSet results) {
this.results = results;
}
public String getAllNotificationsForQuery() {
List<NotificationLogT> notifications=transactionsDao.getAllNotifications(this.search);
int temp=notifications.size();
this.results.setNotificationList(notifications);
if(temp==0){
//prints out not results return message
}
return "success";
}
}
And also the ResultSet class:
public class ResultSet implements Serializable {
private List<NotificationLogT> notificationList;
private boolean visible = false;
//private Integer dataSize;
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public List<NotificationLogT> getNotificationList() {
setVisible(true);
return notificationList;
}
public void setNotificationList(List<NotificationLogT> notificationList) {
this.notificationList = notificationList;
}
}
Related
I am using Primefaces 5.0 to create a dynamic datatable.
My DataObject has some required fields and a List of optional "tupel" (key-value pair). The optional list may vary in size. Therefore I need a dynamic mechanism to show a List of DataObject in Primefaces.DataTable.
My approach looks like:
public class DataObject {
private String staticval1;
private String staticval2;
private List<Tupel> optionalValues;
// .. getter, setter, hashCode, toString.....
}
public class Tupel{
private String id;
private String value;
}
#ManagedBean
#ViewScoped
public class TableOverviewBean {
private List<DataObject> data;
#EJB
private IMyDao myDao;
#PostConstruct
public void init() {
data = myDao.findAll();
}
public List<DataObject> getData() {
return data;
}
public void setData(List<DataObject> data) {
this.data = data;
}
}
<h:form>
<p:dataTable value="#{tableOverviewBean.data}" var="data">
<p:column headerText="static1">
<h:outputText value="#{data.staticval1}" />
</p:column>
<p:column headerText="static2">
<h:outputText value="#{data.staticval2}" />
</p:column>
<p:columns value="#{data.optionalValues}" var="opt" headerText="#{opt.id}">
<h:outputText value="#{opt.value}" />
</p:columns>
</p:dataTable>
</h:form>
But this does not work. The dynamic columns are not rendered.
How can I solve my problem?
EDIT:
Expected result:
staticval1 | staticval2 | dynamic_id1 | dynamic_id2 | ... | dynmic_idn
----------------------------------------------------------------------
static1a | static2a | dyna_value1a| dyna_value2a | ... | dyna_valu3a
static1b | static2b | dyna_value1b| dyna_value2b | ... | dyna_valu3b
static1c | static2c | dyna_value1c| dyna_value2c | ... | dyna_valu3c
It isn't possible to define columns based on row data. Imagine that row 1 has 2 columns, row 2 has 6 columns, row 3 has 1 column, etc how would you ever produce a technically valid table in HTML? Each row must have the same amount of columns.
You've 2 options, depending on whether can change the model or not:
If you can't change the model, then you need to replace that <p:columns> by a single <p:column> and loop over the #{data.optionalValues} using a nested loop with e.g. <ui:repeat> or perhaps even another <p:dataTable><p:columns>:
<p:column>
<p:dataTable value=""><!-- Empty string as value forces 1 row. -->
<p:columns value="#{data.optionalValues}" var="opt" headerText="#{opt.id}">
#{opt.value}
</p:columns>
</p:dataTable>
</p:column>
If you can change the model, then you need to let <p:columns value> point to a bean property instead of to a row property, so that it's exactly the same for every row. This works if you replace List<Tupel> optionalValues by Map<String, Tupel> optionalValues where the key is Tupel#id and add a List<String> property to the bean containing all available Tupel#id values.
<p:columns value="#{tableOverviewBean.availableTupelIds}" var="id" headerText="#{id}">
#{data.optionalValues[id].value}
</p:columns>
java:
#Named
#ViewScoped
public class LiveRangeService implements Serializable {
private List< Map<String, ColumnModel> > tableData;
private List<ColumnModel> tableHeaderNames;
public List<Map<String, ColumnModel>> getTableData() {
return tableData;
}
public List<ColumnModel> getTableHeaderNames() {
return tableHeaderNames;
}
public void PlayListMB() {
tableData = new ArrayList< Map<String, ColumnModel> >();
//Generate table header.
tableHeaderNames = new ArrayList<ColumnModel>();
for (int j = 0; j < 5; j++) {
tableHeaderNames.add(new ColumnModel("header "+j, " col:"+ String.valueOf(j+1)));
}
//Generate table data.
for (int i = 0; i < 10; i++) {
Map<String, ColumnModel> playlist = new HashMap<String, ColumnModel>();
for (int j = 0; j < 5; j++) {
playlist.put(tableHeaderNames.get(j).key,new ColumnModel(tableHeaderNames.get(j).key,"row:" + String.valueOf(i+1) +" col:"+ String.valueOf(j+1)));
}
tableData.add(playlist);
}
}
static public class ColumnModel implements Serializable {
private String key;
private String value;
public ColumnModel(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
}
And XHTML:
<h:form>
<p:dataTable id="tbl" var="result"
value="#{liveRangeService.tableData}"
rendered="#{not empty liveRangeService.tableData}"
rowIndexVar="rowIndex"
>
<f:facet name="header"> header table </f:facet>
<p:column>
<f:facet name="header">
<h:outputText value="序号" />
</f:facet>
<h:outputText value="#{rowIndex+1}" />
</p:column>
<p:columns value="#{liveRangeService.tableHeaderNames}"
var="mycolHeader" columnIndexVar="colIndex">
<f:facet name="header">
<h:outputText value="#{mycolHeader.value}" />
</f:facet>
<h:outputText value="#{result[mycolHeader.key].value}" />
<br />
</p:columns>
</p:dataTable>
</h:form>
That's a example.
I'm Using Primefaces 3.5. I have around 10 input text fields in my .xhtml page.Few text fields are made mandatory with attribute required="true".
I have a search button that displays Data from Database in a Data Table.The functionality of my page is to insert the values into these fields by on row select() the data in the Data Table of Search Button.
The Problem here is the data is inserting into the Fields which are highlighted with the red border ie fields with validations applied.
Example:
Transport Field has no validation but it had value that has to be inserted. These type of things are happening to many of my Input Fields.
Please give me some suggestions.
.xhtml file is:
<p:inputText id="email" value="#{addcust.c.email}" required="true"
validatorMessage="Enter Valid Email">
<f:validateRegex pattern="^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*#[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$"/></p:inputText>
<h:outputLabel value="Transport"></h:outputLabel>
<p:inputText value="#{addcust.c.transport}" </p:inputText>
<p:commandButton value="add" type="submit" update=":form,:msg" actionListener="#{addcust.onAddSelect}"</p:commandButton>
<p:commandButton value="Search" type="submit" onclick="ser.show() "></p:commandButton>
<p:dialog id="dialog11" header=" Search" widgetVar="ser" resizable="false" showEffect="fade"
hideEffect="explode" >
<p:dataTable id="dt" var="sd" value="#{addcust.al}" selection="#{addcust.c}">
<p:ajax event="rowSelect" update=":form" listener="#{addcust.onRowSelect}"
oncomplete="ser.hide()"/>
<p:column>
<f:facet name="header">
<h:outputText value="Email"/>
</f:facet>
<h:outputText value="#{sd.email}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Transport"/>
</f:facet>
<h:outputText value="#{sd.transport}"/>
</p:column>
</p:dataTable>
customerbean.java
public class customerbean {
private String email;
private String transport;
public String getTransport() {
return transport;
}
public void setTransport(String transport) {
this.transport = transport;
}
return email;
}
public void setEmail(String email) {
this.email = email;
}
addcust.java
public customerbean c = new customerbean();
public ArrayList<customerbean> al;
public void onAddSelect(){
// Inserted my values into customer table.
}
public void onSearchSelect() {
try {
st = con.createStatement();
ResultSet rs = st.executeQuery("select * from customer where cmpid=" + getCurrcompanyid() + "");
al = new ArrayList<customerbean>();
while (rs.next()) {
customerbean s = new customerbean();
s.setEmail(rs.getString(1));
s.setTransport(rs.getString(2));
}
} catch (Exception e) {
System.out.println(e);
}
}
public void onRowSelect(SelectEvent event) throws SQLException {
customerbean r = (customerbean)event.getObject();
c = r;
}
If I'm not clear enough please leave me a comment .Thanks for Reading.
For some strange reason I can't manage to process my data when I'm using <p:ajax event="sort" inside <p:dataTable , while it works just fine for <p:ajax event="page" and <p:ajax event="filter"
I'm using myfaces 2.0.11 with primefaces 3.4 (tried with 4.0 snapshot)
If I edit my cell input and do PAGINATE I do see the updated value of the cell in the listener method
If i edit my cell input and do SORT I do NOT see the updated value of the cell in the listener method, Any Idea why? I mean both p:ajax (sort/page) got the process="#form" so why its does not process? I inspected the network of the chrome dev tools and in both cases (sort/page) the relevant updated values are being sent to the server, but for some reason in the sort listener the values are not updated ,
b.t.w the only suspicious difference between page and sort ajax request is the following parameter that is present only in the sort request is:
my_table_skipChildren:true , what does it mean? skip from processing ?
I can grab the relevant ids+value from the request from the ExternalContext, but it will be last resort only
Here is a really simple example
Session Scoped Bean code:
#ManagedBean
#SessionScoped
public class myBean {
private List<MyObject> myList = new ArrayList<MyObject>();
#PostConstruct
public void init() {
myList.add(new MyObject("1", "2"));
myList.add(new MyObject("11", "22"));
myList.add(new MyObject("111", "222"));
myList.add(new MyObject("1a", "2a"));
myList.add(new MyObject("11a", "22a"));
myList.add(new MyObject("111a", "222a"));
}
public List<MyObject> getMyList() {
return myList;
}
public void setMyList(List<MyObject> myList) {
this.myList = myList;
}
}
MyObject code
public class MyObject {
private String one;
private String two;
public MyObject(String one, String two) {
super();
this.one = one;
this.two = two;
}
public String getOne() {
return one;
}
public void setOne(String one) {
this.one = one;
}
public String getTwo() {
return two;
}
public void setTwo(String two) {
this.two = two;
}
}
XHTML :
<p:dataTable id="my_table" value="#{myBean.myList}"
var="deviceRow" widgetVar="myTable"
paginator="true" rows="4" paginatorPosition="bottom"
paginatorTemplate="{FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}">
<p:ajax process="#form" event="sort" listener="#{myBean.detectSortEvent}"></p:ajax>
<p:ajax process="#form" event="page" listener="#{myBean.detectPageEvent}"></p:ajax>
<p:column id="table_selection_column_one" sortBy="#{deviceRow.one}" filterBy="#{deviceRow.one}">
<f:facet name="header">
<h:outputText value="Column One" />
</f:facet>
<h:inputText id="myRow_One" value="#{deviceRow.one}" />
</p:column>
<p:column id="table_selection_column_two" sortBy="#{deviceRow.two}" filterBy="#{deviceRow.two}">
<f:facet name="header">
<h:outputText value="Column Two" />
</f:facet>
<h:inputText id="myRow_Two" value="#{deviceRow.two}" />
</p:column>
</p:dataTable>
(I also asked it on primefaces forum but got no response)
Tried Andy workaround (adding editable="true" to table, but its no good
The scenario is as follows: change some value of an input text , than click on table column header to do sort
Expectations: Inside detectSortEvent listener I see the updated value inside the list
Reality: Same old value in the list
Regards,
Daniel.
I am trying to fetch the checkbox selected values selectedCars from primefaces datatable to my managed bean named TableBean.I am getting null pointer exception at the point where I am trying to fetch the values in the function getSelection() please help me with this
My JSF page:
<h:form id="form">
<p:dataTable id="multiCars" var="car"
value="#{tableBean.mediumCarsModel}" paginator="true" rows="10"
selection="#{tableBean.selectedCars}">
<f:facet name="header">
Checkbox Based Selection
</f:facet>
<p:column selectionMode="multiple" style="width:2%" />
<p:column headerText="Model" style="width:25%">
#{car.model}
</p:column>
<p:column headerText="Year" style="width:25%">
#{car.year}
</p:column>
<p:column headerText="Manufacturer" style="width:24%">
#{car.manufacturer}
</p:column>
<p:column headerText="Color" style="width:24%">
#{car.color}
</p:column>
<f:facet name="footer">
<p:commandButton id="multiViewButton" value="View"
icon="ui-icon-search" update=":form:displayMulti"
oncomplete="multiCarDialog.show()" />
</f:facet>
</p:dataTable>
<p:dialog id="multiDialog" header="Car Detail"
widgetVar="multiCarDialog" height="300" showEffect="fade"
hideEffect="explode">
<p:dataList id="displayMulti" value="#{tableBean.selectedCars}"
var="selectedCar">
Model: #{selectedCar.model}, Year: #{selectedCar.year}
</p:dataList>
</p:dialog>
</h:form>
My managed bean
#SessionScoped
#ManagedBean
public class TableBean implements Serializable {
private List<Car> cars;
private Car[] selectedCars;
private CarDataModel mediumCarsModel;
Connection connection;
Statement stmt;
ResultSet rs;
public TableBean() {
cars = new ArrayList<Car>();
getCars();
getSelection();
mediumCarsModel = new CarDataModel(cars);
}
public Car[] getSelectedCars() {
return selectedCars;
}
public void setSelectedCars(Car[] selectedCars) {
this.selectedCars = selectedCars;
}
public void getCars() {
int i = 0;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:jtds:sqlserver://cvgapp106I/dev2_LPSR");
System.out.println("connected to the database");
stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select * from test");
while(rs.next()) {
cars.add(i,new Car(rs.getString("Model"),rs.getInt("Year"),rs.getString("Manufacturer"),rs.getString("Color")));
i++;
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void getSelection() {
System.out.println(selectedCars[0].getModel());
}
public CarDataModel getMediumCarsModel() {
return mediumCarsModel;
}
public void setMediumCarsModel(CarDataModel mediumCarsModel) {
this.mediumCarsModel = mediumCarsModel;
}
}
public void getSelection() {
System.out.println(selectedCars[0].getModel());
}
This will throw NullPointerException if the list is empty.
If you really want to use this then you should try check if the list not null
if(selectedCars!=null && !selectedCars.isEmpty()){
System.out.println(selectedCars[0].getModel());
}
Do not call getSelection() in your constructor. There selectedCars will be always null
I have the following method in my DAO where I retrieve a list of persons :
public List<Personne> getAllUsers() {
Query query = em.createQuery("SELECT p FROM Personne p where TYPE(p) =Utilisateur");
#SuppressWarnings("unchecked")
List <Personne> personnes = query.getResultList();
return personnes;
}
I want to show the list of persons in a datatable :
<p:dataTable value="#{desacBean.users}" var="us" paginator="true" selection="# {desacBean.selectedUser}" selectionMode="single" rowKey="#{desacBean.getId(us)}}" first="1">
<p:ajax event="rowSelect" listener="#{desacBean.onUserSelect}"/>
<p:column>
<f:facet name="n">
<h:outputText value="nom" />
</f:facet>
<h:outputText value="#{us.nom}"/>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="prenom" />
</f:facet>
<h:outputText value="#{us.prenom}"/>
</p:column>
</p:dataTable>
my BEAN :
#ManagedBean(name="desacBean")
#SessionScoped
public class DesactiveBean implements Serializable{
private static final long serialVersionUID = 1L;
private List<Personne> users = new ArrayList<Personne>();
private Personne selectedUser;
private boolean panelRendered;
UserDAO daoUser = new UserDaoImpl();
public void rowSelect(SelectEvent event){
selectedUser = (Personne)event.getObject();
System.out.println("selectedUser = "+selectedUser.getNom_ut());
this.panelRendered = true;
}
public int getId(Personne car)
{
return System.identityHashCode(car);
}
public void onUserSelect(SelectEvent event){
this.selectedUser = (Personne)event.getObject();
System.out.println("selectedUser = "+selectedUser.getNom_ut());
}
I have the following error when trying to show this dataTable :
java.lang.NumberFormatException: For input string: "prenom"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
For input string: "prenom"
how can I fix it?
You'll probably encountering the same issue as the question answered here: NumberFormatException for input String