PrimeFaces TreeTable - 停止复选框选择传播

时间:2017-09-28 13:42:24

标签: primefaces jsf-2.2

环境:
    PrimeFaces 6.1
    JSF 2.2
    Tomcat 7.0.23
    Java 1.7.0_79

使用复选框选择实现TreeTable,并且需要分别阻止父节点和子节点的选择传播,以便客户端和服务器端处理。

示例树
父节点
--Child Node 1
----子节点1.1
- 儿童节点2
----子节点2.1

所需行为
选择节点时,只应选择该节点的复选框。

实际行为(开箱即用):
选择节点,子节点和父节点。例如,在上面的示例树中选择子节点2,也选择父节点和子子节点2.1。

TreeTable组件:

<p:treeTable id="treeTable" value="#{Bean.rootNode}" var="item" selectionMode="checkbox" nodeVar="node"
    styleClass="widthFull" showUnselectableCheckbox="true" selection="#{Bean.selectedNodes}" stickyHeader="true">
    <p:ajax event="select" listener="#{Bean.processSelect(item)}" ignoreAutoUpdate="true"/>
    <p:ajax event="unselect" listener="#{Bean.processUnselect(item)}" ignoreAutoUpdate="true"/>
    ....
</p:treeTable>

覆盖PrimeFaces JS功能:
能够通过覆盖PrimeFaces.widget.TreeTable.prototype.propagateUp和PrimeFaces.widget.TreeTable.prototype.getDescendants javascript函数来阻止客户端处理中的传播。

PrimeFaces.widget.TreeTable.prototype.propagateUp = function(node) {
    //do nothing, overriding the TreeTable propagate selection up functionality

}

PrimeFaces.widget.TreeTable.prototype.getDescendants = function(node) {
    //do nothing other than return empty array, overriding the TreeTable propagate selection down functionality by overriding getDescendants...hopefully this doesn't cause other issues
    f = [];
    return f;
}

TreeTable更新:
TreeTable更新作为ajax选择和取消选择事件处理的一部分执行。

RequestContext.getCurrentInstance().update("inventoryForm:treeTable");

问题:
当触发ajax select和unselect事件以禁用特定节点上的可选择性并更新TreeTable时,将选择子节点。处理ajax事件侦听器时,子节点不在Bean的selectedNodes数组中。如何防止在TreeTable组件更新时选择子节点?

1 个答案:

答案 0 :(得分:0)

防止来自服务器端的复选框选择传播可以通过扩展CheckboxTreeNode类并覆盖propagateSelectionDown(boolean)和propagateSelectionUp()方法来实现。当然,您需要使用新类而不是CheckboxTreeNode来构建树内容。

public class MyCheckboxTreeNode extends CheckboxTreeNode {

    public MyCheckboxTreeNode() {
        super();
    }

    public MyCheckboxTreeNode(Object data, TreeNode parent) {
        super(data, parent);
    }

    public MyCheckboxTreeNode(Object data) {
        super(data);
    }

    public MyCheckboxTreeNode(String type, Object data, TreeNode parent) {
        super(type, data, parent);
    }

    @Override
    protected void propagateSelectionDown(boolean value) {
        //Do nothing, overriding CheckboxTreeNode method to prevent propagation down of tree node selections when ajax update is performed.
    }

    @Override
    protected void propagateSelectionUp() {
        //Do nothing, overriding CheckboxTreeNode method to prevent propagation up of tree node selections when ajax update is performed.
    }
}

需要覆盖其他PrimeFaces javascript函数,以防止在后代节点崩溃时传播失败。

//--------Override Primefaces JS
PrimeFaces.widget.TreeTable.prototype.fireSelectNodeEvent = function(b) {
    //Overriding this function in order to prevent selection of descendant nodes when parent is selected. See a.oncomplete function below.
    if (this.isCheckboxSelection()) {
           var e = this
             , a = {
               source: this.id,
               process: this.id
           };
           a.params = [{
               name: this.id + "_instantSelection",
               value: b
           }];
           a.oncomplete = function(k, f, g) {
            //commented out the logic to prevent selection of descendant nodes when parent node is selected
            //if (g.descendantRowKeys && g.descendantRowKeys !== "") {
                   //var j = g.descendantRowKeys.split(",");
                   //for (var h = 0; h < j.length; h++) {
                       //e.addToSelection(j[h])
                   //}
                   //e.writeSelections()
               //}
           }
           ;
           if (this.hasBehavior("select")) {
               var d = this.cfg.behaviors.select;
               d.call(this, a)
           } else {
               PrimeFaces.ajax.AjaxRequest(a)
           }
       } else {
           if (this.hasBehavior("select")) {
               var d = this.cfg.behaviors.select
                 , c = {
                   params: [{
                       name: this.id + "_instantSelection",
                       value: b
                   }]
               };
               d.call(this, c)
           }
       }
}