JSF 2 h:dataTable with inputText values binded to List. Values not updating [duplicate] - jsf

This question already has an answer here:
Using <ui:repeat><h:inputText> on a List<String> doesn't update model values
(1 answer)
Closed 7 years ago.
I'm trying to display the data from a List contained in a bean via a datatable, and have actions performed by code as the user types. It initially displays ok, and changes to the List are reflected in UI.
My issue is that values entered into the inputText are ignored. Tried looking for solutions, I've tried checking the List as the values changed, and also tried to check without the ajax in case that was the issue (no change). Have tried using session and view scoped bean without luck. Tried wrapping the Strings in a POJO.
Going crazy here. Feel like I'm missing something obvious. Any idea?
<h:dataTable id="multioptionanswers"
value="#{multiTextBean.texts}" var="texts">
<h:column>
<f:facet name="header">
<h:outputText value="Enter Values"></h:outputText>
</f:facet>
<h:inputText value="#{texts}" immediate="true">
<f:ajax event="blur" execute=":addgroup"
render=":addgroup"
listener="#{multiTextBean.update}" />
</h:inputText>
</h:column>
</h:dataTable>
**addgroup is a panel group that the data table resides in.

Worked it out, needed change to how the inputtext read the data. Solution below in case it helps someone :
<h:dataTable id="multioptionanswers" binding="#{table}"
value="#{multiTextBean.texts}" var="texts">
<h:column>
<f:facet name="header">
<h:outputText value="Enter Values"></h:outputText>
</f:facet>
<h:inputText
value="#{multiTextBean.texts[table.rowIndex]}">
<f:ajax event="blur" execute=":addgroup" render=":addgroup"
listener="#{multiTextBean.update}" />
</h:inputText>
</h:column>
</h:dataTable>

Related

How do I display a list of items inside a list of items on a JSF page? [duplicate]

<h:dataTable value="#{SearchingBeans.list}" var="entry">
<h:column>
<f:facet name="header">
<h:outputLabel>Photo</h:outputLabel>
</f:facet>
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel>Pseudo</h:outputLabel>
</f:facet>
<h:outputLabel value="#{entry.pseudo}"></h:outputLabel>
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel>Description</h:outputLabel>
</f:facet>
<h:outputLabel value="#{entry.description}"></h:outputLabel>
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel>Photo</h:outputLabel>
</f:facet>
<h:outputLabel value="#{entry.photo[0].path}"></h:outputLabel> <-- this a List
</h:column>
</h:dataTable>
i got a entities member one of his property is a List photo with get/set
that property is populate
i don't know how to fetch that value in jsf i want only the first picture for each member since their have 2-3 photos. Its that possible?? any other solution will be appreciate.
Just iterate over it using <ui:repeat> or <h:dataTable> the usual way. It's perfectly valid to nest multiple iterating components in each other. In case of <h:dataTable>, you only need to make sure that you put the nested iterating component inside a <h:column>.
E.g.
<h:dataTable value="#{bean.entities}" var="entity">
<h:column>
#{entity.property}
</h:column>
<h:column>
<ui:repeat value="#{entity.subentities}" var="subentity">
#{subentity.property}
</ui:repeat>
</h:column>
</h:dataTable>
or
<h:dataTable value="#{bean.entities}" var="entity">
<h:column>
#{entity.property}
</h:column>
<h:column>
<h:dataTable value="#{entity.subentities}" var="subentity">
<h:column>
#{subentity.property}
</h:column>
</h:dataTable>
</h:column>
</h:dataTable>
You'll potentially only run into issues when you nest multiple <ui:repeat> components and use <f:ajax> in it while using an older version of Mojarra.
Only JSTL <c:forEach> wouldn't work when nested inside a JSF iterating component for the reasons explained here JSTL in JSF2 Facelets... makes sense?
Unrelated to the concrete problem, please don't abuse <h:outputLabel> for pure text presentation. It generates a HTML <label> element which is intented to label an input element by for attribute. However, you're doing that nowhere in the code. You should be using <h:outputText> instead. By the way, I'm lately seeing this more often in code of starters. There must be somewhere a bad tutorial or resource which is abusing the <h:outputLabel> this way instead of <h:outputText> or even plain EL in template text. Which tutorial/resource was you using? Then I can contact the author about this severe misinstruction. See also Purpose of the h:outputLabel and its "for" attribute

h:dataTable renders rows correctly but does not show cell values while ui:repeat does

I have the following data table.
<h:dataTable value="#{flightReservation.flightList}" var="flight">
<h:column>
<h:outputText value="#{flight.fromPlace}" />
</h:column>
<h:column>
<h:outputText value="#{flight.toPlace}" />
</h:column>
</h:dataTable>
It does not show any data inside the cells, even though the number of rows is equal to the flightList size.
When I replace it by <ui:repeat>, then it shows the data.
<ui:repeat value="#{flightReservation.flightList}" var="value">
<h:outputText value="#{value.fromPlace}" />
<h:outputText value="#{value.toPlace}" />
</ui:repeat>
How is this caused and how can I solve it?
I encountered the same behaviour once and it was simply an issue with the scope of variables:
You are saying, that the number of rows is correct, but the content not displayed. You are naming the variable flight and I assume that a value with this name has been used earlier in your markup or even used as a <ui:param>.
The Datatable is now iterating (internal) and generating the correct amount of rows, but when it tries to access the properties of fligt jsf refers to the earlier declared variable and fails to find the properties fromPlace, toPLace etc.
Try to rename your variable and see if this solves your issue.
The example bellow would produce stringList.size() times yoloyolo even if one would expect the content of the list to be shown. And if the listsource is made out of objects, any instance access on the attributes will fail cause yoloylo is not an object of the expected type.
<ui:param name="foo" value="yoloyolo" />
<h:dataTable value="#{backingBean.stringList}" var="foo">
<h:column>
<f:facet name="header">string</f:facet>
- #{foo}
</h:column>
</h:dataTable>

Dynamically change input type

I am using JSF 2.2. I am trying to build an exam system. Some questions have 1 right answer, others have multiple right answers. According to this, my view should show radio buttons or checkboxes. I have one hard coded example in my XHTML page:
<h:dataTable value="#{main.answerList}" var="list">
<h:column>
<h:selectBooleanCheckbox id="checkboxAnswer" value="#{list.check}"/>
<h:outputText value="#{list.ansValue}"/>
</h:column>
</h:dataTable>
How can I change between radio buttons and checkboxes, depending on the question type?
Just use the rendered attribute to toggle between the input types depending on the question type. The code (the variable names) in your question is not self-documenting, so I'm making changes on that as well, so that it becomes more self-documenting and it's immediately obvious what each of those properties represent.
<h:dataTable value="#{bean.questions}" var="question">
<h:column>
<h:selectOneRadio value="#{question.selectedAnswer}" rendered="#{question.type == 'SINGLE_CHOICE'}">
<f:selectItems value="#{question.availableAnswers}" />
</h:selectOneRadio>
<h:selectManyCheckbox value="#{question.selectedAnswers}" rendered="#{question.type == 'MULTIPLE_CHOICE'}">
<f:selectItems value="#{question.availableAnswers}" />
</h:selectManyCheckbox>
</h:column>
</h:dataTable>
The #{question.type} should represent an enum with the mentioned values.

Command Button action not working [duplicate]

This question already has answers here:
commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated
(12 answers)
Closed 3 years ago.
I have this dataTable:
<h:dataTable value="#{orderController.orderList}" var="order"
styleClass="table table-striped">
<h:column>
<f:facet name="header">#</f:facet>
<h:outputText value="#{order.orderNo}" escape="false"/>
</h:column>
<h:column>
<f:facet name="header">Cliente</f:facet>
<h:outputText value="#{order.client}" escape="false"/>
</h:column>
<h:column>
<f:facet name="header">Data</f:facet>
<h:outputText value="#{order.date}" escape="false"/>
</h:column>
<h:column>
<f:facet name="header">Status</f:facet>
<h:outputText value="#{order.status}" escape="false"/>
</h:column>
<h:column>
<f:facet name="header"></f:facet>
<h:form>
<h:commandButton action="#{orderController.orderDetail}" value="Detalhe" styleClass="btn btn-info"/>
</h:form>
</h:column>
</h:dataTable>
I'm showing a list of orders and each list has a button "Detail" that will redirect the user to the orderDetail.html page inside views/fornecedores.
I've debugged and when I click on the commandButton, it is not calling my bean function.[
I have also tried this: <h:commandButton action="views/fornecedores/orderDetail.html" value="Detalhe" styleClass="btn btn-info"/> and nothing, it keeps redirecting me to the same page, orderSearch.html.
What I need is to call the method that will receive a orderNo parameter and will load the object and redirect to the orderDetail.html page.
What am I doing wrong or what is the best way to this approach?
In this particular case, that can happen when you're nesting forms which is illegal in HTML. This can also happen when the #{orderController} is request scoped and the orderList content depends on a request based value which causes that the list is incompatibly changed when the form submit is to be processed.
So, make sure that you aren't nesting multiple <h:form> components in each other and make sure that the #{orderController} is a #ViewScoped bean and that you aren't manipulating the orderList in its getter method.
See also:
commandButton/commandLink/ajax action/listener method not invoked or input value not updated
As to your concrete functional requirement, the <h:link> with <f:param> and <f:viewParam> is a more sane approach for this. It's bookmarkable and more SEO friendly. See also Communication in JSF 2.0 - Processing GET request parameters.

Nested datatable in JSF 2.0

I am trying to fetch the questions and the associated answers. I am able to get the questions fine but the nested datatable does not even recognize the properties each answer has, and this is also pointed out by the IDE as well. Maybe this is not the right way to go about this. How do I iterate over the answers associated with each question ?
<h:dataTable value="#{questionBacking.recentlyAskedQuestions}" var="questions"
rendered="#{questionBacking.recentlyAskedQuestions.size() > 0}"
border="1">
<h:column>
<h:outputText value="#{questions.questionTitle}" />
<br/>
<h:outputText value="#{questions.questionBody}" />
</h:column>
<h:dataTable value="#{questions.answers}" var="answers">
<tr>
<h:outputText value="#{answers.answer}" />
</tr>
</h:dataTable>
</h:dataTable>
You need to put columns inside a <h:column>.
<h:dataTable value="#{questionBacking.recentlyAskedQuestions}" var="question" rendered="#{not empty questionBacking.recentlyAskedQuestions}">
<h:column>
<h:outputText value="#{question.questionTitle}" />
<br/>
<h:outputText value="#{question.questionBody}" />
</h:column>
<h:column>
<h:dataTable value="#{question.answers}" var="answer">
<h:column>
<h:outputText value="#{answer.answer}" />
</h:column>
</h:dataTable>
</h:column>
</h:dataTable>
(note that I changed the rendered and the var attributes to be a bit more self-documenting, you might want to rename questionTitle, questionBody and answer to title, body and body respectively as well so that you don't keep duplicating the meaning)
You can do the same thing mentioned in BalusC's answer using PrimeFaces as well. Just replace <h:dataTable> with <p:dataTable> and the same goes for column tags as well. You can create nested datatable with PrimeFaces. I recommend people to use PrimeFaces as it reduces the amount of time people spend on writing/modifying css definitions.

Resources