0

I have this primefaces index page:

<p:layoutUnit position...>
<h:form>
<p:outputLabel for="txt" value="enter 6 digits">
<p:inputMask id="txt" value="#{bean.txt}" mask="999-999">
<p:inputLabel for="date1" value="choose date:">
<p:calendar id="date1" value="#{bean.date1}">

<p:commandButton value="submit" actionListener="#{bean.submit}"/>
</h:form>
</p:layoutUnit>
<p:layoutUnit position ...>
<h:form>
<p:dataTable var="data" value="#{bean.result}">
<p:column headerText="result">
#{data}
</p:column>
</p:dataTable>
</h:form>
</p:layoutUnit>

//class Bean.java //the bean is sessionscopped

private String txt;//setter and getter
private Date date1;
private List<String> result;
public List<String> getList()
{return result;}
public void submit()
{
// here the code I get str and add to list using loop
//it's a large code to paste here
result.add(str);
}

when click submit button it display the result but when choose another date to start again search the old data stay displayed in datatable and no new data displayed that the problem! If i refresh the page the new result displayed.

How to solve that?

Kukeltje
  • 11,924
  • 4
  • 19
  • 44
Wasfy
  • 73
  • 8

1 Answers1

0

Since the new data is displayed after you refresh the page, then all you need is to update the <p:dataTable> using primefaces's built-in ajax after you submit the first form.
To do that just add an id to the form containing the dataTable (Thanks @Kukeltje for the clarification) :
<h:form id="foo">
and add an id to the dataTable tag :
<p:dataTable id="beanTable" var="data" value="#{bean.result}">
and then add this to the commandButton, to select the component you are updating :
<p:commandButton update=":foo:beanTable" value="submit" actionListener="#{bean.submit}"/>

RedaN
  • 93
  • 10
  • Although the objective of the answer is ok, there is an error in your code. It's related to the id and the update – Kukeltje Jul 25 '17 at 12:15
  • @Kukeltje is it about the ":" or is it that the update should be more specific ? because AFAIK it can work i there are no other IDs in the same page – RedaN Jul 25 '17 at 13:44
  • The datatable is in another form and forms are namingcontainers. And the second form has no fixed id... – Kukeltje Jul 25 '17 at 13:55
  • Adding id and update give me the error: cannot find component for expression "beanTable" refrenced from. "j_idt12:j_idt17" – Wasfy Jul 25 '17 at 14:01
  • Hi @Kukeltje I already read the link you give in your comment and add update="@all" to the commandButton and now is ok, but this will be good or no? – Wasfy Jul 25 '17 at 14:12
  • @Wasfy No, `@All` should be rarely be used (only as a last resort and if you actually fully are sure you need this and not a full page refresh). And the error you get is because what I stated in a previous comment. The answer in itself is not wrong (although this Q is, after you became aware of 'ajax', a duplicate of https://stackoverflow.com/questions/8634156/how-to-find-out-client-id-of-component-for-ajax-update-render-cannot-find-compo. Post the error in your other comment in google and you'll find answers – Kukeltje Jul 25 '17 at 14:25
  • Suggestion is to just read all JSF related stackoverflow Q/A with more than 20 upvotes and remember them... And also look at http://jsf.zeef.com – Kukeltje Jul 25 '17 at 14:28
  • @Kukeltje Thanks for the clarification, I've updated my answer accordingly. – RedaN Jul 25 '17 at 14:49
  • @RedaN the error still even i update what in your answer. – Wasfy Jul 25 '17 at 18:17
  • 2
    @RedaN only you need to add the colon dots : before to become update=":foo:beanTable" this solve my issue thanks at all for your time. – Wasfy Jul 25 '17 at 18:53