簡體   English   中英

p:數據表分頁不起作用

[英]p:datatable pagination does not work

我正在嘗試使用分頁來呈現DataTable,但是到目前為止我發現的代碼片段都沒有。 我想我缺少了一些東西...

這是我的測試站點,代碼減少了很多,仍然無法正常工作:

test.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui">

<body>
<ui:composition>

    <h:form>
        <p:dataTable id="dataTable" var="car" value="#{testBean.createCars(50)}"
            paginator="true" rows="10"
            paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
            rowsPerPageTemplate="5,10,15">
            <f:facet name="header">
            Ajax Pagination
        </f:facet>

            <p:column>
                <f:facet name="header">
                    <h:outputText value="Brand" />
                </f:facet>
                <h:outputText value="#{car.brand}" />
            </p:column>

            <p:column>
                <f:facet name="header">
                    <h:outputText value="Year" />
                </f:facet>
                <h:outputText value="#{car.year}" />
            </p:column>

            <p:column>
                <f:facet name="header">
                    <h:outputText value="Color" />
                </f:facet>
                <h:outputText value="#{car.color}" />
            </p:column>
        </p:dataTable>
    </h:form>

</ui:composition>
</body>
</html>

testBean.java (代碼取自http://www.primefaces.org/showcase/ui/data/datatable/paginator.xhtml

@Named 
public class TestBean implements Serializable {

/** serialVersionUID. */
private static final long serialVersionUID = 1L;

private final static String[] colors;

private final static String[] brands;

static {
    colors = new String[10];
    colors[0] = "Black";
    colors[1] = "White";
    colors[2] = "Green";
    colors[3] = "Red";
    colors[4] = "Blue";
    colors[5] = "Orange";
    colors[6] = "Silver";
    colors[7] = "Yellow";
    colors[8] = "Brown";
    colors[9] = "Maroon";

    brands = new String[10];
    brands[0] = "BMW";
    brands[1] = "Mercedes";
    brands[2] = "Volvo";
    brands[3] = "Audi";
    brands[4] = "Renault";
    brands[5] = "Fiat";
    brands[6] = "Volkswagen";
    brands[7] = "Honda";
    brands[8] = "Jaguar";
    brands[9] = "Ford";
}

public List<Car> createCars(int size) {
    List<Car> list = new ArrayList<Car>();
    for (int i = 0; i < size; i++) {
        list.add(new Car(getRandomId(), getRandomBrand(), getRandomYear(), getRandomColor(), getRandomPrice(), getRandomSoldState()));
    }

    return list;
}

private String getRandomId() {
    return UUID.randomUUID().toString().substring(0, 8);
}

private int getRandomYear() {
    return (int) (Math.random() * 50 + 1960);
}

private String getRandomColor() {
    return colors[(int) (Math.random() * 10)];
}

private String getRandomBrand() {
    return brands[(int) (Math.random() * 10)];
}

public int getRandomPrice() {
    return (int) (Math.random() * 100000);
}

public boolean getRandomSoldState() {
    return (Math.random() > 0.5) ? true : false;
}

public List<String> getColors() {
    return Arrays.asList(colors);
}

public List<String> getBrands() {
    return Arrays.asList(brands);
}

}

我的輸出始終是:

http://i.stack.imgur.com/MXy7Y.png

有人可以在這里幫助我嗎?

更新1即使我使用以下代碼

<p:dataTable id="dataTable" var="car" value="#{testBean.cars}"

@PostConstruct
public void createCars() {
    int size = 50;
    if (cars == null) {
        cars = new ArrayList<Car>();
        for (int i = 0; i < size; i++) {
            cars.add(new Car(getRandomId(), getRandomBrand(), getRandomYear(), getRandomColor(), getRandomPrice(), getRandomSoldState()));
        }
    }
}

public List<Car> getCars() {
    return cars;
}

它仍然不起作用

也許只是您沒有粘貼所有代碼,但我錯過了范圍。

試穿

@SessionScope

就在之前

@Named

並保留Kukeltje給您的代碼。 絕對是正確的方向

更新:嘗試在ui:composition中添加標簽

<html> 
 <body> 
   <ui:composition
   xmlns="http://www.w3.org/1999/xhtml"
   xmlns:ui="http://java.sun.com/jsf/facelets"
   xmlns:h="http://java.sun.com/jsf/html"
   xmlns:f="http://java.sun.com/jsf/core"
   xmlns:p="http://primefaces.org/ui">

我認為CSS沒有加載

更新2:

這是primefaces展示櫃中帶有工作分頁的代碼。 與您的代碼進行比較並進行測試。

 <?xml version="1.0" encoding="UTF-8"?> 
<html xmlns="http://www.w3.org/1999/xhtml"  xmlns:h="http://java.sun.com/jsf/html"  xmlns:f="http://java.sun.com/jsf/core"      xmlns:ui="http://java.sun.com/jsf/facelets"     xmlns:p="http://primefaces.org/ui">     
<h:head>    
</h:head>   
<h:body>        
<h:form prependId="false">          
<p:dataTable id="dataTable" var="car" value="#{tableBean.cars}" paginator="true" rows="10" paginatorTemplate="CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"rowsPerPageTemplate="5,10,15">
            <f:facet name="header">
                Ajax Pagination
            </f:facet>
         <p:column>
            <f:facet name="header">
                <h:outputText value="Brand" />
            </f:facet>
            <h:outputText value="#{car.brand}" />
        </p:column>

        <p:column>
            <f:facet name="header">
                <h:outputText value="Year" />
            </f:facet>
            <h:outputText value="#{car.year}" />
        </p:column>

        <p:column>
            <f:facet name="header">
                <h:outputText value="Color" />
            </f:facet>
            <h:outputText value="#{car.color}" />
        </p:column>  </p:dataTable>

    </h:form>   
</h:body>
 </html>

我遇到了這個問題。 但是問題是我有兩個數據列表具有相同的widgetVar名稱。 也許對某人有幫助。

通過添加為“ Sytem.out.println”來檢查“ createCars(50)”的調用頻率。 最有可能超出您的預期。 然后查看與PrimeFaces Showcase的區別。

您指向的代碼執行以下操作:

@PostConstruct
public void init() {
    cars = service.createCars(50);
}

public List<Car> getCars() {
    return cars;
}

<p:dataTable var="car" value="#{dtPaginatorView.cars}"...

當您直接調用createCars(...)

奇怪的是,您看到的所有“示例”都無法正常工作,因為PrimeFaces展示櫃可以完美地工作。

也可以看看:

問題顯然與我們所有人的想法無關……只是<ui:composition> ... </ui:composition>的錯誤用法

刪除標簽后,它已修復。 這里的問題是<ui:composition>忽略它周圍的所有內容(甚至是<body> <head>之類的標簽)。 這意味着未將xhtml文件解析為正確的html頁。 而且,如果沒有任何<head> -tag,則無法包含javascript,因此該功能被禁用,因此分頁無法正常工作。

我希望這可以在任何時候幫助任何人。 我顯然永遠不會再犯此錯誤。

無論如何,感謝您的所有時間,並幫助您度過這一切!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM