获取数据表中过滤后的列表

发布于 2025-01-03 00:42:18 字数 260 浏览 0 评论 0原文

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

在此处输入图像描述

在 Primefaces 2.2.1 和 JSF 2 中是否可以执行此操作?如果没有,您能否推荐一个针对这种情况的解决方法?

提前致谢。

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.

enter image description here

Is there anyway to do this in Primefaces 2.2.1 and JSF 2? If not can you rec commend a workaround for this case?

Thanks in advance.

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

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

发布评论

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

评论(1

絕版丫頭 2025-01-10 00:42:18

为了向您展示我如何在 Primefaces 2.2.1 数据表中实现自定义过滤器逻辑的示例,我向您提供了我的代码的缩放版本。

    <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 层,该层重新查询数据库以获取新的 beans 列表。需要对 dataTable 组件进行显式更新来刷新数据。

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

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 事件,因为这将使用之前的值“重新过滤”该列。

我不确定在 Primefaces 的更高版本中是否有必要,我什至不确定现在是否有必要,所以我建议尝试在不使用 Javascript 解决方法的情况下执行此操作,如果您在协调两者时遇到问题,那么您可以考虑使用JavaScript。

To give you an example of how I achieved custom filter logic in my Primefaces 2.2.1 dataTable, I am giving you a scaled version of my code.

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

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.

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();
    }           
}

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.

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.

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