Cant set List<String> value from <ui:repeat> var status - java

in my xhtml i define a inputText variable from which i want to set a specific value to a specific field in my List. Every time i click confirm and debug my code the value is returned as "" (that i set in my init function). I have also tried hardcoding my inputText value to be set to dashboardFilterDialogBean.dashboardObject.texts[0] as in the only item in the list i define but still no result. I get 0 exceptions in my console
Xhtml
<p:dialog id="dashboardFilterDialog">
<h:form id="dashboard_filter_dialog_form">
<ui:repeat value="#{dashboardFilterDialogBean.dashboardObject.texts}" varStatus="loop">
<p:row>
<p:column>
<h:outputText value="Text" />
</p:column>
<p:column>
<p:inputText value="#{dashboardFilterDialogBean.dashboardObject.texts[loop.index]}" />
</p:column>
</p:row>
</ui:repeat>
</h:form>
<f:facet name="footer">
<h:form id="submit_dashboards_dialog_form">
<p:commandButton id="confirm_button" actionListener="#{dashboardFilterDialogBean.confirm()}" />
</h:form>
</f:facet>
</p:dialog>
Bean
public class DashboardFilterDialogBean {
private DashboardObject dashboardObject;
#PostConstruct
public void init() {
dashboardObject.getTexts().add("");
}
public void confirm() {
for (String txtValue : getDashboardObject().getTexts()) {
if (!txtValue.equals("")) {
...;
}
}
}
public DashboardObject getDashboardObject() {
return dashboardObject;
}
public void setDashboardObject(DashboardObject dashboardObject) {
this.dashboardObject = dashboardObject;
}
}
Object
public class DashboardObject {
private List<String> texts;
public DashboardObject() {
texts = new ArrayList<String>();
}
public List<String> getTexts() {
return texts;
}
public void setTexts(List<String> texts) {
this.texts = texts;
}
}

I've fixed my issue by adding <p:ajax event="blur" process="#this" /> into my inputText so it saves the value before i click confirm. So the end code looks like this:
<p:inputText value="#{dashboardFilterDialogBean.dashboardObject.texts[loop.index]}" />
<p:ajax event="blur" process="#this" />
</p:inputText>

Related

Select Row in primefaces data table

I have this table:
<h:form>
<p:dataTable id="books" value="#{ordersBean.userOrders}" var="book" selection="#{ordersBean.selectedBook}">
<p:column>
<f:facet name="header">Order ID</f:facet>
<h:outputText value="#{book[0]}"/>
</p:column>
<p:column>
<f:facet name="header">Title</f:facet>
<h:outputText value="#{book[1]}"/>
</p:column>
<p:column>
<f:facet name="header"></f:facet>
<p:commandButton id="selectRowBtn" value="select" action="#{ordersBean.showSelectedBook}"/>
</p:column>
</p:dataTable>
</h:form>
I want when i click on each select button, it's row information assigned to selectedBook property and displays it in showSelectedBook() method:
Here is the ordersBean:
private Book selectedBook = new Book();
public Book getSelectedBook() {
return selectedBook;
}
public void setSelectedBook(Book selectedBook) {
this.selectedBook = selectedBook;
}
public void showSelectedBook() {
System.out.println("In selected Book(), book: " + getSelectedBook());
}
But result is this:
In selected Book(), book: null
Should be something like this:
XML code:
<p:commandButton id="selectRowBtn" value="select"
action="#{ordersBean.showSelectedBook}">
<f:param name="bookId" value="#{book[0]}" />
</p:commandButton>
Java bean method:
public void showSelectedBook() {
Map<String,String> params =
FacesContext.getExternalContext().getRequestParameterMap();
int bookId = Integer.valueOf(params.get("bookId"));
for(Book book : bookList){
if(book.bookId == bookId){
selectedBook = book;
break;
}
}
System.out.println("In selected Book(), book: " + getSelectedBook());
}
Beside, you must have knowledge about the patterns for sending parameters to the actions, refer below link.
http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/
If you want to show the selected book you have to set selectionMode="single" and there's no need to put a commandButton in each row, just specify only one commandButton in the footer facet like this:
<f:facet name="footer">
<p:commandButton id="selectRowBtn" value="select" action="#{ordersBean.showSelectedBook}"/>
</f:facet>
And your main problem here is that you are setting a new Book() to your selectedBook variable, so a null value to your selectedBook , this declaration:
private Book selectedBook = new Book();
Should be :
private Book selectedBook;
You don't have to instantiate a new Book() in your selectedBook.
Take a look at the second Example in this Showcase, to see how it works.
better solution and without select button :
Xml code:
<p:dataTable id="ListBook"
value="#{ordersBean.bookList}"
selection="#{ordersBean.selectedBook}" var="book"
rowKey="#{book.id}" selectionMode="single">
<p:ajax event="rowSelect"
listener="#{ordersBean.onRowSelectDataTable()}"
update="ListBook" />
..... <columns> ..
</p:datatable>
Java bean:
private Book selectedBook=new Book();
private boolean headerButtonsDisabled=true;
//add a List object for all books (bookList) with getter and setter
public boolean isHeaderButtonsDisabled() {
return headerButtonsDisabled;
}
public void setHeaderButtonsDisabled(boolean headerButtonsDisabled) {
this.headerButtonsDisabled = headerButtonsDisabled;
}
public void onRowSelectDataTable() {
this.setHeaderButtonsDisabled(false);
}
public Book getSelectedBook() {
return selectedBook;
}
public void setSelectedBook(Book selectedBook) {
this.selectedBook = selectedBook;
}

Updating a selected row in datatable in jsf

I am Creating a jsf application and I need to perform CRUD. So far I have managed to delete,create,and read but am unable to update the record.So my problem is, I want when the user click the update button a dialog box to pop with the details of the selected row and update the details. Here is My sample code.
<p:panelGrid columns="2">
<h:outputLabel value="Account Id"/>
<h:inputText value="#{accCtr.acc.accountNum}" />
<h:outputLabel value="Account Bal"/>
<h:inputText value="#{accCtr.acc.balance}"/>
<h:outputLabel />
<p:commandButton action="#{accCtr.create()}" value="Enter" update="dt"/>
</p:panelGrid>
<p:dataTable value="#{accCtr.list}" var="i" id="dt" style="width: 40%;" rowStyleClass="height" rowKey="#{accCtr.acc.accountNum}" >
<p:column>
<f:facet name="header">Account Num</f:facet>
#{i.accountNum}
</p:column>
<p:column>
<f:facet name="header">Account Balance</f:facet>
#{i.balance}
</p:column>
<p:column>
<f:facet name="header">Action</f:facet>
<p:commandButton value="Remove" styleClass="height"
action="#{accCtr.removeAccount(i)}"
/>
<p:commandButton value="Edit" styleClass="height"
onclick="pop.show()"
action="#{accCtr.edit(i)}"
>
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
<p:dialog widgetVar="pop" header="Account Edit">
<h:form>
<p:panelGrid columns="2">
<h:outputLabel value="Account Balance"/>
<h:inputText value="#{accCtr.acc.balance}"/>
<h:outputLabel/>
<p:commandButton value="Update"/>
</p:panelGrid>
</h:form>
</p:dialog>
can someone help me.
and my backing bean.
#ManagedBean(name="accCtr")
#SessionScoped
public class AccountController {
List<AccountTable> list=new ArrayList<>();
public AccountController() {
}
private Account_dao getDao()
{
return new Account_dao();
}
public List<AccountTable> getList() {
return getDao().findAll();
}
public void setList(List<AccountTable> list) {
this.list = list;
}
public void removeAccount(AccountTable acc) {
getDao().remove(acc);
}
public AccountTable acc=new AccountTable();
public AccountTable getAcc() {
return acc;
}
public void setAcc(AccountTable acc) {
this.acc = acc;
}
public void edit(AccountTable acc) {
setAcc(acc);
}
public String create()
{
this.acc.setUserid(10);
getDao().create(this.acc);
return "index";
}
Change your
<p:commandButton value="Edit" styleClass="height"
onclick="pop.show()"
action="#{accCtr.edit(i)}"
/>
to
<p:commandButton value="Edit" styleClass="height"
oncomplete="pop.show()"
actionListener="#{accCtr.edit(i)}"
process="#this" update=":your_dialog_form_id"
/>
A few things: In general the action atributte is used for navigation (redirect to another page for instance). Also is better to use oncomplete beacuse it gets executed when your ajax request is completed, instead of onclick that fires the action (open the dialog in your case) in the moment when you press the button, skipping validation and such things.
If your problem was, refreshing the dialog content, with the update/process (ajax) mechanism, will be updated with your current selection if you do like the second snippet.

Primefaces 4.0 dataTable dialog details doesn't working

I'm using PrimeFaces 4.0 and Netbeans 6.9.1. I followed primefaces demo here:
DataTable Single Selection
Everything working fine expect button view. Here is my code:
Customer_list.xhtml
<p:growl id="msgs" showDetail="true" />
<h:form id="formTable">
<p:dataTable styleClass="table" id="customers" var="customer" value="#{customerBean.customer}">
<p:column>
<f:facet name="header">First Name</f:facet>
#{customer.firstName}
</p:column>
<p:column>
<f:facet name="header">Last Name</f:facet>
#{customer.lastName}
</p:column>
<p:column>
<f:facet name="header">Email</f:facet>
#{customer.email}
</p:column>
<p:column>
<f:facet name="header">DOB</f:facet>
#{customer.dob}
</p:column>
<p:column style="width:4%">
<p:commandButton id="selectButton" update=":formCreate" oncomplete="dialogCustomerCreate.show()" icon="ui-icon-search" title="Update">
<f:setPropertyActionListener value="#{customer}" target="#{customerBean.selectedCustomer}" />
</p:commandButton>
</p:column>
</p:dataTable>
</h:form>
<h:form id="formCreate">
<p:dialog header="Create New Customer" widgetVar="dialogCustomerCreate" resizable="false" id="dlgCustomerCreate"
showEffect="fade" hideEffect="explode" modal="true">
<h:panelGrid id="display" columns="2" cellpadding="4" style="margin:0 auto;">
<h:outputText value="First Name:" />
<h:outputText value="#{customerBean.selectedCustomer.firstName}" style="font-weight:bold"/>
<h:outputText value="Last Name:" />
<h:outputText value="#{customerBean.selectedCustomer.lastName}" style="font-weight:bold"/>
<h:outputText value="Email:" />
<h:outputText value="#{customerBean.selectedCustomer.email}" style="font-weight:bold"/>
<h:outputText value="DOB:" />
<h:outputText value="#{customerBean.selectedCustomer.dob}" style="font-weight:bold"/>
</h:panelGrid>
</p:dialog>
</h:form>
customerBean.java
public class customerBean {
private List<Customer> customer;
private Customer selectedCustomer;
/** Creates a new instance of customerBean */
public customerBean() {
customer = new ArrayList<Customer>();
}
public List<Customer> getCustomer() {
CustomersDao cust_dao = new CustomersDao();
customer = cust_dao.findAll();
return customer;
}
public Customer getSelectedCustomer() {
return selectedCustomer;
}
public void setSelectedCustomer(Customer selectedCustomer) {
this.selectedCustomer = selectedCustomer;
}
}
CustomersDao.java
public class CustomersDao {
public List<Customer> findAll(){
List<Customer> list_cust = null;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
String sql = "FROM Customer";
try{
session.beginTransaction();
list_cust = session.createQuery(sql).list();
session.beginTransaction().commit();
}catch(Exception e){
session.beginTransaction().rollback();
}
return list_cust;
}
}
Hope anyone suggest me what wrong in my code. It takes me 2 days to solve it. Thanks for reading!
You need to have a converter for your Customer class:
#FacesConverter(forClass = Customer.class)
public class CustomerConverter implements Converter {
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value != null && !value.equals("") && !value.equals("0")) {
//find and return object from DAO
} else {
return null;
}
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
//return object id as string
}
}
1) Try removing your <h:form/>, since you're not submitting any information in that dialog;
2) Try to change your <p:commandButton update=""/> to update=":display";
3) Add process="#form" to your <p:commandButton/>;
4) If that works, i don't think you need that <h:form/>.
you did not mention the correct id for displaying popup dialog at commandButton attribute update.
<p:commandButton id="selectButton" update=":formCreate:display" oncomplete="dialogCustomerCreate.show()" icon="ui-icon-search" title="Update">
<f:setPropertyActionListener value="#{customer}" target="#{customerBean.selectedCustomer}" />
</p:commandButton>

JSF / Primefaces dataTable and sorting issue

I am using PrimeFaces 2.2, with JSF 2.0.3.
I have an xhtml view that contains the following datatable:
<p:dataTable id="fooTable"
widgetVar="fooTable"
rowKey="#{foo.id}"
var="foo"
value="#{fooQueue.foos}"
styleClass="table"
selection="#{fooQueue.selectedfoo}"
filteredValue="#{fooQueue.filteredfoos}"
paginatorPosition="bottom"
paginatorAlwaysVisible="true"
selectionMode="single"
rowEditListener="#{fooQueue.save}"
paginator="true"
rows="10"
rowIndexVar="#{foo.id}"
emptyMessage="No foos found with given criteria"
rowStyleClass="#{foo.status == const.submitted ? 'gray' : null}"
onRowSelectUpdate=":form:deleteValidation">
<p:column id="mothersName" filterBy="#{foo.header1}" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Mother's Name" styleClass="tableHeader2" />
</f:facet>
<p:commandLink action="#{fooQueue.next(foo)}"
ajax="false"
disabled="#{foo.status == const.submitted || foo.id == 0}">
<f:setPropertyActionListener value="#{foo}" target="#{fooQueue.selectedfoo}" />
<h:outputText value="#{foo.header1}" styleClass="tableData" />
</p:commandLink>
</p:column>
<p:column sortBy="#{foo.header4}" >
<f:facet name="header">
<h:outputText value="DOB" styleClass="tableHeader2" style="width: 400px" />
</f:facet>
<h:outputText value="#{foo.header4}" styleClass="#{(foo.overSevenDays and foo.status != const.submitted and foo.id != 0)?'tableDataRed':'tableData'}" />
</p:column></p:dataTable>
.. with the following backing bean:
#ViewScoped
#ManagedBean
public class FooQueue extends BaseViewBean {
private List foos;
private Foo selectedFoo;
private List filterdFoos;
public FooQueue() {
logger.debug("Creating new FooRegQueue");
retrieveFooRecords();
}
private void retrieveFooRecords(){
foos = new ArrayList();
foos.addAll(fooService.getAllFoos());
}
public String next(Foo clickedFoo) {
System.out.println(clickedFoo.getId());
return "";
}
public Collection getFoos() {
return foos;
}
public Foo getSelectedFoo() {
return selectedFoo;
}
public void setSelectedFoo(Foo foo) {
if (foo != null) {
this.selectedFoo = foo;
}
}
public void setFilterdFoos(List filterdFoos) {
this.filterdFoos = filterdFoos;
}
public List getFilterdFoos() {
return filterdFoos;
}
}
Note the "commandLink" on the mother's name column, and the "sortBy" on the DOB column.
I've encountered a strange issue, which seems to be limited to IE, where if I sort the data by DOB, then paginate to the last page and click the commandLink of the last record in the table, it fires two action events. The first event correctly reports that I clicked on the last record in the sorted table. But the second invocation of the next() method is for the last record in the UNsorted table.
Can anyone identify anything wrong with my xhtml, or backing bean? Is this a known PrimeFaces issue? A known IE issue?
I am testing with IE 8.
I solved it. I simply had to change this:
<p:commandLink action="#{fooQueue.next(foo)}"
ajax="false"
disabled="#{foo.status == const.submitted || foo.id == 0}">
<f:setPropertyActionListener value="#{foo}" target="#{fooQueue.selectedfoo}" />
<h:outputText value="#{foo.header1}" styleClass="tableData" />
</p:commandLink>
to this:
<p:commandLink action="#{fooQueue.next(foo)}"
ajax="true"
disabled="#{foo.status == const.submitted || foo.id == 0}">
<f:setPropertyActionListener value="#{foo}" target="#{fooQueue.selectedfoo}" />
<h:outputText value="#{foo.header1}" styleClass="tableData" />
</p:commandLink>
Note the ajax="true". Because it was previously set to ajax="false", it was instantiating a new instance of my backing bean, and the row that was selected in the default sort order was being loaded. Now it does not instantiate a new instance, and I load the actual record I clicked on.

Filter DataTable in Primefaces only works once

I'm trying to filter a dataTable using Primefaces much like this example. (In a web browser) I type the text I want to filter by, it works once but when I remove the text I've written the result stays the same when it should go back to it's original state.
So it works once and then won't respond. (I can remove or re-type the filter text I've written but it does not affect the table anymore)
Sorry about the weird attribute names in the code, bear with me. :)
xhtml-page:
<h:form>
<p:dataTable var="aggr" value="#{aggregationManagedBean.logiskAdressatModel}"
widgetVar="aggrTable"
emptyMessage="No aggr found with given criteria">
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Filter:" />
<p:inputText id="globalFilter" onkeyup="aggrTable.filter()" />
</p:outputPanel>
</f:facet>
<p:column filterBy="#{aggr.name}">
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{aggr.name}" />
</p:column>
</p:dataTable>
</h:form>
backing bean:
#ManagedBean
#SessionScoped
public class AggregationManagedBean {
private List<LogiskAdressat> logiskaAdressater;
private DataModel<LogiskAdressat> logiskAdressatModel;
public AggregationManagedBean() {
logiskaAdressater = getLogiskaAdressater();
logiskAdressatModel = new ListDataModel<LogiskAdressat>(logiskaAdressater);
}
private static List<LogiskAdressat> getLogiskaAdressater(){
List<LogiskAdressat> logiskaAdressater = new ArrayList<LogiskAdressat>();
logiskaAdressater.add(new LogiskAdressat("01 addr_id 01", "Joe"));
logiskaAdressater.add(new LogiskAdressat("02 addr_id 02", "John"));
logiskaAdressater.add(new LogiskAdressat("03 addr_id 03", "Jake"));
return logiskaAdressater;
}
public DataModel<LogiskAdressat> getLogiskAdressatModel() {
return logiskAdressatModel;
}
public void setLogiskAdressatModel(DataModel<LogiskAdressat> adressatModel) {
this.setLogiskAdressatModel(adressatModel);
}
}
Is LogiskAdressat Serializable?
If not, then try making it Serializable -
public class LogiskAdressat implements Serializable {
//....

Categories