So I am trying to use a PrimeFaces data table to set data to a list as follows;
<h:form id="frmSalarySupplement">
<p:panel>
<p:dataTable id="tblSalarySupplement"
value="#{salSupplementMB.dataListFromDB}"
var="salSupp"
rowIndexVar="rowSn"
scrollable="true"
rows="10"
paginator="true"
editable="true"
editMode="cell"
rowsPerPageTemplate="10,20,50,100">
<p:ajax event="cellEdit"
listener="#{salSupplementMB.onCellEdit}"
update="#this" />
<p:column headerText="#" escape="false"
style="white-space:pre-line;
word-break:break-all;
width:20px;
text-align:center;">
<h:outputText value="#{rowSn+1}" />
</p:column>
<p:column headerText="Name"
style="white-space:pre-line;
word-break:break-all;
width:250px;">
<h:outputText value="#{salSupp.empId.name}" />
</p:column>
<p:column headerText="Designation"
style="white-space:pre-line;
word-break:break-all;
width:150px;">
<h:outputText value="#{salSupp.empId.designation.designationName}" />
</p:column>
<p:column headerText="Allowance"
style="white-space:pre-line;
word-break:break-all;
width:100px;">
<p:cellEditor>
<f:facet name="input">
<h:inputText value="#{salSupp.allowance}"
style="width:100%" />
</f:facet>
<f:facet name="output">
<h:outputText value="#{salSupp.allowance}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Special
Allowance"
style="white-space:pre-line;
word-break:break-all;
width:100px;">
<p:cellEditor>
<f:facet name="input">
<h:inputText value="#{salSupp.specialAllowance}"
style="width:100%" />
</f:facet>
<f:facet name="output">
<h:outputText value="#{salSupp.specialAllowance}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Transport
Allowance"
style="white-space:pre-line;
word-break:break-all;
width:100px;">
<p:cellEditor>
<f:facet name="input">
<h:inputText value="#{salSupp.transportAllowance}"
style="width:100%" />
</f:facet>
<f:facet name="output">
<h:outputText value="#{salSupp.transportAllowance}" />
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
</p:panel>
</h:form>
So I want to be able to save the data I have edited on the cell to set it on the list. When I do the following, I don't get an updated value.
public void onCellEdit(CellEditEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
}
Can someone suggest what shall I do with it?
Update
Also, can anyone tell me why my table loads multiple times when I edit the value? Maybe that's what's giving me the problem.
Related
I am having issue in firing an ajax call on the cellEdit event of a Data Table. The table shows up just fine on the UI but nothing happens when I click any of the cell.
xhtml
<h:form>
<p:dataTable id="decisionTree" var="tree"
value="#{treeBean.content}" editable="true" editMode="cell"
styleClass="smallGrid">
<f:facet name="header">
Notes Decision Tree
</f:facet>
<p:ajax event="cellEdit" listener="#{treeBean.onCellEdit}"
immediate="true" update=":#{p:component('notesTextArea')}" />
<p:column headerText="Comment Type">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tree.commentType}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tree.commentType}" disabled="true" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="MTCNs">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tree.mtcns}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tree.mtcns}" disabled="true" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Call Type">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tree.callType}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tree.callType}" disabled="true" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Phone">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tree.phone}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tree.phone}" disabled="true" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Dispute Reason">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tree.disputeReason}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tree.disputeReason}" disabled="true" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Placement Decision">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{tree.placementDescision}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{tree.placementDescision}" disabled="true" />
</f:facet>
</p:cellEditor>
</p:column>
<!--
<p:column>
<p:rowEditor />
</p:column>
-->
</p:dataTable>
</h:form>
Here is the Bean.
#Component("treeBean")
#Scope(value = "view", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class TreeBean {
private List<TreeDto> content;
private String result="";
public List<TreeDto> getContent() {
return content;
}
public void setContent(List<TreeDto> content) {
this.content = content;
}
#PostConstruct
public void init() {
content=new ArrayList<TreeDto>();
TreeDto dto1=new TreeDto();
dto1.setCommentType("First Attempt");
dto1.setMtcns("mtcn1");
dto1.setCallType("OBC");
dto1.setPhone("8975730838");
dto1.setDisputeReason("Fraud");
dto1.setPlacementDescision("Write Off");
content.add(dto1);
}
public void onCellEdit(CellEditEvent event) {
RequestContext.getCurrentInstance().showMessageInDialog(new FacesMessage(FacesMessage.SEVERITY_INFO, "Status","something clicked"));
}
}
My intention to capture the value of the cells clicked. But I am not able to get an event fired in the first place on cell edit. Please give me some suggestions on how to resolve this.
Try to add a widgetVar to your dataTable and then edit your cell with a contextMenu for your data table, which call onclick="PF('yourWidgetVar').showCellEditor();return false;"
Somenthing like this
<p:contextMenu for="decisionTree" widgetVar="cMenu">
<p:menuitem value="Edit Cell" icon="ui-icon-search" onclick="PF('yourWidgetVar').showCellEditor();return false;"/>
<p:menuitem value="Hide Menu" icon="ui-icon-close" onclick="PF('cMenu').hide()"/>
</p:contextMenu>
You can take a look to Primefaces documentation too:
https://www.primefaces.org/showcase/ui/data/datatable/edit.xhtml
I am new to primefaces. I have one datatable in my application. In that for 3 columns i need to merge some rows. That row count will be generated dynamically based on the data. If I tried to use rowspan in that particular column tag, it additionally created blank cells and the total datatable format is changed. How to do merge that datarows in primefaces. For reference, I am attaching the design and code.
Xhtml code:
<f:facet name="header">
<h:outputText value="Date"></h:outputText>
</f:facet>
<h:outputText value="#{step2table.rs2_date}" />
</p:column>
<p:column style="font-size:12px;width:73px;" >
<f:facet name="header">
<h:outputText value="Slot"></h:outputText>
</f:facet>
<h:outputText value="#{step2table.rs2_slot}" />
</p:column>
<p:column style="font-size:12px;width:73px;" >
<f:facet name="header">
<h:outputText value="Number of Resources Required"></h:outputText>
</f:facet>
<h:outputText value="#{step2table.rs2_noresources}" />
</p:column>
<p:column headerText="Select" id="hSelect" style="font-size:12px;width:60px;">
<p:selectBooleanCheckbox id="Booleanchkbox" onclick="{dtstep2_tab1.select='true'}" >
<p:ajax listener="#{dtstep2_tab1.UpdateStatus}" update="panel1" />
</p:selectBooleanCheckbox>
</p:column>
<p:column style="font-size:12px;width:70px;">
<f:facet name="header">
<h:outputText value="Employee Name"></h:outputText>
</f:facet>
<h:outputText value="#{step2table.rs2_empname}" />
</p:column>
<p:column style="font-size:12px;width:120px;">
<f:facet name="header">
<h:outputText value="Contact Details"></h:outputText>
</f:facet>
<h:outputText value="#{step2table.rs2_contactdet}" />
</p:column>
<p:column style="font-size:12px;width:70px;">
<f:facet name="header">
<h:outputText value="Worked Hrs to Date"></h:outputText>
</f:facet>
<h:outputText value="#{step2table.rs2_totworkhours}" />
</p:column>
<p:column style="font-size:12px;width:50px;">
<f:facet name="header">
<h:outputText value="Agency Recruited"></h:outputText>
</f:facet>
<h:outputText value="#{step2table.rs2_recruitmode}" />
</p:column>
<p:column style="font-size:12px;width:70px;">
<f:facet name="header">
<h:outputText value="Priority"></h:outputText>
</f:facet>
<h:outputText value="#{step2table.rs2_priority}"/>
</p:column>
<p:column style="font-size:12px;width:90px;">
<f:facet name="header">
<h:outputText value="Type of Skill"></h:outputText>
</f:facet>
<h:outputText value="#{step2table.rs2_skilltype}" />
</p:column>
<p:column style="font-size:12px;width:120px;">
<f:facet name="header">
<h:outputText value="Restriction"></h:outputText>
</f:facet>
<h:outputText value="#{step2table.rs2_restriction}" />
</p:column>
<p:column style="font-size:12px;width:150px;">
<f:facet name="header">
<h:outputText value="Rest. Details"></h:outputText>
</f:facet>
<h:outputText value="#{step2table.rs2_restdet}" />
</p:column>
<p:column style="font-size:12px;width:70px;">
<f:facet name="header">
<h:outputText value="B2B Days"></h:outputText>
</f:facet>
<h:outputText value="#{step2table.rs2_b2bdays}" />
</p:column>
<p:column style="font-size:12px;width:70px;">
<f:facet name="header">
<h:outputText value="B2B Hrs"></h:outputText>
</f:facet>
<h:outputText value="#{step2table.rs2_b2bhrs}" />
</p:column>
<p:column headerText="Status" id="hStatus" style="font-size:12px;width:100px;">
<h:selectOneMenu id="cbo" value="#{step2table.rs2_status}" >
<f:selectItem itemLabel="Confirmed" itemValue="Confirmed"></f:selectItem>
<f:selectItem itemLabel="Selected" itemValue="Selected"></f:selectItem>
<f:selectItem itemLabel="Rejected" itemValue="Rejected"></f:selectItem>
</h:selectOneMenu>
</p:column>
<p:column style="font-size:12px;width:150px;">
<f:facet name="header">
<h:outputText value="Comments"></h:outputText>
</f:facet>
<h:inputText value="#{step2table.rs2_comments}" rendered="#{dtstep2_tab1.editable}"/>
</p:column>
Need to merge first three columns "date, Slot, and No of Resources Required" based on the employee column data. Guide me on this. Thanks in advance.
You can use p:summaryRow tag in p:dataTable. Example:
<h:form>
<p:dataTable var="car" value="#{dtSummaryRowView.cars}" sortBy="#{car.brand}">
<p:column headerText="Id" sortBy="#{car.id}">
<h:outputText value="#{car.id}" />
</p:column>
<p:column headerText="Year" sortBy="#{car.year}">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Brand" sortBy="#{car.brand}">
<h:outputText value="#{car.brand}" />
</p:column>
<p:column headerText="Color" sortBy="#{car.color}">
<h:outputText value="#{car.color}" />
</p:column>
<p:summaryRow>
<p:column colspan="3" style="text-align:right">
<h:outputText value="Total:" />
</p:column>
<p:column>
<h:outputText value="#{dtSummaryRowView.randomPrice}">
<f:convertNumber type="currency" currencySymbol="$" />
</h:outputText>
</p:column>
</p:summaryRow>
</p:dataTable>
</h:form>
For more information, see the primefaces showcase link: http://www.primefaces.org/showcase/ui/data/datatable/summaryRow.xhtml
This is simply not possible in the datatable.
<p:column width="440">
<p:panelGrid rendered="#{someCondition}">
<f:facet name="header">
<p:row>
<p:column width="80"><h:outputText value="#{msg.isin}" /></p:column>
<p:column width="80"><h:outputText value="#{msg.wkn}" /></p:column>
<p:column width="200"><h:outputText value="#{msg.name}" /></p:column>
<p:column width="80"><h:outputText value="#{msg.amount}" /></p:column>
</p:row>
</f:facet>
<p:row>
<p:column><h:outputText value="#{savingsPlanPosition.isin}" /></p:column>
<p:column><h:outputText value="#{savingsPlanPosition.wkn}" /></p:column>
<p:column><h:outputText value="#{savingsPlanPosition.name}" /></p:column>
<p:column><h:outputText value="#{savingsPlanPosition.amount}" /></p:column>
</p:row>
</p:panelGrid>
<p:panelGrid rendered="#{!someCondition}">
<h:link id="savingsPlanPositionDetails${rowIndex}"
title="#{msg.assign_etf}"
outcome="portfolioPositionDetail">
<f:param name="portfolioId"
value="#{savingsPlanController.portfolioId}"></f:param>
<f:param name="portfolioPositionId"
value="#{savingsPlanPosition.portfolioPositionId}"></f:param>
<f:param name="from"
value="savingsPlan"></f:param>
<h:outputText value="#{msg.assign_etf}"/>
</h:link>
</p:panelGrid>
</p:column>
I have a problem in datatable when adding a new line:
I want to add a blank line, but if I click on the button it adds a line but component remains.
<p:selectCheckboxMenu> contains old value.
That is to say value of <p:selectCheckboxMenu> preserves the value of the previous line
<p:dataTable value="#{solotionManager.customers}"
id="table" var="o"
widgetVar="50"
style="width: 100%;"
editable="true"
scrollHeight="330"
styleClass="datatable">
<p:ajax event="rowEdit" listener="#{customerBeansController2.onEdit}" update=":form1:table" process="#this"/>
<f:facet name="header">
Order List
</f:facet>
<p:ajax event="rowEditCancel" listener="#{customerBeansController2.onCancel}" update=":form1:messages"/>
<p:column>
<f:facet name="header" >
<h:outputText value="ID customer" style="width:100%"/>
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{o.id}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{o.id}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="firstName"/>
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{o.firstName}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{o.firstName}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="lastName"/>
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{o.lastName}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{o.lastName}" style="width:100%"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="SelectManyMenu" style="width:100%"/>
</f:facet>
<p:cellEditor>
<f:facet name="input">
<p:selectCheckboxMenu value="#{solotionManager.choice}" label="Phone" converter="entrantConverter">
<f:selectItems value="#{solotionManager.listeChoice}" var="c" itemValue="#{c}" itemLabel="#{c}"/>
</p:selectCheckboxMenu >
</f:facet>
<f:facet name="output">
<p:selectCheckboxMenu value="#{solotionManager.choice}" label="Phone" converter="entrantConverter">
<f:selectItems value="#{solotionManager.listeChoice}" var="c" itemValue="#{c}" itemLabel="#{c}"/>
</p:selectCheckboxMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Options">
<p:rowEditor/>
</p:column>
</p:dataTable>
public String newLine(javax.faces.event.ActionEvent actionEvent) {
this.customers.add(this.custom1);
this.custom1=new Customer();
return null;
}
the code for the button that adds the line:
<p:commandButton value="add row" actionListener="#{solotionManager.newLine}" update="table" oncomplete="addRowOnComplete()"/>
Function javascript addRowOncomplete :
<script type="text/javascript">
function addRowOnComplete() {
jQuery('.ui-datatable-data tr td').last().find('span.ui-icon-pencil').each(function(){jQuery(this).click();});
}
</script>
I have a datatable element where I use a rowEditor. Additionally it is possible to add a new record which is implemented as inputFields in the footer of the table. All elements are validated. The problem is, that when a user hits the rowEditor the validation complains that the values for a new record are not set.
How can I avoid that the new record fields are being validated when the rowEditor is clicked?
I fiddled around a bit with proccess & immediate on the ajax events but I'm not able to figure this out. Is this possible and if yes, how? I'm thankful for help.
<p:dataTable var="contact"
value="#{data.activeCustomer.contacts.toArray()}"
id="contactsTable"
emptyMessage="#{ivy.cms.co('/Translations/Administration/noRecordsFoundSearch')}"
editable="true">
<p:ajax event="rowEdit" listener="#{logic.onEdit(contact)}" />
<p:ajax event="rowEditCancel" listener="#{logic.onCancel}" />
<f:facet name="header">
#{ivy.cms.co('/Translations/Administration/Contacts/labelContacts')}
</f:facet>
<p:column
headerText="#{ivy.cms.co('/Translations/Administration/Contacts/prename')}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{contact.prename}" />
</f:facet>
<f:facet name="input">
<p:inputText required="true"
requiredMessage="#{ivy.cms.co('/Translations/CustomerAdministration/Validation/enterPrename')}"
value="#{contact.prename}"></p:inputText>
</f:facet>
</p:cellEditor>
<f:facet name="footer">
<p:inputText required="true"
requiredMessage="#{ivy.cms.co('/Translations/CustomerAdministration/Validation/enterPrename')}"
value="#{data.contactPrename}" />
</f:facet>
</p:column>
<p:column
headerText="#{ivy.cms.co('/Translations/Administration/Contacts/surname')}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{contact.surname}" />
</f:facet>
<f:facet name="input">
<p:inputText required="true"
requiredMessage="#{ivy.cms.co('/Translations/CustomerAdministration/Validation/enterSurname')}"
value="#{contact.surname}"></p:inputText>
</f:facet>
</p:cellEditor>
<f:facet name="footer">
<p:inputText required="true"
requiredMessage="#{ivy.cms.co('/Translations/CustomerAdministration/Validation/enterSurname')}"
value="#{data.contactSurname}" />
</f:facet>
</p:column>
<p:column
headerText="#{ivy.cms.co('/Translations/Administration/Contacts/phone')}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{contact.phone}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{contact.phone}"
requiredMessage="#{ivy.cms.co('/Translations/CustomerAdministration/Validation/enterPhone')}"
required="true"></p:inputText>
</f:facet>
</p:cellEditor>
<f:facet name="footer">
<p:inputText
requiredMessage="#{ivy.cms.co('/Translations/CustomerAdministration/Validation/enterPhone')}"
required="true" value="#{data.contactPhone}" />
</f:facet>
</p:column>
<p:column
headerText="#{ivy.cms.co('/Translations/Administration/Contacts/email')}">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{contact.email}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{contact.email}" required="true"
requiredMessage="#{ivy.cms.co('/Translations/CustomerAdministration/Validation/enterEmail')}">
<f:validator validatorId="EmailValidator" />
</p:inputText>
</f:facet>
</p:cellEditor>
<f:facet name="footer">
<p:inputText required="true"
requiredMessage="#{ivy.cms.co('/Translations/CustomerAdministration/Validation/enterEmail')}"
value="#{data.contactEmail}">
<f:validator validatorId="EmailValidator" />
</p:inputText>
</f:facet>
</p:column>
<p:column style="width: 6%"
headerText="#{ivy.cms.co('/Translations/CustomerAdministration/actionsColumnTitle')}">
<p:rowEditor />
<p:commandLink styleClass="ui-icon ui-icon-trash"
actionListener="#{logic.deleteContact(contact)}"
icon="ui-icon-trash" update=":#{p:component('contactForm')}"
immediate="true" />
<f:facet name="footer">
<h:commandButton
value="#{ivy.cms.co('/Translations/Administration/Contacts/ButtonAddContact')}"
actionListener="#{logic.addContact}" />
</f:facet>
</p:column>
</p:dataTable>
</h:form>
My web page looks like this:
<h:form>
<p:dataTable var="car" value="#{tableBean.carsSmall}">
<f:facet name="header">
Expand rows to see detailed information
</f:facet>
<p:column style="width:16px">
<p:rowToggler />
</p:column>
<p:column style="width:250px">
<f:facet name="header">
Model
</f:facet>
<h:outputText value="#{car.model}" />
</p:column>
<p:column style="width:250px">
<f:facet name="header">
Year
</f:facet>
<h:outputText value="#{car.year}" />
</p:column>
<p:rowExpansion>
<p:fieldset legend="Detail">
<p:dataTable value="#{car.colors}" var="color">
<p:column>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{color.disable}" />
</f:facet>
<f:facet name="input">
<h:selectBooleanCheckbox value="#{color.disable}" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Edit">
<p:rowEditor />
</p:column>
</p:dataTable>
</p:fieldset>
</p:rowExpansion>
</p:dataTable>
</h:form>
This component is automatically updating the wrong values in database, for example it is setting false for the color.disable property upon just expanding the row.
I am using:
JSF 2.0.9
Primefaces 2.2.1