简体   繁体   English

p:数据表分页不起作用

[英]p:datatable pagination does not work

I'm trying to render a DataTable with pagination but no code snippet i found so far really works. 我正在尝试使用分页来呈现DataTable,但是到目前为止我发现的代码片段都没有。 I guess I'm missing something very little ... 我想我缺少了一些东西...

This is my test site with very reduced code and it still does not work: 这是我的测试站点,代码减少了很多,仍然无法正常工作:

test.xhtml 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 (Code taken from http://www.primefaces.org/showcase/ui/data/datatable/paginator.xhtml ) 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);
}

} }

My output is always: 我的输出始终是:

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

Anyone can help me here ? 有人可以在这里帮助我吗?

UPDATE 1 Even if I use the following Code 更新1即使我使用以下代码

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

and

@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;
}

it still does not work 它仍然不起作用

Maybe there is just that you didn't paste all the code, but i am missing scope. 也许只是您没有粘贴所有代码,但我错过了范围。

Try put 试穿

@SessionScope

just before 就在之前

@Named

And keep code that Kukeltje gave to you. 并保留Kukeltje给您的代码。 It's definitely step in right direction 绝对是正确的方向

UPDATE: try to add tags in ui:composition 更新:尝试在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">

I think css are not loaded 我认为CSS没有加载

UPDATE 2: 更新2:

Here is code with working pagination from primefaces showcase. 这是primefaces展示柜中带有工作分页的代码。 Compare with your code and test it. 与您的代码进行比较并进行测试。

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

I faced this issue. 我遇到了这个问题。 But the problem was that I had two datalist have the same widgetVar name. 但是问题是我有两个数据列表具有相同的widgetVar名称。 Maybe it helps to somebody. 也许对某人有帮助。

Check via adding as 'Sytem.out.println' how often your 'createCars(50)' is called. 通过添加为“ Sytem.out.println”来检查“ createCars(50)”的调用频率。 Most likely way more than you expect. 最有可能超出您的预期。 And then look at the differences with the PrimeFaces Showcase. 然后查看与PrimeFaces Showcase的区别。

The code you point to does this: 您指向的代码执行以下操作:

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

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

and

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

While you directly call the createCars(...) . 当您直接调用createCars(...)

And it is weird that all 'examples' you saw did not work, since the PrimeFaces showcase works perfectly. 奇怪的是,您看到的所有“示例”都无法正常工作,因为PrimeFaces展示柜可以完美地工作。

See also: 也可以看看:

The problem was clearly nothing of what we all thought of ... it was just the wrong usage of <ui:composition> ... </ui:composition> 问题显然与我们所有人的想法无关……只是<ui:composition> ... </ui:composition>的错误用法

After I deleted the tag it was fixed. 删除标签后,它已修复。 The problem here was that <ui:composition> ignores ALL content around it (even tags like <head> of <body> ). 这里的问题是<ui:composition>忽略它周围的所有内容(甚至是<body> <head>之类的标签)。 This means that the xhtml file wasn't parsed as a correct html-page. 这意味着未将xhtml文件解析为正确的html页。 And without any <head> -tag the javascript could not be included, so the functionality was disabled and the pagination didn't work as it should. 而且,如果没有任何<head> -tag,则无法包含javascript,因此该功能被禁用,因此分页无法正常工作。

I hope this helps anyone out at some point. 我希望这可以在任何时候帮助任何人。 I will clearly never do this mistake again. 我显然永远不会再犯此错误。

Thanks anyway for all of your time and help you spent on this! 无论如何,感谢您的所有时间,并帮助您度过这一切!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM