Search code examples
expandsapui5treetable

How to make SAPUI5 TreeTable expanded by default?


I use following code (in controller):

onAfterRendering: function () {
    var t, i;
    if (!this.initExpand) {
        try {
            t = this.getView().getContent()[1];
            for (i=0; i<t.getRows().length; i++) {
                t.expand(i);
            }
            this.initExpand = true;
        } catch (e) {
            console.log("expand failed: "+e.message);
        }
    }
},

for initial (after loading and rendering) expanding all nodes of following sap.ui.table.TreeTable (in view):

<mvc:View
    displayBlock="true" 
    xmlns:l="sap.ui.layout"
    xmlns:table="sap.ui.table"
    xmlns:mvc="sap.ui.core.mvc"
    controllerName="sap.app.controller.scr1"
>
    <l:HorizontalLayout>
        <SearchField liveChange="onSearch"/>
    </l:HorizontalLayout>
    <table:TreeTable rows="{/root}"
        selectionMode="None"
        visibleRowCount="11"
        showColumnVisibilityMenu="true"
        enableCellFilter="true"
        enableColumnReordering="false"
        class="sapUiSizeCompact"
        expandFirstLevel="true"
    >
        <table:columns>
        ...
        </table:columns>
    </table:TreeTable>
</mvc:View>

But sometimes nodes still are not expanded (remains collapsed).

Is there some other (more stable) way for initial expanding all nodes of sap.ui.table.TreeTable?


Solution

  • The below code works:

    onAfterRendering: function(){
    
       var oTreeTable = this.getView().byId("myTreeTableId");
       oTreeTable.expandToLevel(3); //number of the levels of the tree table.
    
    }
    

    Where '3' is the number of the levels for your tree table and,

    myTreeTableId: is the Id of your control at XML view:

    <table:TreeTable id="myTreeTableId" ...