Delete Rows Using Checkbox in JSF - jsf

I am trying to delete multiple rows of employee data using checkboxes. However i am having a hard time finding a way to do this. The following is some code:
DeleteEmployees.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:loadBundle basename="resources.application" var="msg"/>
<head>
<title><h:outputText value="#{msg.welcomeTitle}" /></title>
</head>
<h:body>
<h:dataTable value="#{empController.list}" var="emp" border="2">
<h:column>
<f:facet name="header">
<h:outputText value="First Name"/>
</f:facet>
<h:outputText value="#{emp.firstName}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Last Name"/>
</f:facet>
<h:outputText value="#{emp.lastName}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Email"/>
</f:facet>
<h:outputText value="#{emp.email}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Phone"/>
</f:facet>
<h:outputText value="#{emp.phone}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Hire Date"/>
</f:facet>
<h:outputText value="#{emp.hireDate}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Manager ID"/>
</f:facet>
<h:outputText value="#{emp.managerId}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Department ID"/>
</f:facet>
<h:outputText value="#{emp.departmentId}"/>
</h:column>
<h:column>
<f:facet name="header">Delete</f:facet>
<h:form>
<h:selectBooleanCheckbox value="#{empController.checked[emp.id]}"/>
</h:form>
</f:facet>
</h:column>
</h:dataTable>
<h:commandButton value="Delete Employees" action="#{empController.delEmp}"/>
</h:body>
</html>
In my Managed bean i have this method in order to delete the ID. However the objects are not being deleted. I need some guidance here thank you!
public void delEmp(){
list = em.getAllEmployees();
for(Employee e : list){
if(checked.get(e.getId())){
em.deleteEmployee(e.getId());
}
}
checked.clear();
}

Your h:commandButton is not nested within the h:form. It needs to be nested for the form data to submit. Note that this is not a JSF limitation, but an HTML limitation.
See also
Call another form from the command button

Related

p:dataTable not updated by PrimeFaces.current().ajax().update

I am starting developpment with JSF and PrimeFace datatable. I have prepared the following xhtml page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Find Capital of Countries</title>
</h:head>
<h:body>
<h:form id="FrmTest">
<p:dataTable id="ListSites" value="#{testing.SiteSearch()}" var="lst" editable="true">
<f:facet name="header">
List of Sites
</f:facet>
<p:column>
<f:facet name="header">
Site Code
</f:facet>
#{lst.stCode}
</p:column>
<p:column headerText="Site Description">
<h:outputText value="#{lst.stDescription}" />
</p:column>
<p:column headerText="is Active">
<div style="text-align: center;">
<p:selectBooleanCheckbox value="#{lst.stActive}" />
</div>
</p:column>
<p:column>
<f:facet name="header">
Site Active
</f:facet>
<div style="text-align: center;">
<h:outputText value= "#{lst.stActive}" />
</div>
</p:column>
<p:column>
<f:facet name="header">
Modified By
</f:facet>
#{lst.modifiedBy}
</p:column>
<p:column>
<f:facet name="header">
Modification Date
</f:facet>
#{lst.modifiedDate}
</p:column>
<p:column>
<f:facet name="header">
Has Error
</f:facet>
#{lst.hasError}
</p:column>
<p:column>
<f:facet name="header">
Error Description
</f:facet>
#{lst.errorDescription}
</p:column>
<p:column>
<f:facet name="header">
Row State
</f:facet>
#{lst.rowState}
</p:column>
</p:dataTable>
<h:outputLabel value="Enter Country Name:"/>
<h:inputText id="country" binding="#{testing.country}"
valueChangeListener="#{testing.findCapitalListener}"
immediate="true"
onchange="document.getElementById('findcapital').click();" />
<br/>
<br/>
<h:outputLabel value="Capital is: " />
<h:inputText id="capital" binding="#{testing.capital}" immediate="true"/>
<br/>
<br/>
<h:commandButton value="Submit" partialSubmit="true" />
<div style="visibility: hidden" >
<h:commandButton id="findcapital" value="findcapital" partialSubmit="true" immediate="true" />
</div>
</h:form>
</h:body>
</html>
I want entry made in text box named country to update datatable named ListSites. The associated listener is called (system.out.println made) but the primeface update is not made. Here is the listener:
```
public void findCapitalListener(ValueChangeEvent cdl) {
String country = cdl.getNewValue().toString();
System.out.println("Country is : " + country);
StringBuilder capitalCountry = new StringBuilder();
findCapital(country, capitalCountry);
capital.setValue(capitalCountry.toString());
siteDto = listSiteDto.get(0);
siteDto.setStDescription(capitalCountry.toString());
listSiteDto.set(0, siteDto);
PrimeFaces.current().ajax().update("FrmTest:ListSites");
//System.out.println("Capital is : " + capital.getValue());
//System.out.println("New DTO Description : " + siteDto.getStDescription());
System.out.println("New list Description : " + listSiteDto.get(0).getStDescription());
}
```
The problem was due to the fact that the function populating the datatable (SiteSearch()) was retrieving data from the database. At that stage I wanted to refresh the page display before updating the database. I replaced the function by the list getter, things are working properly now.

jsf h:dataTable - not refreshing after deleting row

Hello Guys i need your help as usual
Jsf h:dataTable - not refreshing after deleting row.
Not until i have to restart the programm.
what can i do please to resolve this problem?
Thanks John.
ManagedBean
public List<Customer> getCustomerList() {
if (customerList == null) {
customerList = new ArrayList<Customer>(bookstoreManager.getAllCustomers());
for (Customer cus : customerList) {
System.out.println("Hello" + cus.getFirstName() + "......." + cus.getLastName());
}
}
return customerList;
}
Business Logik
public List<Customer> getAllCustomers() {
TypedQuery<Customer> query = em.createQuery("Select c from Customer c", Customer.class);
return query.getResultList();
}
XHTML Code
<ui:define name="content">
<h:body>
<h:outputStylesheet name="css/style.css" />
<h:form id="form">
<h:dataTable var="cus" cellpadding="5" cellspacing="5" border="1"
width="400" styleClass="customerList"
value="#{customerBean.getCustomerList()}">
<h:column>
<f:facet name="header">
<h:outputText value="#{texts.firstName}" />
</f:facet>
<h:outputText value="#{cus.firstName}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{ texts.lastName}" />
</f:facet>
<h:outputText value="#{cus.lastName}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{texts.address}" />
</f:facet>
<h:outputText value="#{cus.address}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{texts.creditCard}" />
</f:facet>
<h:outputText value="#{cus.creditCard}" />
</h:column>
<h:column>
<h:commandLink action="#{customerBean.selectCustomer(cus)}"
value="Edit" />
</h:column>
<h:column headerText="Delete">
<h:commandLink value="#{texts.delete}"
action="#{customerBean.deleteCustomer(cus)}" />
</h:column>
</h:dataTable>
</h:form>
</h:body>
</ui:define>

Conditional column rendering

I'm working with jsf 2.0. I have this datatable
<h:dataTable value="#{agreement.licenseProducts}"
var="licenseProduct"
styleClass="pnx-table pnx-table-bdr pnx-table-unstyled mp-license-info">
<h:column>
<f:facet name="header">
<h:outputText value="Product" />
</f:facet>
<h:outputText value="#{licenseProduct.description}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Serial Number" />
</f:facet>
<h:outputText value="#{licenseProduct.serialNumber}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="#{agreement.labelForConcurrency}" />
</f:facet>
<h:outputText value="#{licenseProduct.concurrent}" />
</h:column>
<ui:fragment rendered="#{agreement.managementTool != 'NONE'}">
<h:column>
<f:facet name="header">
<h:outputText value="#{agreement.labelForLicenseType}" />
<span class="pnx-icon-right pnx-icon-info pnx-tooltip">
<div class="pnx-tooltip-content">
<h:outputText value="Tooltip content" />
</div>
</span>
</f:facet>
<h:outputText value="#{licenseProduct.licenseBase}" />
</h:column>
</ui:fragment>
<h:column>
<f:facet name="header">
<h:outputText value="#{agreement.labelForSeatCount}" />
</f:facet>
<h:outputText value="#{licenseProduct.seats}" />
</h:column>
</h:dataTable>
The problem is that the ui:fragment part is not working. No matter what the attribute's value is, it will NEVER show the column.
Any ideas?
--EDIT--
Just in case, I have other ui:fragments that depend on that same attribute, and they do render correctly depending on the attribute's value. I'm sure it has to do with the dataTable and the columns.
The rendered's attribute of <h:column> tag performs simply the job :
<h:column rendered="#{agreement.managementTool != 'NONE'}">
...
</h:column>

Why does <h:column><f:facet name="header"><h:outputText> render as th instead of td

I have below code:
<h:dataTable class="pretty" value="#{ftManagedBean.ftDataModel}" >
<h:column>
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Outstanding"/>
</f:facet>
</h:column>
</h:dataTable>
But it prints Name and Outstanding between <th></th> tags. Why? I need them in <td></td> tags. How can I do that?
Name and Outstanding end up in <th> elements because you placed them inside <f:facet name="header"> tags. Remove those and they'll be output to <td> tags.
<h:dataTable class="pretty" value="#{ftManagedBean.ftDataModel}" >
<h:column>
<h:outputText value="Name"/>
</h:column>
<h:column>
<h:outputText value="Outstanding"/>
</h:column>
</h:dataTable>
See the JSF HTML Tag Reference for an example on creating a table.
You have to change your code into this:
<h:dataTable class="pretty" value="#{ftManagedBean.ftDataModel}" >
<h:column>
<h:outputText value="Name"/>
</h:column>
<h:column>
<h:outputText value="Outstanding"/>
</h:column>
</h:dataTable>
If you wrap your output text within the <f:facet name="header"> this corresponds to <th> (table header), otherwise they will be printed in <td>.
I try this solution and ok.
(http://www.coderanch.com/t/431222/JSF/java/dynamically-set-panel-header-condition)
<rich:dataGrid value="#{myMB.student.list}" rendered="#{!empty myMB.student and !empty myMB.student.list}" var="st" rowKeyVar="row">
<rich:panel>
<f:facet name="header">
<h:panelGroup id="panelHeader">
<h:outputText value="Just one student" id="header1" required="true" rendered="#{!myMB.manyStudents}"/>
<h:outputText value="#{row+1}ยบ Student" id="header2" required="true" rendered="#{myMB.manyStudents}"/>
</h:panelGroup>
</f:facet>
<rich:panel>

primefaces 3.2 datatable incell editing not working

I have a datatable with incell editing which displays the records correctly but when I try to edit a row, the change is not reflected. Following is the xhtml code:
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{scrip.companyName}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{scrip.companyName}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Exchange">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{scrip.exchange}"/>
</f:facet>
<f:facet name="input">
<p:selectOneMenu style="width: 150px" value="#{manageScrip.exchange}">
<f:selectItem itemLabel="nse" itemValue="nse"/>
<f:selectItem itemLabel="bse" itemValue="bse"/>
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Brokerage Charge">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{scrip.brokerageCharge}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{scrip.brokerageCharge}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Options">
<p:rowEditor/>
</p:column>
</p:dataTable>
</h:form>
</h:body>
Following is the managed bean function:
public void updateScrip()
{
try{
Integer i=(Integer)dt.getRowKey();
String str=i.toString();
sc=(ScripMaster) dt.getRowData(str);
scripSymbol=sc.getScripSymbol();
exchange=sc.getExchange();
companyName=sc.getCompanyName();
updateScrip(i, scripSymbol, companyName, exchange,);
}catch(Exception ex){
}
}
Alternatively I also tried:
public void updateScrip(RowEditEvent e)
{
try{
sc=(ScripMaster) e.getObject();
scripSymbol=sc.getScripSymbol();
exchange=sc.getExchange();
companyName=sc.getCompanyName();
Integer i=sc.getScripID();
updateScrip(i, scripSymbol, companyName, exchange);
}catch(Exception ex){
}
}
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form id="frm">
<p:commandLink action="/adminpages/addScrip" value="add scrip" ajax="false"/>
<p:dataTable editable="true" var="scrip" value="#{manageScrip.scripList}" binding="#{manageScrip.dt}" rowKey="#{scrip.scripID}" id="dt">
<p:ajax event="rowEdit" listener="#{manageScrip.updateScrip}" update="#this"/>
<p:column headerText="Scrip Symbol">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{scrip.scripSymbol}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{scrip.scripSymbol}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Company Name">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{scrip.companyName}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{scrip.companyName}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Exchange">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{scrip.exchange}"/>
</f:facet>
<f:facet name="input">
<p:selectOneMenu style="width: 150px" value="#{manageScrip.exchange}">
<f:selectItem itemLabel="nse" itemValue="nse"/>
<f:selectItem itemLabel="bse" itemValue="bse"/>
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Brokerage Charge">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{scrip.brokerageCharge}"/>
</f:facet>
<f:facet name="input">
<p:inputText value="#{scrip.brokerageCharge}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Options">
<p:rowEditor/>
</p:column>
</p:dataTable>
</h:form>
</h:body>
The most important part is missing the datatable tag.
Make sure the "editable" attribute is set to true. This attribute was invented in version 3.2 i think, making many onld datatables not editable.
Try using h:inputText instead of p:inputText.

Resources