显示战斧分页、可排序数据表中的所有行

发布于 2024-10-27 04:30:15 字数 3037 浏览 0 评论 0原文

我正在尝试使用 MyFaces 创建分页、可排序的数据表。排序工作正常;我可以单击列标题,它将根据该列对数据进行排序。而且分页在大多数情况下都工作得很好。数据表将适当地分割自身,每页包含一定数量的项目。此外,我希望用户能够更改每页显示的项目数。同样,这似乎一直有效,直到我希望所有项目都显示在一页上。

根据此参考(也此处),如果您设置了 t 的“rows”属性:datatable 为“0”,则会显示表中剩余的行。然而,当我尝试这样做时,我收到一个异常,其中包含以下消息:

javax.faces.FacesException - You need to set a value to the 'rows' attribute of component 'myComponent'

我正在尝试使用支持 bean 中的属性来设置每页的项目数。我的 t:datatable 看起来像这样:

<t:dataTable id="myComponent" var="cur"
    value="#{backingBean.list}" sortAscending="#{backingBean.ascending}"
    sortColumn="#{backingBean.sortColumn}" sortable="true"
    styleClass="myClass" rowClasses="oddRow,evenRow"
    rows="#{backingBean.itemsPerPage}" preserveDataModel="false">
    <!-- data here -->
</t:datatable>

后来,我有一个 t:dataScroller 来控制分页:

<t:dataScroller id="pageNavigation" for="myComponent"
    paginatorActiveColumnStyle="font-weight:bold;"
    renderFacetsIfSinglePage="false" 
    binding="#{backingBean.scroller}" 
    paginator="true" >
    <!-- facets here -->
</t:dataScroller>

然后,我有一个 h:selectOneMenu选择每页的项目数

<h:selectOneMenu id="myScroller"
    value="#{backingBean.itemsPerPage}"
    required="true" onchange="this.form.submit();"
    valueChangeListener="#{backingBean.updateItemsPerPage}">
    <f:selectItems value="#{backingBean.itemsPerPageArray}" />
</h:selectOneMenu>

我的支持 bean 看起来像这样:

public class BackingBean {

    private boolean ascending;
    private Long itemsPerPage;
    private String sortColumn;
    private ArrayList<SelectItem> itemsPerPageArray;
    private ArrayList<SomeObject> list;  // data for table
    private HtmlDataScroller scroller;

    // constructors, getters, setters, and other stuff here

    public void updateItemsPerPage(ValueChangeEvent valueChangeEvent) {
        itemsPerPage = (Long) valueChangeEvent.getNewValue();
        resetScrollerIndex();
    }

    private void resetScrollerIndex() {
        if (scroller!=null && scroller.isPaginator())
            scroller.getUIData().setFirst(0);
    }

    // called in constructor
    private void constructItemsPerPageArray() {
        itemsPerPageArray = new ArrayList<SelectItem>();
        itemsPerPageArray.add(new SelectItem(new Long(10), "10"));
        itemsPerPageArray.add(new SelectItem(new Long(50), "50"));
        itemsPerPageArray.add(new SelectItem(new Long(100), "100"));
        itemsPerPageArray.add(new SelectItem(new Long(0), "All"));
    }
}

总而言之,当我从 h:selectOneMenu 中选择“All”项目时,我得到了上面提到的异常。希望我已经包含了适当的细节。谢谢!

I'm trying to create a paginated, sortable datatable using MyFaces. The sorting works fine; I can click on a column header and it will sort the data based on the column. Also the pagination works fine for the most part. The datatable will split itself appropriately with some number of items per page. In addition, I want the user to be able to change the number of items displayed per page. Again, this seems to be working until I want all of the items displayed on one page.

According to this reference (also here), if you set the "rows" attribute of t:datatable to "0", it will display the remaining rows in the table. However, when I try this, I get an exception that includes this message:

javax.faces.FacesException - You need to set a value to the 'rows' attribute of component 'myComponent'

I'm trying to set the number of items per page using an attribute in a backing bean. My t:datatable looks like this:

<t:dataTable id="myComponent" var="cur"
    value="#{backingBean.list}" sortAscending="#{backingBean.ascending}"
    sortColumn="#{backingBean.sortColumn}" sortable="true"
    styleClass="myClass" rowClasses="oddRow,evenRow"
    rows="#{backingBean.itemsPerPage}" preserveDataModel="false">
    <!-- data here -->
</t:datatable>

Later, I have a t:dataScroller to control the pagination:

<t:dataScroller id="pageNavigation" for="myComponent"
    paginatorActiveColumnStyle="font-weight:bold;"
    renderFacetsIfSinglePage="false" 
    binding="#{backingBean.scroller}" 
    paginator="true" >
    <!-- facets here -->
</t:dataScroller>

Then, I have a h:selectOneMenu to select the number of items per page

<h:selectOneMenu id="myScroller"
    value="#{backingBean.itemsPerPage}"
    required="true" onchange="this.form.submit();"
    valueChangeListener="#{backingBean.updateItemsPerPage}">
    <f:selectItems value="#{backingBean.itemsPerPageArray}" />
</h:selectOneMenu>

My backing bean looks something like this:

public class BackingBean {

    private boolean ascending;
    private Long itemsPerPage;
    private String sortColumn;
    private ArrayList<SelectItem> itemsPerPageArray;
    private ArrayList<SomeObject> list;  // data for table
    private HtmlDataScroller scroller;

    // constructors, getters, setters, and other stuff here

    public void updateItemsPerPage(ValueChangeEvent valueChangeEvent) {
        itemsPerPage = (Long) valueChangeEvent.getNewValue();
        resetScrollerIndex();
    }

    private void resetScrollerIndex() {
        if (scroller!=null && scroller.isPaginator())
            scroller.getUIData().setFirst(0);
    }

    // called in constructor
    private void constructItemsPerPageArray() {
        itemsPerPageArray = new ArrayList<SelectItem>();
        itemsPerPageArray.add(new SelectItem(new Long(10), "10"));
        itemsPerPageArray.add(new SelectItem(new Long(50), "50"));
        itemsPerPageArray.add(new SelectItem(new Long(100), "100"));
        itemsPerPageArray.add(new SelectItem(new Long(0), "All"));
    }
}

To sum up, when I select the "All" item from the h:selectOneMenu, I get the exception mentioned above. Hopefully, I've included an appropriate level of detail. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘你却要生生世世 2024-11-03 04:30:15

好吧,我想我找到了答案...

如果我从 updateItemsPerPage 方法中删除第一行,问题就解决了。该方法现在看起来像:

public void updateItemsPerPage(ValueChangeEvent valueChangeEvent) {
    resetScrollerIndex();
}

它认为我已经尝试过......

Alright, I think I found the answer...

If I remove the first line from the updateItemsPerPage method, the problem is fixed. The method now looks like:

public void updateItemsPerPage(ValueChangeEvent valueChangeEvent) {
    resetScrollerIndex();
}

It thought I had tried that...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文