How do you update the page size of a PrimeFaces datatable dynamically?

霸气de小男生 提交于 2021-02-08 10:38:27

问题


I am trying to update the page size of a PrimeFaces datatable dynamically, after the datatable is displayed on the page but I can't seem to be able to do that. I am using a lazy loaded datatable if that matters... putting an EL expression in the rows attribute and doing a table refresh does not work and causes the paginator to return no data.

Any ideas if this is a bug and how to fix it?

Thanks


回答1:


You need not refresh or update the datatable. Use EL expression in rowsPerPageTemplate attribute of <p:dataTable> and after loading datatable just change the rows number.

<p:dataTable value="#{managedBean.users}" var="user" lazy="true" paginator="true" rows="10" rowsPerPageTemplate="10,25 #{fn:length(managedBean.users)}">

And include this xmlns:fn="http://java.sun.com/jsp/jstl/functions" in page.




回答2:


What exactly are you trying to do? This works for me (Mojarra 2.1.26, PrimeFaces 3.5):

@ManagedBean
@ViewScoped
public class Bean {

    public class Item {

        private String name;

        public Item(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    private List<Item> items;

    public Bean() {
        Item item1 = new Item("item1");
        Item item2 = new Item("item2");
        items = Arrays.asList(item1, item2);
    }

    public List<Item> getItems() {
        return items;
    }

    private int tableSize = 1;

    public int getTableSize() {
        return tableSize;
    }

    public void actionSwitchSize() {
        tableSize = tableSize == 1 ? 2 : 1;
    }

}
<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">
<h:head />
<h:body>
    <h:form>
        <p:dataTable id="table" var="item" value="#{bean.items}"
            rows="#{bean.tableSize}" paginator="true">

            <p:column>
                    #{item.name}
                </p:column>

        </p:dataTable>
        <p:commandButton value="switch size" action="#{bean.actionSwitchSize}"
            update="table" />
    </h:form>
</h:body>
</html>


来源:https://stackoverflow.com/questions/18908722/how-do-you-update-the-page-size-of-a-primefaces-datatable-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!