0

I have PrimeFaces dataTable that is been filled by a Ajax call.

When I click on a column title, to order its values, the values disappear.

<p:commandButton value="Pesquisar" actionListener="#{requestController.listRequests}" update="dataTable" />

Here is my view:

<p:dataTable id="dataTable" var="order" value="#{requestController.backing.requestsList}"
                         paginator="true" rows="10"
                         paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink}">
                <p:column sortBy="#{order.companyRequest}">
                    <f:facet name="header">
                        <h:outputText value="Pedido" />
                    </f:facet>
                    <h:outputText value="#{order.companyRequest}" />
                </p:column>

                <p:column sortBy="#{order.company.companyName}">
                    <f:facet name="header">
                        <h:outputText value="Cliente" />
                    </f:facet>
                    <h:outputText value="#{order.company.companyName}" />
                </p:column>

                <p:column sortBy="#{order.emissionDate}">
                    <f:facet name="header">
                        <h:outputText value="Data" />
                    </f:facet>
                    <h:outputText value="#{order.emissionDate}">
                        <f:convertDateTime pattern="dd/MM/yyyy"/>
                    </h:outputText>
                </p:column>
                <p:column sortBy="#{order.requestSituation.description}">
                    <f:facet name="header">
                        <h:outputText value="Status" />
                    </f:facet>
                    <h:outputText value="#{order.requestSituation.description}" />
                </p:column>
                <p:column>
                    <f:facet name="header">
                    </f:facet>
                    <h:form>
                        <h:commandLink value="Detalhes"/>
                    </h:form>
                </p:column>
            </p:dataTable>

EDIT

RequestController

@ManagedBean
@RequestScoped
public class RequestController implements Serializable
{

    private RequestBacking backing;

    public RequestController()
    {
        backing = new RequestBacking();
    }

    public void changeEventListener(ValueChangeEvent e)
    {
        backing.requestSearchType = e.getNewValue().toString();
    }

    public void change()
    {
        switch (backing.requestSearchType)
        {
            case "data":
                backing.mask = "99/99/9999";
                backing.maskSize = "10";
                break;
            case "cnpj":
                backing.mask = " 99.999.999/9999-99";
                backing.maskSize = "20";
                break;
            default:
                backing.mask = "";
                backing.maskSize = "50";
        }
    }

    public void listRequests() throws ParseException
    {

        CompanyVO companyVO = new CompanyVO();
        switch (backing.requestSearchType)
        {
            case "cnpj":
                companyVO.setCnpj(backing.requestSearchValue);
                break;
            case "cliente":
                companyVO.setCompanyName(backing.requestSearchValue);
                break;
            case "pedido":
                backing.requestVO.setCompanyRequest(Integer.parseInt(backing.requestSearchType));
                break;
        }
        SupplierVO supplierVO = new Support().getUserSession().getSupplier();
        backing.requestVO.setEmissionDate(new Support().convertDate(backing.requestSearchValue));
        backing.requestVO.setSupplier(supplierVO);
        backing.requestVO.setCompany(companyVO);

        backing.requestsList = new ArrayList<>(backing.getBo().getRequest(backing.requestVO));

        if (backing.requestsList.isEmpty())
        {
            FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_WARN, "Nenhum registro encontrado!", null);
            FacesContext.getCurrentInstance().addMessage(null, facesMessage);
            backing.requestsList = null;
        }
    }

..backing getter and setter
}

My requestsList is on my RequestBacking where I have all my getters and setters, please correct me if there is a better way of doing this, I'm using it because it leaves my controller cleaner.

public List<RequestVO> requestsList;

1 Answers1

1

Apparently the value="#{requestController.backing.requestsList}" didn't return the same value as it did on the initial request. That can happen if it's a request scoped bean and/or if the requestsList is populated on every request based on a request based variable.

That's just a design mistake. Put the managed bean in the view scope and make sure that you aren't doing any business logic in a getter method. The nested class backing is also suspicious or it must be a poor naming.

See also:


Update in a nutshell, your bean should look something like this:

@ManagedBean
@ViewScoped
public class Orders {

    private String query; // +getter +setter
    private List<Order> results; // +getter (no setter required)

    @EJB
    private OrderService service;

    public void search() {
        results = service.search(query);
    }

    // Add/generate normal getters/setters (don't change them!)
}

and your view should look like this:

<h:form>
    <p:inputText value="#{orders.query}" />
    <p:commandButton value="Search" action="#{orders.search}" update=":tableForm" />
</h:form>
<h:form id="tableForm">
    <p:dataTable value="#{orders.results}" var="order" ... rendered="#{not empty orders.results}">
        ...
    </p:dataTable>
    <h:outputText value="No results found" rendered="#{facesContext.postback and empty orders.results}" />
</h:form>
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • when I click a column to order, it makes the request again? In this case, the `requestsList` is result of a search based on a date informed by the user. Have done that without ajax and was working. –  May 22 '12 at 19:02
  • Surely it makes a new request. Check the HTTP traffic and server logs. – BalusC May 22 '12 at 19:04
  • changin the bean to `@ViewScoped` and my method was not called, nothing happens. Ajax, in this case is working only with `@RequestScoped` bean –  May 22 '12 at 19:05
  • It's hard to tell without seeing backing bean logic. But based on backing bean code provided in your previous questions I won't be surprised if it's another thinking mistake again caused by that misleading ICEfaces article. – BalusC May 22 '12 at 19:06
  • I've added a kickoff example to the answer which should work just fine. – BalusC May 22 '12 at 19:11
  • You're creating the `backing` on every single request, so it get recreated on every subsequent ajax request. Please check the "How to choose the right bean scope?" link and carefully read the answer and links therein. – BalusC May 22 '12 at 19:17
  • I'm gonna read but something came to my mind, is it possible to use `@ManagableProperty` in this case? –  May 22 '12 at 19:20
  • 1
    No, this is not intented for this purpose. You've there just a design mistake. Rewrite it based on the kickoff example. Your `changeEventListener` is by the way strange. It seems completely superfluous. – BalusC May 22 '12 at 19:21
  • I've read and now I totally understand how and when to use each scope, great answer. Now there is one thing I'm not clear, when I change my controller to `@ViewScoped`, it is not making my ajax call –  May 22 '12 at 19:26