Table disappears after cell edit in primefaces (also data not saved) - ajax

I have an input field and a table on page. Whenever a number in a field changes (ajax listener to change event) - I want number of rows to change accordingly.
I pointed the table to an Arraylist in a Backing bean, and play with its size.
But then when I edit a value in the table - and press enter, the table disappears, and reappears only when I change field value again.
As a bonus, in debug mode, I see that backing arraylist always have empty values.
Here goes the Bean code:
#ManagedBean(name = "bean")
#ViewScoped
public class TestBean {
private List<String> hostnames = new ArrayList<String>();
private int copiesQuantityJustCopy;
public int getCopiesQuantityJustCopy() {
return copiesQuantityJustCopy;
}
public void setCopiesQuantityJustCopy(int copiesQuantityJustCopy) {
this.copiesQuantityJustCopy = copiesQuantityJustCopy;
}
public List<String> getHostnames() {
return hostnames;
}
public void setHostnames(List<String> hostnames) {
this.hostnames = hostnames;
}
public void onUpdateCount() {
int hostnamesCount = hostnames.size();
System.out.println("delta = " + hostnamesCount + " - " + copiesQuantityJustCopy);
int delta = hostnamesCount - copiesQuantityJustCopy;
if (delta > 0)
for (int i = 1; i < delta; i++)
hostnames.remove(delta);
if (delta < 0)
for (int i = 0; i < -delta; i++)
hostnames.add("");
}
}
and the view code:
<h:form id="form1">
<p:inputMask mask="9?99" maxlength="3" placeHolder=" " value="#{bean.copiesQuantityJustCopy}">
<p:ajax event="change" listener="#{bean.onUpdateCount()}" update=":form1:hostnamesTable" />
</p:inputMask>
<p:outputPanel id="hostnamesTable" rendered="true">
<p:dataTable value="#{bean.hostnames}" var="hostname"
id="hostnames" editable="true" editMode="cell">
<p:ajax event="cellEdit" update=":form1:hostnamesTable" process="#this" />
<p:column>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{hostname}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{hostname}" style="width:96%" />
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
</p:outputPanel>
</h:form>

I think my answer won't help #Anton, but it may be helpful to another "stackers" facing same problem.
My problem is similar (dataTable dissapears when cellEdit event is completed). In my case the cause was the table update performed by ajax. Since I realy don't need table update after listener firied I simply removed update attribute and now everything works good to me.

Related

Change value of checkbox using ajax in JSF

I try to solve the following problem: i have a group of h:selectBooleanCheckbox that generates using ui:repeat:
<h:form>
<ui:repeat value="#{uiCheckboxList.list}" var="boxElement" varStatus="i">
<h:selectBooleanCheckbox id="a">
<f:attribute name="value" value="#{boxElement.enabled}"/>
</h:selectBooleanCheckbox>
<h:outputLabel value="#{boxElement.val}" for="a"/>
</ui:repeat>
</h:form>
Values and label's text stored in array list and controls by servlet:
#ManagedBean(name="uiCheckboxList")
#ApplicationScoped
public class CheckBoxListBean implements Serializable {
private ArrayList<BoxTuple> list;
// BoxTyple is class with 2 fields - val (double) and enabled (boolean)
public CheckBoxListBean() {
list = new ArrayList<>();
for(double i = -2.0; i <= 2.0; i += 0.5) {
list.add(new BoxTuple(i, false));
}
// Set first element to true
list.get(0).setEnabled(true);
}
public ArrayList<BoxTuple> getList() {
return list;
}
public void setList(ArrayList<BoxTuple> list) {
this.list = list;
}
And my problem is this: when you click on one of the h:selectBooleanCheckbox server asynchronously sent request with information about the changed box, and then change the values of the another boxes without reloading page. In other words, I need to get the behavior of radio boxes. Maybe it sounds silly, but I need to solve this problem. How can i implement this?
You can do something like this:
<h:form id="form">
<ui:repeat value="#{uiCheckboxList.list}" var="boxElement"
varStatus="i">
<h:selectBooleanCheckbox id="a">
<f:attribute name="value" value="#{boxElement.enabled}" />
<f:ajax render="form"
listener="#{uiCheckboxList.listen(boxElement)}" />
</h:selectBooleanCheckbox>
<h:outputLabel value="#{boxElement.val}" for="a" />
</ui:repeat>
</h:form>
(get rid of the varStatus if you don't use it)
public void listen(BoxTuple tuple) {
if (tuple.enabled)
for (BoxTuple t : list)
if (!t.equals(tuple))
t.setEnabled(false);
}

Validate if new value of <h:inputText> is not higher than old value

I want to control the value in an <h:inputText> ..I want that once I leave the input fieldsif I have a higher value than the old value an error message is displayed and I can not validate fields after entering a value lower (the older value is loaded from the database)
I have done somthing like this :
<p:column headerText="Qte" >
<h:inputText value="#{ligne.qte}" styleClass="span12" required="true" valueChangeListener="#{myBean.changeQte}" >
<h:message id="message"></h:message>
<p:ajax event="keyup" listener="#{myBean.onQteChange}" update="message" />
</h:inputText>
</p:column>
in myBean I have this two methods :
public void onQteChange(){
String message =null;
int newqte = getQte();
if (newqte > previousQTE ){
message="invalid qte";
}
}
public void onQteChange(){
String message =null;
int newqte = getQte();
if (newqte > previousQTE ){
message="invalid qte";
}
}
Can someone tells me how can I do this because even if I try to make ajax call into my inputtext I have no result ?
You can use :
public void onQteChange(){
String message =null;
int newqte = getQte();
if (newqte > previousQTE ){
message="invalid qte";
FacesContext.getCurrentInstance().addMessage(
"message",new FacesMessage(FacesMessage.SEVERITY_ERROR, message,message));
}
}
<p:column headerText="Qte" >
<h:inputText value="#{ligne.qte}" id="x" styleClass="span12" required="true" valueChangeListener="#{myBean.changeQte}" >
<p:ajax event="keyup" listener="#{myBean.onQteChange}" update="message" />
</h:inputText>
<h:message id="message" for="x" />
</p:column>

Problems with ajax in jsf updating an output text field

I'm new to JSF and in class we should just set up a little "Pizza-Service" with logins etc. On the Page where i want to prepare my Order as a user I've a data table (primeface data table) with 4 columns. The last one is for the number of pizza of the type in the current cell.
What I want: If I am typing in a Number i want to set up a current Order (works fine so far with ajax) in the same moment i just want to show the user the current price. This is the little thing that is not working.
When I am using the tag there is just nothing happening and if I am taking the tag I'm getting this mistake:
<f:ajax> contains an unknown id 'ausgabe' - cannot locate it in the context of the component menge
What I thought was take the ajax tag set up a function (take the current order) and render an output text where the calculated price is shown directly. I called those function in german so bestellungAufnehmen = takeOrder and preisBerechnen = calculatePrice.
Thanks if someone could help me!
Here is the xhtml:
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<p:dataTable id="table" var="pizza" value="#{model.getPizzen()}">
<p:column headerText="Nr">
<h:outputText value="#{pizza.nr}"/>
</p:column>
<p:column headerText="Artikel">
<h:outputText value="#{pizza.name}"/>
</p:column>
<p:column headerText="Preis">
<h:outputText value="#{pizza.preis}"/>
</p:column>
<p:column headerText ="Menge">
<h:inputText id="menge" value="#{bestellung.anzahl}">
<f:ajax event="keyup" render="ausgabe" listener="#{bestellung.bestellungAufnehmen(pizza)}"/>
</h:inputText>
</p:column>
</p:dataTable>
<h:outputLabel value="Zu Zahlen: "/>
<h:outputText id="ausgabe" value="#{bestellung.preisBerechnen()} €"/>
</h:form>
</h:body>
and here is the the Bean with those functions.
#Named
#SessionScoped
public class Bestellung implements Serializable {
private static int bstzaehler=0;
private int bstnr;
private Map<String,Bestellposten> posten;
private int anzahl;
public Bestellung(){
this.bstnr=++bstzaehler;
this.posten = new HashMap<>();
}
/**
* #return the bstnr
*/
public int getBstnr() {
return bstnr;
}
/**
* #param bstnr the bstnr to set
*/
public void setBstnr(int bstnr) {
this.bstnr = bstnr;
}
public void bestellungAufnehmen(Pizza pizza){
if(this.posten.containsKey(pizza.getName()) == false){
Bestellposten b = new Bestellposten(this.getAnzahl(), pizza);
this.posten.put(pizza.getName(),b);
} else {
if(this.getAnzahl() > 0 ){
Bestellposten tmp = this.posten.get(pizza.getName());
tmp.setMenge(this.getAnzahl());
} else {
this.posten.remove(pizza.getName());
}
}
System.out.println("groesse: "+posten.size());
System.out.println("akt preis: "+this.preisBerechnen());
}
/**
* #return the anzahl
*/
public int getAnzahl() {
return anzahl;
}
/**
* #param anzahl the anzahl to set
*/
public void setAnzahl(int anzahl) {
this.anzahl = anzahl;
}
public double preisBerechnen(){
double sum = 0.0;
for(String key : this.posten.keySet()){
Pizza tmp = this.posten.get(key).getPizza();
sum += tmp.getPreis() * this.posten.get(key).getMenge();
}
return sum;
}
}
Try giving the full qualified id for the component:
<h:form id="frmPizza">
<p:dataTable id="table" var="pizza" value="#{model.getPizzen()}">
<p:column headerText="Nr">
<h:outputText value="#{pizza.nr}"/>
</p:column>
<p:column headerText="Artikel">
<h:outputText value="#{pizza.name}"/>
</p:column>
<p:column headerText="Preis">
<h:outputText value="#{pizza.preis}"/>
</p:column>
<p:column headerText ="Menge">
<h:inputText id="menge" value="#{bestellung.anzahl}">
<f:ajax event="keyup" render=":frmPizza:ausgabe" listener="#{bestellung.bestellungAufnehmen(pizza)}"/>
</h:inputText>
</p:column>
</p:dataTable>
<h:outputLabel value="Zu Zahlen: "/>
<h:outputText id="ausgabe" value="#{bestellung.preisBerechnen()} €"/>
</h:form>
I think, you should use update attribute to update that outputText, like update=":FormId:ausgabe"
<f:ajax event="keyup"
render=":frmPizza:ausgabe"
listener="#{bestellung.bestellungAufnehmen(pizza)}"
update=":frmPizza:ausgabe" />
The solution of luiggi worked but i couldnt answer directly because of my "low-level" here.
Iam not on my computer right now but isnt "update" an attribute of p:ajax and not of f:ajax and is the same as render? but i will try it aswell later on.
thx!

ajax call makes field empty

updated the code but not real change.
When I set the player I want to update the other select one menu with the current players club. But the menu will become completely empty. The bean will always return the correct value.
Navigating from the page and back will show the correct value. It will then always work as intended after the first time you navigate back.
<p:selectOneMenu value="#{player}"
converter="playerConverter" id="playerList">
<f:selectItem itemLabel="---" noSelectionOption="true" />
<f:selectItems value="#{servicePlayer.allPlayers}"
var="n"
itemValue="#{n}"
itemLabel="#{n.combinedName}"
itemLabelEscaped="true"/>
<p:ajax event="change" execute="#this" update="ClubMenu" actionListener="#{serviceHCP.getClubs(player)}"/>
<!-- p:ajax event="change" execute="#this" render="ClubMenu" /-->
</p:selectOneMenu>
<h:outputText value="Klubb"></h:outputText>
<p:selectOneMenu id="ClubMenu" value="#{serviceHCP.myClubList}" rendered="#{not empty serviceHCP.myClubList}">
<f:selectItems value="#{serviceHCP.getClubs(player)}" />
</p:selectOneMenu>
<h:outputText value="Serietyp"></h:outputText>
Backing bean function
public void getClubs(Player player) {
if (factory == null) {
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
}
EntityManager em = factory.createEntityManager();
//If we have a player just return that players club
Query q;
if (player == null || player.getClub() == null)
q = em.createQuery("select t from Club t");
else {
q = em.createQuery("select t from Club t where t.id = :playerID");
q.setParameter("playerID", player.getClub().getId());
}
myClubList = q.getResultList();
for (Club aClub : myClubList) {
System.out.println("Club list info: " + aClub);
}
System.out.println("Club Size: " + myClubList.size());
em.close();
}
You need to also process the playerList field:
<p:ajax process="#this" update="ClubMenu" />

Issue faced with Trinidad Table, Partial Trigger, and Read Only

I have a trinidad table with one column contains drop downs (select one choice) for each row and another column contains an input text for each row.
When a particular option is selected, the input becomes editable (else it remains read only).
The read only behaviour and the partial event are working perfectly as expected. The problem is whenever the input text is supposed to move from editable to read only, I still see the value previously entered when the input text was editable.
From here if I select another option (for which the input text would also be read only) the value goes away.
The JSP Code is as follows:
<tr:table emptyText="No Data" var="vo" autoSubmit="true" partialTriggers="accountType" value="#{testingBean.voList}" binding="#{testingBean.table}" width="50%">
<tr:column>
<f:facet name="header">
<tr:outputText value="Sl No" />
</f:facet>
<tr:outputText value="#{vo.slNo}" />
</tr:column>
<tr:column>
<f:facet name="header">
<tr:outputText value="Account Type" />
</f:facet>
<tr:selectOneChoice unselectedLabel="Select" id="accountType" value="#{vo.accountType}"
autoSubmit="true" immediate="true" valuePassThru="true"
valueChangeListener="#{testingAction.optionsChanged}">
<f:selectItems value="#{testingBean.options}" />
</tr:selectOneChoice>
</tr:column>
<tr:column>
<f:facet name="header">
<tr:outputText value="Other" />
</f:facet>
<tr:inputText value="#{vo.otherType}"
partialTriggers="accountType"
readOnly="#{vo.readOnlyFlag}"></tr:inputText>
</tr:column>
</tr:table>
The code for the valueChangeListener is as follows:
public void optionsChanged(ValueChangeEvent vce) {
try {
System.out.println("Inside Method");
testingBean = (TestingBean) FacesContext.getCurrentInstance()
.getExternalContext().getSessionMap().get("testingBean");
ArrayList<TableRowVO> list = testingBean.getVoList();
UIXTable table = testingBean.getTable();
TableRowVO vo = list.get(table.getRowIndex());
vo.setReadOnlyFlag(true);
vo.setAccountType(vce.getNewValue().toString());
vo.setOtherType("");
if (vce.getNewValue().equals("3")) {
System.out.println("Setting false");
vo.setReadOnlyFlag(false);
}
list.set(table.getRowIndex(), vo);
table.setValue(list);
testingBean.setVoList(list);
FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().put("testingBean", testingBean);
} catch (Exception e) {
e.printStackTrace();
}
}
Is this behaviour expected? Or is there a workaround?
Interestingly, the "View Page Source" option in Firefox does not contain the text, even though I can see it on the page. I had to use FireBug to analyze the element which revealed the following:
<div class="af_inputText_content"
id="j_id_jsp_545172797_17:0:j_id_jsp_545172797_27">
tttt
</div>
Version used:
Trinidad 2.x
JSF 2.x
Tomcat 7.0
Ok, so I got it to work a while back but forgot to post the answer....
I used FacesContext to find the CoreComponent for the input text and then reset the components value.
Basically:
Find the CoreTable object
Find the CoreColumn object
Find the CoreInputText object
Reset the value for CoreInputText
The Modified JSP Code is as follows:
<tr:table emptyText="No Data" var="vo" autoSubmit="true" partialTriggers="accountType" id="voTable" value="#{testingBean.voList}" binding="#{testingBean.table}" width="50%">
<tr:column id="slNoCol">
<f:facet name="header">
<tr:outputText id="slNoHdr" value="Sl No" />
</f:facet>
<tr:outputText id="slNo" value="#{vo.slNo}" />
</tr:column>
<tr:column id="accountTypeCol">
<f:facet name="header">
<tr:outputText id="accountTypeHdr" value="Account Type" />
</f:facet>
<tr:selectOneChoice unselectedLabel="Select" id="accountType" value="#{vo.accountType}" autoSubmit="true" immediate="true" valuePassThru="true" valueChangeListener="#{testingAction.optionsChanged}">
<f:selectItems value="#{testingBean.options}" />
</tr:selectOneChoice>
</tr:column>
<tr:column id="otherTypeCol">
<f:facet name="header">
<tr:outputText id="otherTypeHdr" value="Other" />
</f:facet>
<tr:inputText value="#{vo.otherType}" columns="15" partialTriggers="accountType" id="otherType" readOnly="#{vo.readOnlyFlag}" immediate="true">
</tr:inputText>
</tr:column>
</tr:table>
The code for the modified valueChangeListener is as follows:
public void optionsChanged(ValueChangeEvent vce) {
try {
testingBean = (TestingBean) FacesContext.getCurrentInstance()
.getExternalContext().getSessionMap().get("testingBean");
ArrayList<TableRowVO> list = testingBean.getVoList();
CoreTable table = (CoreTable) FacesContext.getCurrentInstance()
.getViewRoot().findComponent("voTable");
CoreColumn column = (CoreColumn) table
.findComponent("otherTypeCol");
CoreInputText text = (CoreInputText) column
.findComponent("otherType");
text.setValue("");//This is the trick....
TableRowVO vo = list.get(table.getRowIndex());
vo.setReadOnlyFlag(true);
vo.setAccountType(vce.getNewValue().toString());
vo.setOtherType("");
if (vce.getNewValue().equals("3")) {
vo.setReadOnlyFlag(false);
}
list.set(table.getRowIndex(), vo);
table.setValue(list);
testingBean.setVoList(list);
FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().put("testingBean", testingBean);
} catch (Exception e) {
e.printStackTrace();
}
}

Resources