I am trying to implement primefaces treetable with checkbox and multiselection, like the one in primefaces showcase, everything works as expected but when I try to get the list of selected nodes, I only get the top nodes. So for example, if the user selects a child node of "Movies", it is not present in the TreeNode[] selectedNodes. Why is it behaving like this? What am I missing?
My codes are
In .xthml file
<p:treeTable value="#{backingBean.theTree}" var="tree" id="tree"
selection="#{backingBean.selectedNodes}" selectionMode="checkbox">
<p:column style="width:150px">
<f:facet name="header">
Name
</f:facet>
<span title="#{tree.objectCode}"><h:outputText value="#{tree.objectName}" /></span>
</p:column>
<p:column style="width:150px">
<f:facet name="header">
Object Description
</f:facet>
<h:outputText value="#{tree.objectDescription}" />
</p:column>
</p:treeTable>
Backingbean:
this.theTree = new DefaultTreeNode("root", null);
TreeNode eachParent = new DefaultTreeNode(new Document(...some properties...), root);
And I have a recursive loop which basically creates more nodes in the same way.
Related
I have a dataTable which has cell editing and global filter capability:
<p:dataTable id="tbl01" value="#{userMappingBacking.staffList}" editable="true" editMode="cell" filteredValue="#{userMappingBacking.staffListFiltered}" var="s" rowKey="#{s.id}" widgetVar="tbl101WV">
...
<f:facet name="header">
<p:outputPanel>
<p:toolbar>
<p:toolbarGroup align="${text.line_start}">
<p:commandButton value="#{text.save}" action="#{userMappingBacking.saveChanges}" update="msgs"/>
</p:toolbarGroup>
<p:toolbarGroup align="${text.line_end}">
<h:outputText value="#{text.search_all_fields}:"/>
<p:spacer width="10"/>
<h:inputText id="globalFilter" onkeyup="PF('tbl101WV').filter()" />
</p:toolbarGroup>
</p:toolbar>
</p:outputPanel>
</f:facet>
...
<p:column headerText="#{text.user}" filterBy="#{s.userName}" filterable="false">
<p:cellEditor id="ceUser">
<f:facet name="output"><h:outputText value="#{s.userName}"/></f:facet>
<f:facet name="input">
<p:autoComplete id="acUser" dropdown="true" value="#{s.userName}" completeMethod="#{userMappingBacking.completeUnAssignedUser}"/>
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
and the backing bean userMappingBacking supports all the methods and works fine.
My problem is quite strange, the <p:autoComplete id="acUser"... doesn't invoke the complete method unless a filter is applied to the table, after a filter application every thing works fine, but before that no values are completed in the p:autoComplete.
Could any one help or give an insight into this.
I actually found the problem to be in the backing bean, I had 3 lists:
staffList = new ArrayList<>();
staffListFiltered = new ArrayList<>();
userDTOList = new ArrayList<>();
that I fill in a viewAction listener
public void loadStaffListAction() {
staffList = zaties.listAllStaff();
staffListFiltered = zaties.listAllStaff();
userDTOList = iacw.listAllUsers();
}
the problem was having 2 different lists staffList and staffListFiltered filled with different objects from the service, so changing the code like this:
public void loadStaffListAction() {
staffList = zaties.listAllStaff();
staffListFiltered = staffList;
userDTOList = iacw.listAllUsers();
}
and this solved the problem, nevertheless this is not the best practice, as explained below.
Common Error:
in the primefaces showcase for filtered datatable the never initialize the filteredValue list to any thing, they leave it as null so the datatable filter logic initialize it in the right way, paying attention to this detail I advise:
Don't initialize any thing unless you get an error.
comments are welcome!
I'm not sure if this is a limitation or a coding error on my part. I have a Primefaces DataTable that allows the user to select it (single select radio) and then click on a CommandButton to edit the data in a Dialog.
In the HTML code where I display the selected object, I omitted the ID, Lat, and Long attributes because a human user shouldn't be touching these attributes. The Dialog is showing the selected attributes correctly, however, when I tried to find the existing record using the selected object's ID for update, it thrown a no result error. When I checked the selected object's ID attribute, it has a value of NULL.
I tinker around with the HTML code and found that in order to have the any of the attributes copied to the selected object, the attributes must be shown in the XHTML. When I include the attribute as with Render = False, the attribute will have a value of NULL. Any suggestions on hiding attributes that human users shouldn't modify beside using Disable tag attribute?
Class Property{
Int ID;
String Address;
Long Lat;
Long Long;
String Desc;
}
public void modifyCurrentProject() {
HashMap<String, Object> params = new HashMap();
params.put("id", currentProject.getId());
//fails to find Project by ID as currentProject.getID == null
Project temp = (Project) dbu.findByNamedQuery("project.findID", params);
//Omit statements to overwrite attached object's values before writing back to database
dbu.modifyEntity(); //dbu is another object to control database access
currentProject = null;
}
<h:body>
<p:growl id="projectMsg" showDetail="true"/>
<h:form id="form">
<p:dataTable id="projectTable" var="project" value="#{projectBacking.builder.projects}" selection="#{projectBacking.currentProject}" rowKey="#{project.id}">
<f:facet name="header">My Project List
<p:commandButton value="Add" type="button" onclick="PF('projectAdd').show()"/>
<p:commandButton value="Edit" update=":editForm" oncomplete="PF('projectEdit').show()"/>
</f:facet>
<p:column selectionMode="single"/>
<p:column headerText="ID">
<h:outputText value="#{project.id}"/>
<p:column headerText="Description">
<h:outputText value="#{project.property.description}"/>
</p:column>
<p:column headerText="Street">
<h:outputText value="#{project.street}"/>
</p:column>
<p:column headerText="City">
<h:outputText value="#{project.city}"/>
</p:column>
<p:column headerText="Province">
<h:outputText value="#{project.state}"/>
</p:column>
<p:column headerText="Latitude">
<h:outputText value="#{project.latitude}"/>
</p:column>
<p:column headerText="Longitude">
<h:outputText value="#{project.longitude}"/>
</p:column>
<p:column headerText="Status">
<h:outputText value="#{project.status}"/>
</p:column>
</p:dataTable>
</h:form>
<!-- Omitted the Add Project Dialog box -->
<p:dialog header="Edit Project" modal="true" height="400" width="80%" widgetVar="projectEdit">
<h:form id="editForm">
<p:panel id="editProject" header="Edit Project" toggleable="true" closable="false">
<!--<h:inputText id="id" value="#{projectBacking.currentProject.id}"/>-->
<h:inputText id="country" value="#{projectBacking.currentProject.country}"/>
<h:inputText id="lat" rendered="true" value="#{projectBacking.currentProject.latitude}"/>
<h:inputText id="long" rendered="true" value="#{projectBacking.currentProject.longitude}"/>
<h:outputLabel for="desc" value="Description" />
<h:inputText id="desc" value="#{projectBacking.currentProject.description}"/>
<h:outputLabel for="street" value="Street" />
<h:inputText id="street" value="#{projectBacking.currentProject.street}"/>
<h:message for="street"/>
<h:outputLabel for="city" value="City:" />
<h:inputText id="city" required="false" value="#{projectBacking.currentProject.city}"/>
<h:message for="city" />
<h:outputLabel for="state" value="Province" />
<h:inputText id="state" required="true" value="#{projectBacking.currentProject.state}"/>
<h:commandButton value="Save Project" type="submit" action="#{projectBacking.modifyCurrentProject()}">
</h:commandButton>
</p:panel>
</h:form>
</p:dialog>
</h:body>
After placing some more breakpoints in the code, I discovered that upon saving the changes in the Dialog, it's creating a new backing bean. If the is set to Render=false, Disabled=true, or ReadOnly=true then new backing bean's object's value will not be set and retain the initial value as specified in the object's constructor.
Basically, the cause is incorrect scope. I was using Request scope when the problem occurred. However, changing to Session scope resolved it.
I've done some research and haven't been able to find anything that directly addresses the problem I've been having.
I have a DataTable column that is editable (by cell) and I have the ability to click on the cell, enter a new number in the cell editor box, but when I hit the cell editor closes but does not save the new value (same thing happens if you just click off and don't hit enter).
Here is the Code Snippet
<h:form>
<c:forEach items="#{extensionsBean.getPhases()}" var="phase">
<p:fieldset legend="#{phase.getPhaseName()}">
<p:dataTable value="#{extensionsBean.getActivities(phase)}" var="activity" editable="true" editMode="cell">
<p:column>
<f:facet name="header">
<h:outputText value="Actual Hours"/>
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{activity.getEstimateDetail().actualHours}"/>
</f:facet>
<f:facet name="input">
<h:inputText value="#{activity.getEstimateDetail().actualHours}" />
</f:facet>
</p:cellEditor>
//the rest of the closing tags are present
Any suggestions would be great! For the record, I have basically the exact same setup in a different xhtml page and I'm able to edit each of the cells without a problem. Not sure what's causing this one to bug out on me.
Add Ajax event cellEdit and store the values
Add ajax event celledit and where do you want save the value you can save it.
Code is below in my project
Xhtml page(Primefaces):
<p:dataTable var="my" value="#{java.custom_info}" editMode="cell" editable="true" style="font-size: 12px">
<p:ajax event="cellEdit" listener="#{java.custom_detail}"/>
<p:column style="background: white">
<h:outputText value="First Name"/>
</p:column>
<p:column style="background: white">
<p:cellEditor>
<f:facet name="output"> <h:outputText value="#{my.first}"/> </f:facet>
<f:facet name="input"><p:inputText value="#{my.first}" placeholder="Enter First Name" style="width:93%"/></f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
Java Class
public void custom_detail(CellEditEvent event){
String old_str = (String) event.getOldValue();
String first_name = (String) event.getNewValue();
System.out.println("update method reached..."+first_name);
FacesContext fc = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
Query db = new Query();
Customer bean = new Customer();
if(session!=null){
if(((String)session.getAttribute("login"))!=null){
Calendar currentDate=Calendar.getInstance();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datenow=format.format(currentDate.getTime());
session.setAttribute("firstname", first_name);
session.setAttribute("modified_date",datenow);
bean.setFirst(first_name);
bean.setUsrid((String)session.getAttribute("user"));
bean.setUsr_modified(datenow);
db.update_personalinfo(bean);
}
}
}
After filtering with filterBy selected var instance get lost.
<p:dataTable id="searchResults"
var="searchResult"
value="#{employeeController.searchResults}"
selectionMode="single"
selection="#{employeeController.selectedEmployee}"
rowKey="#{searchResult.id}">
<p:column headerText="Employee Name"
filterBy="#{searchResult.name}"
sortBy="#{searchResult.name}">
<h:outputText value="#{searchResult.name}"/>
</p:column>
</p:dataTable>
<p:commandButton actionListener="#{employeeController.editEmployee(searchResult)}"
icon="ui-icon-pencil"
title="Güncelle" ajax="true" update="#form">
my event does not get the selected searchResult instance but instead a null value
I solved the problem. I was just missing filteredValue.
Currently using primefaces 3.3 and want to refresh the datatable on removing an item from the list.
When I refresh the page, do see updated table, so the list is getting updated in java code.
Thanks for your time and help.
Please let me know where I am misktaken.
Here's the relevant code:
//in xhtml
<p:dataTable id="userConnections" var="connection" value="#{loginBean.userConnectionEmailList}">
<p:column>
<h:outputText value="#{connection.email}" />
</p:column>
<p:column>
<p:commandButton id="deleteButton" icon="ui-icon-close" title="Delete" process="#this" action="#{loginBean.deleteConnectionAction}" update="userConnections">
<f:setPropertyActionListener value="#{connection}" target="#{loginBean.selectedUserConnection}"/>
</p:commandButton>
</p:column>
</p:dataTable>
// java code
public String deleteConnectionAction() {
System.out.println("###deleteConnectionAction ###" + selectedUserConnection.getEmail() );
userConnectionEmailList.remove(selectedUserConnection);
return "";
}