DataTable按列擦除值排序

时间:2012-05-22 18:54:55

标签: ajax jsf datatable primefaces

我有一个由 Ajax 调用填充的PrimeFaces dataTable

当我点击列标题时,为了对其值进行排序,值会消失。

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

以下是我的观点:

<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>

修改

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
}

我的requestsList位于我的RequestBacking,我有我的所有吸气剂和制定者,请纠正我,如果有更好的方法这样做,我正在使用它,因为它让我的控制器更清洁

public List<RequestVO> requestsList;

1 个答案:

答案 0 :(得分:1)

显然value="#{requestController.backing.requestsList}"没有返回与初始请求相同的值。如果它是请求范围的bean和/或基于基于请求的变量在每个请求上填充requestsList,则会发生这种情况。

这只是一个设计错误。将托管bean放在视图范围中,并确保您没有在getter方法中执行任何业务逻辑。嵌套类backing也是可疑的,或者它必须是一个糟糕的命名。

另见:


简而言之,

更新,您的bean应该如下所示:

@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!)
}

您的观点应如下所示:

<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>