在数据表中获取过滤后的列表 [英] Getting the filtered list in datatable

查看:100
本文介绍了在数据表中获取过滤后的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用列中的过滤器字段过滤数据表,并获取过滤后的列表(填充数据表),并根据页面中的过滤后列表值更新另一个组件(Toplam TL Teminati和Toplam Dolar Teminati). /p>

I am trying to filter a datatable using filter field in the column and get the filtered list (which populates datatable) and update another component (Toplam TL Teminati and Toplam Dolar Teminati) according to the filtered list's values in my page.

Primefaces 2.2.1和JSF 2是否有这样做的方法?如果不能,您可以推荐这种情况的解决方法吗?

谢谢.

推荐答案

为您提供一个示例,说明如何在Primefaces 2.2.1 dataTable中实现自定义过滤器逻辑,我为您提供了代码的扩展版本.

解决方案

>

    <p:dataTable value="#{listBeans.beans}" var="bean" dynamic="true" paginator="true" rows="20" paginatorPosition="bottom"
                 emptyMessage="No Beans" loadingMessage="Loading. . ." selectionMode="single" selection="#{listBeans.selectedBean}"
                 id="beanList" widgetVar="beanTable">
        <f:facet name="header">
            <h:panelGrid columns="4">
                <h:outputText value="Bean List" />
                <p:outputPanel>
                    <h:outputText value="Year Filter: " />
                    <h:selectOneMenu value="#{listBeans.yearFilter}"
                        onchange="yearFilterBtn.jq.click(); refreshFilters();">
                        <f:selectItems value="#{listBeans.years}" />

                    </h:selectOneMenu>
                    <p:commandButton id="yearFilterBtn" widgetVar="yearFilterBtn" action="#{listBeans.filterYears}"
                        update="listBeansForm:beanList" style="display:none;" />

                </p:outputPanel>
                <p:outputPanel>
                    <h:outputText value="Filter By Beanchild: " />
                    <p:inputText value="#{listBeans.beanchildFilter}" style="width: 150px; margin-right: 4px;" />
                    <!-- refreshFilters forces the visitor filter to refresh the selection if column filters are selected. -->
                    <p:commandButton oncomplete="refreshFilters();" value="Filter"
                        action="#{listBeans.filterBeanchildren}" update="listBeansForm:beanList" />
                </p:outputPanel>
                <p:commandButton value="Export to XLS" ajax="false">
                    <p:dataExporter target="beanList" type="xls" fileName="BeanReport"
                        excludeColumns="0,5,6" postProcessor="#{listBeans.postProcessExcelReport}" />
                </p:commandButton>
            </h:panelGrid>
        </f:facet>  

        <p:column style="width:16px">
            <p:rowToggler />
        </p:column>
        <p:column filterStyleClass="filtertag" filterBy="#{bean.beanDateDisplay}" filterMatchMode="startsWith">

....
    </p:dataTable>

实际上,关于托管bean代码没有太多细节,命令按钮的操作实际上是将过滤器参数传递给我的BO层,该BO参数重新查询数据库以获取beans的新列表.需要dataTable组件的显式update来刷新数据.

Without really going into too much detail about the managed bean code, the actions of the command button are essentially passing filter arguments to my BO layer that requery the database for the new list of beans. The explicit update of the dataTable component is needed to refresh the data.

您会注意到,在未隐藏的显式Beanchild筛选器按钮上,我有一个oncomplete属性,该属性引用了名为refreshFilters()的javascript函数.我不记得确切的问题,但是当列过滤器值存在并且dataTable本身发生异步更新时,我认为这是Primefaces 2.2.1版本中的错误.这是javascript函数:

You will notice that on the explicit Beanchild Filter button that is not hidden, I have an oncomplete attribute that references a javascript function called refreshFilters(). I can't remember exactly the problem I had, but I think it is a bug in the 2.2.1 version of Primefaces when a column filter value exists and an asynchronous update occurs within the dataTable itself. Here is the javascript function:

function refreshFilters() {
    var filters = jQuery('.filtertag');
    var uppedOnce = false;
    filters.each(function(idx) {
        var curEl = jQuery(this);
        if (curEl.val() != '') {
            curEl.keyup();
            uppedOnce = true;
        }
    });

    if (!uppedOnce) {
        jQuery(filters[0]).keyup();
    }           
}

您可以在这里看到我正在查找具有类filtertag的每个DOM元素,这将是列过滤器.我说的是,如果服务器操作完成后该字段中存在一个值,那么我将手动触发一个keyup事件,因为这将使用之前的值重新过滤"该列.

You can see here that I am locating every DOM element that has the class filtertag, which would be the column filters. I am saying that if a value exists in that field after the server action is complete, then I am manually triggering a keyup event as this will "refilter" the column with the previous value from before.

我不确定在更高版本的Primefaces中是否有必要,甚至现在也没有必要,因此我建议尝试在没有Javascript解决方法的情况下进行此操作,如果遇到使两者协调的问题,则可以考虑使用Javascript.

I am not sure if it is necessary in later versions of Primefaces, I am not even sure it is necessary now, so I suggest trying to do this without the Javascript workaround and if you run into problems cooridnating the two then you can consider using the Javascript.

这篇关于在数据表中获取过滤后的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆