Editable Data table in request scop JSF2.0 - jsf-2

Hi All
I have list of module object. I want to show it to user and user can edit it and can save it also. In module object three members are there 1 String moduleName 2.boolean readFlag
3. writFlag.
The bean i am using is in Request Scop i don't want to use any other scop.
My problem is setterOfThatList is not called when update button is clicked.
Where as on the same page i used another inputText, setter of the variable which is connected to this inputText is called.
<h:dataTable value="#{roleBean.listMatrixDo}" var="list">
<h:column>
<f:facet name="header">
<h:outputText value="Module Name"></h:outputText>
</f:facet>
<h:outputText value="#{list.moduleDo.moduleName}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Read Access"></h:outputText>
</f:facet>
<h:selectBooleanCheckbox value="#{list.readFlag}" />
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Write Access"></h:outputText>
</f:facet>
<h:selectBooleanCheckbox value="#{list.writeFlag}" />
</h:column>
</h:dataTable>

My problem is setterOfThatList is not called when update button is clicked
The setter is never called for collection and nested properties. JSF does basically as follows:
((RowObject) bean.getList().get(rowIndex)).setSomeProperty(submittedValue);
It gets the list and then gets the row object by current index and then sets the property on it. As you see, it's important that the very same list is already prepared in the subsequent request (i.e. you need to do it in constructor/postconstruct of the bean).
This is regardless of the scope. In view and session scope, however, you don't need to prepare the list in subsequent requests. I'd recommend to use the view scope when data loading from the DB is been involved.
If your intent is to process/save the submitted data, then you should just do the job in the bean's action method which you attached to the commandbutton.
public void submit() {
someService.save(list);
}

Related

PrimeFaces CommandLink in dynamic datatable

I have a breadcrumb and datatable
Number of columns is dynamic. (ie: On clicking "Header7" in breadcrumb, 7 header column plus one "More-Info" column is to be rendered).
I created data-table using primefaces-datatable-columns
My xhtml file is :
<p:dataTable id="dataMain" var="car" value="#{orgUnitBean.rows}">
<p:columns value="#{orgUnitBean.columns}" var="column"
columnIndexVar="colIndex" sortBy="#{car[column.property]}" filterBy="#{car[column.property]}">
<f:facet name="header">
<h:outputText value="#{column.header}" />
</f:facet>
<h:outputText value="#{car[column.property]}" />
</p:columns>
</p:dataTable>
Question:
how to add commandLink in "More-Info" column. (currently I am just displaying "id" of last "Header" entity in it. In image attached: 3 is id of unitL3 and 13 is id of Lnitu3. I want to replace it with commandLink which will invoke bean method and pass these id as parameter to it).
Thanks
Use the rendered attribute of a commandLink in combination with the colIndex. Put this in the column like this
<p:commandLink rendered="#{colIndex == 5}" ... />
Where 5 is the index of the more-info column. You can also do a string compare on the colum header if you want.
(This is a very basic jsf pattern btw)

primefaces p:commandLink in p:datatable doesn't work [duplicate]

This is the code:
<h:form id="articleForm" >
<p:commandButton value="Select tags" ajax="true" >
<f:ajax execute="#form" render="#form :articleForm:tags" />
</p:commandButton>
<p:pickList id="tags" value="#{articleController.dualListModelForTags}" var="tag" itemLabel="#{tag.tag}" itemValue="#{tag}" converter="distinctTagConverter">
<f:facet name="sourceCaption">Distinct tags</f:facet>
<f:facet name="targetCaption">Connected tags</f:facet>
</p:pickList>
</h:form>
When the commandbutton is clicked, the getDualListModelForTags() in the backing bean is called and executed. In getDualListModelForTags() I make some modifications so I want the picklist to be updated. But the picklist(id=tags) is not rendered again. Only when I refresh the page, are the modifications made to the picklist.
The PrimeFaces <p:commandButton> component doesn't work together with <f:ajax>. You need to use the button's own ajax-targeted attributes instead. Instead of the <f:ajax execute> you should use <p:commandButton process>. But this already defaults to #form, so you can omit it. Instead of the <f:ajax render> you should use <p:commandButton update>. Specifying client IDs which are already covered by #form is unnecessary, so just #form is sufficient. Also the ajax="true" attribute is unnecessary as that's the default already.
So just this should do:
<p:commandButton value="Select tags" update="#form" />
Unrelated to the concrete problem, you're doing the business job inside a getter method. This is a bad idea. Do it in the button's action method instead. You also seem to be using a session scoped bean for view scoped data. This is a bad idea. Put the bean in the view scope instead.

Understanding navigation in JSF

Trying to build a simple JSF application. One i don't get is, what mechanism is to use to trigger page reload. I have tried following
<h:selectOneMenu
id="ChallengesListBox"
onchange="submit()"
valueChangeListener="#{bean.projectselected}">
<f:selectItems value="#{bean.projectnames}"/>
</h:selectOneMenu>
<h:commandButton value="submit" action="#{bean.submit}" />
with
public String submit() {
this.description.setRendered(true);
return null;
}
But this has no effect. The bean method
public String submit()
remains untouched, as I can see in debug mode.
This is not related to navigation. There are many causes why a bean action method is not invoked. You can find them all in this answer: commandButton/commandLink/ajax action/listener method not invoked or input value not updated
I suspest that you've either simply no <h:form>, or that you have multiple buttons in the form (the first one would then be invoked on submit()), or that the button is by itself not rendered during processing of the form submit.
Unrelated to the concrete problem, this JSF 1.x style approach is very clumsy. As you're apparently already on JSF 2.x, I suggest the following much simpler approach:
<h:selectOneMenu value="#{bean.projectname}">
<f:selectItems value="#{bean.projectnames}" />
<f:ajax render="somepanel" />
</h:selectOneMenu>
<h:panelGroup id="somepanel">
<h:outputText value="#{bean.description}" rendered="#{not empty bean.projectname}" />
</h:panelGroup>
(this will render the #{bean.description} value whenever the #{bean.projectname} is not null/empty according to the selection of the drop down list)
And make sure that you remove all binding attributes to the backing bean. Also make sure that the bean is #ViewScoped whenever you have input/button components inside the <h:panelGroup id="somepanel">.

RowselectionListener

I have a query about PrimeFaces. Is it possible to implement a RowSelectionListener component that is similar to
<h:commandLink value ="selection"
action="#{usuariosGruposBean.selectionOfGroupObject}">
<f:setPropertyActionListener target="#{usuariosGruposBean.grps}"
value="#{groups}"/> </h:commandLink>
within a datatable?
You mean for a datatable right ?
if so yes it's possible to do that, you just need to create a method like this
public void onEditRow(RowEditEvent event) {
enter code here
}
and register it in the JSF using something like this inside the DataTable tag
<p:ajax event="rowEdit" update="#this" listener="#{userController.onEditRow}" />
here it will update the whole Datatable because of the #this, if you want to update just a few column you could change that to the name of those columns separated by a space
which would look something like this
<p:dataTable var="user" value="#{userController.allUsers}" id="userTable">
<p:ajax event="rowEdit" update="#this" listener="#{userController.onEditRow}" />
things inside the table
</p:datatable>
I don't know exactly what you're trying to do, but have you looked at the primefaces showcase yet? They have a lot of examples how to build a row selection listener.

Dynamically passing bean to rich:popupPanel

I have a base class, which is normal Java class. Three subclasses extend it which are #ViewScoped beans. There's a facelet using a dynamic variable. I have three xhtml pages which use this facelets with the three beans viz. bean1, bean2 and bean3 which are dynamically included in a rich:tab component on a main page.
So far so good. But on every page there are a few popups which should refer to the current bean. And since those popups need form tag inside them, I have included them outside the main page's form tags in order to avoid nested form tags. Now I want the popup to refer to current bean (bean1, bean2 or bean3, depending upon from where the popup is called) in question. How do I achieve this?
Try something like this:
Add a new bean (popupBean) that will contain reference to current bean in popup.
Add an action that will set needed bean as property of popupBean. Example:
<h:commandLink>
<a4j:ajax render="myPopup"/>
<f:setPropertyActionListener target="#{popupBean.currentBean}" value="#{bean1}"/>
<h:outputText value="Click ME!"/>
</h:commandLink>
Use show attribute of rich:popupPanel to show the popup:
<rich:popupPanel id="myPopup" modal="true" width="600" moveable="true"
show="#{not empty popupBean.currentBean}" onmaskclick="#{rich:component('myPopup')}.hide();" autosized="true">
<f:facet name="header"><h:outputText value="Cool popup!"/></f:facet>
<h:panelGrid style="margin: 0">
<h:panelGroup style="max-height: 400px;overflow-y: auto;" layout="block">
<h:outputText value="#{popupBean.currentBean.myBeanProperty}"/>
</h:panelGroup>
<h:button value="Close" onclick="#{rich:component('myPopup')}.hide();return false;"/>
</h:panelGrid>
</rich:popupPanel>

Resources