如何使用p:selectCheckboxMenu过滤带有selectionMode的p:dataTable? [英] How to filter a p:dataTable with selectionMode with p:selectCheckboxMenu?

查看:90
本文介绍了如何使用p:selectCheckboxMenu过滤带有selectionMode的p:dataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一个列标题中使用Primefaces组件 p:selectCheckboxMenu 过滤 p:dataTable 。但是,这不能按预期工作。数据表上的其他过滤器也可以正常工作,例如输入字段。
有问题的列是房间类型,其具有 p:selectCheckboxMenu



过滤操作一次,在selectCheckbox菜单上选中或取消选中复选框不会在表上添加或删除任何过滤。



这里有一个有趣的问题:



如果我删除了 selectionMode = single 来自数据表的code>属性,那么即使在第一个复选框切换后,排序仍然有效。在这种情况下,我可以切换和取消切换框,并且 p:dataTable 会被相应地过滤。但是,我这里需要选择模式,因为我应该能够选择一行并通过单击来导航到另一个视图。当数据表上没有 selectionMode 属性时,这将不起作用。



这是我的数据表:

 < div class = background> 

< div class = freeRoomsContent>

< br />
< p:outputLabel value =免费房间 styleClass = headerfont />
< br />
< h:form id = freeRoomsForm>
< p:dataTable id = freeRoomsTable var = room paginatorPosition = bottom paginatorAlwaysVisible = false
value =#{freeRoomsController.freeRoomsList} selectionMode = single selection = #{freeRoomsController.room}
rowKey =#{room.roomId} widgetVar = freeRoomsTable
paginator = true rows = 20 pageLinks = 5 scrollable = false
paginatorTemplate = {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}
rowsPerPageTemplate = 20,50,100 skipChildren = true emptyMessage =没有可用的房间。
< p:ajax event = rowSelect listener =#{freeRoomsController.onRowSelect} />
< p:列headerText =房间ID sortBy =#{room.roomId} filterMatchMode = contains filterBy =#{room.roomId}>
< h:outputText value =#{room.roomId} />
< / p:列>

< p:栏headerText =房间号 sortBy =#{room.roomNumber} filterMatchMode = contains filterBy =#{room.roomNumber}>
< h:outputText value =#{room.roomNumber} />
< / p:列>
<!-其他类似的列->

< p:列headerText =房间类型 filterMatchMode =精确 filterBy =#{room.roomType}>
< f:facet name = filter>
< p:selectCheckboxMenu onchange = PF(’freeRoomsTable’)。filter()
label = Room type>
< f:selectItems value =#{staticData.roomTypes} var = rt itemLabel =#{msg [rt.name]} itemValue =#{rt.name}
/>
< p:ajax event = change process = @ this update = freeRoomsForm />
< p:ajax event = toggleSelect process = @ this update = freeRoomsForm />
< / p:selectCheckboxMenu>
< / f:facet>
< h:outputText value =#{msg [room.roomtype.name]}>
< f:convertDateTime pattern = dd.MM.yyyy />
< / h:outputText>
< / p:列>

<!-正常的输入字段起作用->

< / p:dataTable>
< / h:form>
< / div>
< / div>


解决方案

这对我有用:



  • p:dataTable value 和 filteredValue >

  • 仅使用 onchange = PF('freeRoomsTable')。filter(),不带子元素 p:selectCheckboxMenu

    中的> p:ajax
    元素

  • 设置<$ p:selectCheckboxMenu 的c $ c> value 属性,例如 value =#{someBean.selectedItems}



  • 在您的bean中,使用 private String [] selectedItems ,带有getter和setter



  • 在您的bean实现中, filterFunction ,方法必须具有签名布尔值(对象,对象,区域设置),在本示例中,仅使用了第一个参数(对象值):

      public boolean filterFunction(Object value,Object filter,Locale locale){

    // instanceof可能不需要
    if(value == null ||! (value instanceof String)){
    返回true;
    }

    字符串valueInRow =(String)value;

    //如果未选择任何内容,则在表中显示行,即返回true,
    //如果(selectedItems == null || selectedItems.length == 0){
    返回true;
    }

    //如果行中的项目与标题中选择的任何项目匹配,则
    //在表中显示,即为true返回
    (int i = 0; i< selectedItems.length; i ++){
    if(selectedItems [i] .equals(valueInRow)){
    返回true;
    }
    }

    //如果不想在表格中显示行,则返回false
    return false;

    }

  • p中:列,调用 filterfunction =#{someBean.filterFunction} -没有参数,没有括号,不需要使用任何 filterMatchMode ,仅保留 filterBy


    I'm trying to filter a p:dataTable with Primefaces component p:selectCheckboxMenu in one column header. This however, does not work as intended. The other filters on the datatable work just fine, such as input fields. The column in question is the Room type, that has the p:selectCheckboxMenu.

    The filtering works once, after that checking or unchecking boxes on the selectCheckbox menu doesn't add or remove any filtering on the table.

    Here's an interesting bit on the problem:

    If I remove the selectionMode="single" attribute from the datatable, then the sorting works even after the first checkBox toggle. As in, I can toggle and untoggle a box and the p:dataTable gets filtered accordingly. But, I need the selection mode here, since I'm supposed to be able to select a row and navigate to another view by clicking it. That doesn't work when there's no selectionMode attribute on the datatable.

    Here's my datatable:

    <div class="background">
    
            <div class="freeRoomsContent">
    
                <br/>
                <p:outputLabel value="free rooms" styleClass="headerfont"/>
                <br/>
        <h:form id="freeRoomsForm">
            <p:dataTable id="freeRoomsTable" var="room" paginatorPosition="bottom" paginatorAlwaysVisible="false"
                         value="#{freeRoomsController.freeRoomsList}" selectionMode="single" selection="#{freeRoomsController.room}"
                         rowKey="#{room.roomId}" widgetVar="freeRoomsTable"
                         paginator="true" rows="20" pageLinks="5" scrollable="false"
                         paginatorTemplate="{FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                         rowsPerPageTemplate="20,50,100" skipChildren="true" emptyMessage="No free rooms available.">
                <p:ajax event="rowSelect" listener="#{freeRoomsController.onRowSelect}" />
                <p:column headerText="Room Id" sortBy="#{room.roomId}" filterMatchMode="contains" filterBy="#{room.roomId}">
                    <h:outputText value="#{room.roomId}"/>
                  </p:column>
    
                <p:column headerText="Room number" sortBy="#{room.roomNumber}" filterMatchMode="contains" filterBy="#{room.roomNumber}">
                    <h:outputText value="#{room.roomNumber}" />
                </p:column>
    <!-- other similar columns -->
    
                <p:column headerText="Room type" filterMatchMode="exact" filterBy="#{room.roomType}">
                    <f:facet name="filter">
                        <p:selectCheckboxMenu  onchange="PF('freeRoomsTable').filter()"
                                         label="Room type">
                            <f:selectItems value="#{staticData.roomTypes}" var="rt" itemLabel="#{msg[rt.name]}" itemValue="#{rt.name}"
                                           />
                        <p:ajax event="change" process="@this" update="freeRoomsForm" />
                        <p:ajax event="toggleSelect" process="@this" update="freeRoomsForm" />
                        </p:selectCheckboxMenu>
                    </f:facet>
                    <h:outputText value="#{msg[room.roomtype.name]}">
                        <f:convertDateTime pattern="dd.MM.yyyy" />
                    </h:outputText>
                </p:column>
    
    <!-- normal input field columns that work -->
    
            </p:dataTable>
        </h:form>
    </div>
    </div>
    

    解决方案

    This worked for me:

  • Using value and filteredValue in p:dataTable

  • Use only onchange=PF('freeRoomsTable').filter(), without child p:ajax elements in p:selectCheckboxMenu

  • Set value attribute for p:selectCheckboxMenu, e.g. value="#{someBean.selectedItems}"

  • In your bean, use private String[] selectedItems, with getter and setter

  • In your bean implement filterFunction, method need to have signature boolean (Object, Object, Locale) altough for this example only 1st argument (Object value) is used:

    public boolean filterFunction(Object value, Object filter, Locale locale) {
    
        // instanceof checking probably not needed
        if (value == null || !(value instanceof String)) {
            return true;  
        }
    
        String valueInRow = (String)value;
    
        // if nothing is selected, show row in table i.e. return true, 
        // you can play with this ofcourse
        if (selectedItems == null || selectedItems.length == 0) {
            return true;
        }
    
        // if item in row matches any of the items that were selected in header,    
        // show in table i.e. return true
        for (int i = 0; i < selectedItems.length; i++) {
            if (selectedItems[i].equals(valueInRow)) {
                return true;
            }
        }
    
        // if you don't want to show row in table, return false
        return false;
    
    }
    

  • In p:column, call filterfunction=#{someBean.filterFunction} - no arguments, no parenthesis, no need to use any filterMatchMode, leave only filterBy

    这篇关于如何使用p:selectCheckboxMenu过滤带有selectionMode的p:dataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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