How do I insert a new item into the tree structure using Sapui5 CustomTreeItem?

Having an issue with the CustomTreeItem in SAPUI5. I can successfully display the tree structure from JSON, but struggling to manually add data on click of the Add button.

https://i.sstatic.net/BpLOK.png

In the image above, when I click the + Button, I need to add a Tile below it. However, I am unable to do so and there are no error messages in the console either...

This is my XML code:

<Tree items="{selModel>/}" id="Tree" class="sapUiTinyMarginTop tilesClass selTree">
    <CustomTreeItem id="treeItem" detailPress="pressCustomTreeItem">
        <VBox class="customSelTile" id="customSelTile">
            <HBox>
                <VBox class="sapUiTinyMarginTop customTxts">
                    <Label class="sapUiSmallMarginBegin headTxtFont" text="{selModel>text}"/>
                    <Label class="sapUiSmallMarginBegin subHeadTxtFont" text="{selModel>subheader}"/>
                </VBox>
                <Button class="sapUiSmallMarginBegin sapUiTinyMarginTop" press="addTile" icon="sap-icon://add"/>
                <Button class="sapUiTinyMarginBegin sapUiTinyMarginTop" press="removeTile" icon="sap-icon://less"/>
            </HBox>
        </VBox>
    </CustomTreeItem>
</Tree>

Controller:

var selOrgJson = [{
    "text": "Department",
    "subheader": "Production Management JP",
    "personName":"Kuwahara Atushi",
    "nodes": [{
        "text": "Department",
        "subheader": "Production JP",
        "personName":"Kawasaki Takeshi"
    }, {
        "text": "Department",
        "subheader": "Planning Scheduling JP",
        "personName":"Fukuyama Sumire",
        "nodes": [{
            "text": "Department",
            "subheader": "Planning Operations",
            "personName":"Sakata Junko"
        }]
    }, {
        "text": "Department",
        "subheader": "Sales Office, North East Japan",
        "personName":"Emoto Hloronori"
    }, {
        "text": "Department",
        "subheader": "Sales Office, South East Japan",
        "personName":"Suzuki Aya"
    }, {
        "text": "Department",
        "subheader": "Partner Sales JP",
        "personName":"Kuwahara Atushi"
    }]
}];

var selModel = new sap.ui.model.json.JSONModel();
selModel.setData(selOrgJson);
this.getView().setModel(selModel, "selModel");

addTile: function (oEvt) {
    var vBox = new sap.m.VBox();
    vBox.addStyleClass("customSelTile");
    var hBox = new sap.m.HBox();
    var insideVBox = new sap.m.VBox();
    insideVBox.addStyleClass("sapUiTinyMarginTop customTxts");
    var headInput = new sap.m.Input({
        type: "Text"
    });
    var subInput = new sap.m.Input({
        type: "Text"
    });

    insideVBox.addItem(headInput);
    insideVBox.addItem(subInput);

    hBox.addItem(insideVBox);
    vBox.addItem(hBox);

    var button1 = new sap.m.Button({
        press: this.addTile,
        icon: "sap-icon://add"
    });

    var button2 = new sap.m.Button({
        press: this.removeTile,
        icon: "sap-icon://less"
    });

    hBox.addItem(button1);
    hBox.addItem(button2);
    vBox.addItem(hBox);

},

Can someone please assist me with adding a tile to the CustomTreeItem on click of the Add button? If my current approach in the controller is incorrect, please provide an alternative solution. Thank you in advance.

Answer №1

Solved the issue with the following code snippet:

addTile: function (oEvt) {

        var _oItem = oEvt.getSource().getParent().getParent().getParent();
        var _sBindingPath = _oItem.getBindingContextPath();
        var _oModel = this.getView().getModel("selModel");
        var aData = _oModel.getProperty(_sBindingPath);
        var _oChildNodes = [];
        if (aData.nodes !== undefined) {
            _oChildNodes = aData.nodes;
        } else {
            aData.nodes = _oChildNodes;
        }

        var newData = {
            "text": "",
            "subheader": ""
       }

Appreciate it!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Javascript to Change Background Color on Button Click

The button press does not result in a change of the background color. What could be causing this issue? var body = document.getElementsByTagName("body"); var bgbutton = doucument.getElementById("bgchange"); bgbutton.onclick = function() { body.style.b ...

The Issue of Horizontal Scrolling - None of the elements are out of

I am facing an issue with an unwanted horizontal scroll on my website and I can't seem to identify the cause. The header of my site is quite simple, utilizing display: flex, justify-content: center, marin-top: 5vh, along with some padding on the logo ...

Incorporate Add to Homescreen functionality into your Angular 4 Project using JavaScript

Currently I am in the beginning stages of learning Angular development. One of my assignments involves integrating the add to homescreen jQuery plugin into one of my pages. This is my attempt at implementing it in my home.component.ts file: ngAfterViewIn ...

Leveraging Javascript Modules within a Typescript Vue Application

The issue at hand I've encountered a problem while attempting to integrate https://github.com/moonwave99/fretboard.js into my Vue project. My initial approach involved importing the module into a component as shown below: <template> <div&g ...

Discovering the magic of activating a JavaScript function on jQuery hover

I need to call a JavaScript function when hovering over an li element. var Divhtml='<div>hover</div>'; $('li a').hover(function(){ $(this).html(Divhtml); //I want to trigger hovercall(); wh ...

There seems to be an issue with the AJAX REST call failing to transmit data

Whenever I attempt to submit the form, the page refreshes and nothing gets saved in the database. The code in subforum.js $(document).on('click','#submit',function(e) { var user = JSON.parse(sessionStorage.getItem("ulogovan")); consol ...

How can jQuery verify if an input value is contained within an array?

I've been attempting to verify if the value of an input field is part of an array, but for some reason it's not working. Here is my approach: var postcode1 = []; for (var i = 21000; i < 21999; i++) { postcode1.push({ val: i, text ...

The use of event.returnValue in jQuery has been marked as deprecated

Every time I create a webpage using jQuery version , I encounter a warning message in the Console of Google Chrome. Is there a specific piece of code I can use to avoid this warning? Note that I am currently learning jQuery. The warning reads: event.retu ...

Pass on only the necessary attributes to the component

I have a simple component that I want to include most, if not all, of the default HTML element props. My idea was to possibly extend React.HTMLAttributes<HTMLElement> and then spread them in the component's attributes. However, the props' ...

Confirming if the value is an array using jQuery

Currently, I have a list of elements with various types associated with them. My goal is to identify specific text within these types and hide only those types that do not contain the desired text, leaving the rest unaffected. The structure looks like thi ...

How can I display a particular section of my JSON data in an AngularJS application?

Below is an example of a JSON structure: {"years":[ { "year_title":"94", "months":[...] } { "year_title":"95", "months":[...] } { "year_title":"96", "months":[...] } ]} I was able to display the data using the code sni ...

Transform or retrieve information from input into unformatted text

I'm working on a JavaScript function that can convert input values to text. However, I am facing an issue as I only want to convert data from one specific row into plain text. Each row in my table has 5 cells along with a button for saving the data. I ...

JQuery Ajax: The loaded content flickers into view, revealing old content momentarily

Having an issue with my ajax code. I am working on a project that involves some links and a content area. The idea is that when a user clicks on a link, the content area should be hidden, load new data, and then show the updated content. However, I have no ...

A step-by-step guide to creating a clipboard stream using guacamole-common.js

When utilizing Guacamole.client.createClipboardStream to generate a clipboard stream and sending the stream on paste event, I noticed that the remote clipboard remains empty. const customStream = client.createClipboardStream("text/plain"); cust ...

Tips for overlaying text onto a canvas background image

I'm curious about how to overlay text on top of an image that is within a canvas element. The image is a crucial part of my game (Breakout), so it must remain in the canvas. I've tried adding text, but it always ends up behind the image, which is ...

What is the method for obtaining multiple indices on an Array?

Attempting to find multiple index positions in an Array for a Boolean value. Experimenting with while and for loops to iterate through more than one index position without success so far. Below is the code snippet: let jo = [1,2,3,4,5] let ji = [1,2,3] ...

Struggling to locate images in Three.js

I'm facing a challenge with my Vue Cli app where I am attempting to use Three.js for rendering 3D objects within it. The issue arises when I attempt to load an image to utilize as a texture. let scene = new Three.Scene(); const spaceTexture = new Th ...

Leveraging ajax for showcasing page content in a floating div container

I am in need of creating a button on my page that, when clicked, will display a floating div above the page and load another internal page. I believe this can be achieved using Ajax, but I have no experience with it and don't know where to start. Her ...

Tooltip positioned within a custom container using Material UI

Currently, as part of my Chrome extension development utilizing React and Material UI, I am implementing an inject page strategy. I have managed to set up a tooltip for the Fab button, but unfortunately, it appears outside the DOM of my extension. Upon ins ...

Having trouble utilizing the data obtained from an API, encountering errors such as 'data is undefined' leading to nothing displaying on the screen, or 'data.map() is undefined'

I'm relatively new to working with APIs. Although I have successfully made a post, I am creating a new one to clearly outline my issue. My goal is to retrieve posts from the dummyapi. Below is the code snippet that I am using: import React from &a ...