【问题标题】:SAPUI5 TreeTable - Node expanding behaviorSAPUI5 TreeTable - 节点扩展行为
【发布时间】:2015-04-22 14:51:24
【问题描述】:

问题是:

当我在运行时添加行时,我试图保持 TreeTable 中的节点展开。 TreeTable 的默认行为是,当它发生某些事情时,它会再次呈现并且所有节点都被折叠。

API 仅提供保持第一级扩展的方法,但我也喜欢保持较低级别的节点扩展。我怎样才能做到这一点?

在添加一行之前:

添加一行后:

期望:

[编辑]

我已经尝试通过使用 expand(iRowIndex) 来获得正确的行为,但就我而言,该 TreeTable 的生命周期(添加内容、重新渲染)没有帮助。

我在做什么:

我正在尝试使用拖放功能添加数据。当我们试图将内容添加到 TreeTable 的特定位置时,我们必须获得父元素和子元素的正确位置。不幸的是,在添加所述内容后,第二个级别被隐藏了,这与我的拖放操作相混淆,因为表格行在折叠时具有不同的 ID。

基本上我需要一个像 ."setExpandFirstLevel(true)" 这样的 TreeTable 函数用于所有其他级别。

【问题讨论】:

    标签: javascript sapui5 treetable


    【解决方案1】:

    这有点脏,但您可以使用TreeTable 的expand(iRowIndex) 方法,在遍历每个行项目时调用它


    编辑:我创建了一个工作示例(见下文),表明您不需要使用 rowID 或向 DOM 添加任何控件。拖放应该做的唯一一件事就是将子节点添加到所选节点仅使用模型。
    但实际上,expand(rowIndex) 工作得非常好,所有可见行都会立即展开(但请参阅下面的 NB2)

    NB1:为简单起见,我没有创建完整的拖放示例,但单击“添加子节点”按钮应该会模拟“拖放”事件。

    NB2:显然expand 方法中存在一个错误:它仅扩展了 可见 树项。滚动后的任何项目都不会展开...

    sap.ui.controller("view1.initial", {
        onInit : function(oEvent) {
            var oModel = new sap.ui.model.json.JSONModel();
            oModel.setData({
                data : [
                    { 
                        name  : "node1", 
                        description : "Lorem ipsum dolor sit amet",
                        data : [
                            { 
                                name : "node1.1", 
                                description : "Cras pretium nisl ac ex congue posuere"
                            },
                            { 
                                name : "node1.2", 
                                description : "Consectetur adipiscing elit",
                                data: [
                                    { 
                                        name : "node1.2.1",
                                        description : "Maecenas accumsan ipsum diam"
                                    }
                               ]
                            },
                            { 
                                name : "node1.3", 
                                description : "Sed tristique diam non imperdiet commodo"
                            },
                            { 
                                name : "node1.4", 
                                description : "Consectetur adipiscing elit",
                                data: [
                                    { 
                                        name : "node1.4.1",
                                        description : "Maecenas accumsan ipsum diam",
                                        data: [
                                            { 
                                                name : "node1.4.1.1",
                                                description : "Maecenas accumsan ipsum diam",
                                                data: [
                                                    { 
                                                        name : "node1.4.1.1.1",
                                                        description : "Maecenas accumsan ipsum diam",
                                                        data: [
                                                            { 
                                                                name : "node1.4.1.1.1.1",
                                                                description : "Maecenas accumsan ipsum diam"
                                                            }
                                                       ]
                                                    }
                                               ]
                                            }
                                       ]
                                    }
                               ]
                            },
                            { 
                                name : "node1.5", 
                                description : "Sed tristique diam non imperdiet commodo"
                            },
                            { 
                                name : "node1.6", 
                                description : "Consectetur adipiscing elit",
                                data: [
                                    { 
                                        name : "node1.6.1",
                                        description : "Maecenas accumsan ipsum diam"
                                    }
                               ]
                            },
                            { 
                                name : "node1.7", 
                                description : "Sed tristique diam non imperdiet commodo"
                            },
    
                        ]
                    },
                ]
            });
            this.getView().setModel(oModel);
        },
    
        onAfterRendering : function() {
            this._doExpandAll();
        },
    
        addNode : function(oEvent) {
            var oContext = oEvent.getSource().getBindingContext();
            var obj      = oContext.getObject();
    
            var oNew = { name : "New node", description : "New description"};
    
            if (!obj.data) obj.data = []; //if no child array, create empty one
    
            obj.data.push(oNew);
    
            this.getView().getModel().setProperty(oContext.getPath(), obj);
    
            this._doExpandAll();
        },
    
        _doExpandAll : function() {
            var oTTbl = this.getView().byId("tbl");
            for (var i=0; i<oTTbl.getRows().length; i++) {
                oTTbl.expand(i);
            }
        }
    });
      
    var app = new sap.m.App({});
    
    var oView = sap.ui.xmlview({
        viewContent: jQuery("#view1").html()
    });
    
    app.addPage(oView);
    app.placeAt("uiArea");
    <script id="sap-ui-bootstrap"
        src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"
        data-sap-ui-theme="sap_bluecrystal"
        data-sap-ui-xx-bindingSyntax="complex"
        data-sap-ui-libs="sap.m"></script>
    
    <script id="view1" type="ui5/xmlview">
        <mvc:View 
          controllerName="view1.initial"
          xmlns:t="sap.ui.table"
          xmlns="sap.ui.commons"
          xmlns:mvc="sap.ui.core.mvc" >
            <t:TreeTable id="tbl" rows="{path:'/',parameters:{arrayNames:['data']}}" visibleRowCount="10">
                <t:columns>
                    <t:Column>
                        <t:label><Label text="name" /></t:label>
                        <t:template><TextView text="{name}" /></t:template>
                    </t:Column>
                    <t:Column>
                        <t:label><Label text="description" /></t:label>
                        <t:template><TextView text="{description}" /></t:template>
                    </t:Column>
                    <t:Column>
                        <t:label><Label text="" /></t:label>
                        <t:template><Button text="Add child node" press="addNode"/></t:template>
                    </t:Column>
                </t:columns>
            </t:TreeTable>
        </mvc:View>
    </script>
    
    <div id="uiArea"></div>

    【讨论】:

    • 我明白了。但是一旦你将一个项目添加到你的树中(通过拖放或其他方式),你的模型也会更新,所以你仍然可以遍历所有的 rowID 并展开它们,我想?
    • 是的,但这应该没什么区别。在TreeTable 中删除一个项目后,唯一应该发生的事情是您在模型中添加一个新条目。您无需添加任何控件或 HTML DOM 对象,因为您更新的模型会为您触发。因此,如果您放在树节点上(无论是关闭的还是打开的),您只需添加一个新条目作为相应模型条目的子节点。不应调用任何不良行为
    • 是的,但您犯的错误是您使用 jQuery 绑定 drop 事件,这需要您拥有 DOM ID(这是一种非常错误的方法)。我总是建议使用attachBrowserEvent 将其绑定到 UI5 控件(并且不是 DOM ID),这样您仍然可以使用我的方法进行绑定。见openui5.hana.ondemand.com/#docs/api/symbols/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多