本文介绍了排序在 PrimeFaces 的数据表中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

排序在 PrimeFaces 的数据表中不起作用.请提出建议.

Sorting is not working in datatable in PrimeFaces. Please suggest.

见下面我的 .xhtml 文件

See below my .xhtml file

<h:form>

  <p:dataTable style="width: 60%" id="dt1" value="#{bean.list}" var="entry" first="0" paginator="true" rows="10" paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}" rowsPerPageTemplate="5,10,15" emptyMessage="No cars found with given criteria" >

    <f:facet name="header">
      <h2>Cars View</h2>
    </f:facet>

    <p:column sortBy="#{entry.carno}" filterBy="#{entry.carno}">
      <f:facet name="header">
        <h:outputText value="Car Number" />
      </f:facet>
      <h:outputText value="#{entry.carno}"></h:outputText>
    </p:column>

    <p:column sortBy="#{entry.carsettings['car-model']}" filterBy="#{entry.carsettings['car-model']}">
      <f:facet name="header">
        <h:outputText value="Car Model"/>
      </f:facet>
      <h:outputText value="#{entry.carsettings['car-model']}"></h:outputText>
    </p:column>

    <p:column sortBy="#{entry.carsettings.year}" filterBy="#{entry.carsettings.year}">
      <f:facet name="header">
        <h:outputText value="Car Year"/>
      </f:facet>
      <h:outputText value="#{entry.carsettings.year}"></h:outputText>
    </p:column>

    <p:column sortBy="#{entry.carsettings.color}" filterBy="#{entry.carsettings.color}">
      <f:facet name="header">
        <h:outputText value="Car Color"/>
      </f:facet>
      <h:outputText value="#{entry.carsettings.color}"></h:outputText>
    </p:column>
  </p:dataTable>
</h:form>

@肖恩

看下面的代码

汽车清单

    <ui:composition template="/template.xhtml">
        <ui:define name="content">
            <f:view>
                <f:event type="preRenderView" listener="#{MyBackingBean.load}"></f:event>
                <center>
                    <h1>Car View</h1>
                    <h:outputText value="No data found" style="font-size: 15px;font-family: Arial, Verdana,Helvetica, sans-serif" rendered="#{MyBackingBean.noDataExist}"></h:outputText>
                    <h:form id="dataform1">
                        <p:dataTable var="item" value="#{MyBackingBean.dataList}" dynamic="true" paginator="true" rows="2" id="table"  style="width:60%"
                                     rendered="#{!MyBackingBean.noDataExist}" >
                            <p:column sortBy="#{item.id}" filterBy="#{item.id}">
                                <f:facet name="header">
                                    <h:outputText value="ID" />
                                </f:facet>
                                <h:outputText value="#{item.id}" />
                            </p:column>
                            <p:column sortBy="#{item.carsettings['car-color']}" filterBy="#{item.carsettings['car-color']}">
                                <f:facet name="header">
                                    <h:outputText value="Color" />
                                </f:facet>
                                <h:outputLink target="_blank" value="http://#{item.carsettings['car-color']}">
                                    <h:outputText value="#{item.carsettings['car-color']}" />
                                </h:outputLink>
                            </p:column>
                            <p:column sortBy="#{item.carsettings.model}" filterBy="#{item.carsettings.model}">
                                <f:facet name="header">
                                    <h:outputText value="Model" />
                                </f:facet>
                                <h:outputText value="#{item.carsettings.model}" />
                            </p:column>
                            <p:column sortBy="#{item.carsettings.manufacturer}" filterBy="#{item.carsettings.manufacturer}">
                                <f:facet name="header">
                                    <h:outputText value="Manufacturer" />
                                </f:facet>
                                <h:outputText value="#{item.carsettings.manufacturer}" />
                            </p:column>
                        </p:dataTable>
                    </h:form>
                </center>
            </f:view>
        </ui:define>
    </ui:composition>
</h:body>

排序在上面的代码中不起作用

Sorting is not working in the above code

请帮忙

推荐答案

primefaces 支持 bean (ViewScoped!) 必须拥有它自己的行列表.因此,例如,如果每次请求 p:dataTable:value 时都查询数据库,则排序将不起作用.

The primefaces backing bean (ViewScoped!) must hold it's own List of rows. So, e.g., if you query the database every time you request the p:dataTable:value, sorting will not work.

解决方案:从数据库中收集列表并将其保存在支持 bean 的本地列表变量中.

Solution: Collect the List from the Database and keep it in a local List variable in the backing bean.

public List<Row> getDataTable() {
    if (tableDataList == null)
        tableDataList = loadListOnce();
    return tableDataList;
}

这篇关于排序在 PrimeFaces 的数据表中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:33