Search code examples
javascriptsapui5

Remove item from flexbox by key UI5


There is a nice function to insertItem with the key into Flexbox in UI5.

https://sapui5.hana.ondemand.com/#/api/sap.m.FlexBox%23methods/insertItem

I couldn't find any function to remove item from the flexbox by key though. In fact the documentation for removing item is not very descriptive. How can I delete an item from the Flexbox?

I'm inserting Tokens to Flexbox.

const hbox = new sap.m.HBox();
const key = 1;
hbox.insertItem(new sap.m.Token({
    text: text
}), key);

Solution

  • it is not key, but index of which the position where item is going to be inserted at. here is the code where we insert one item at index 0 and then remove it.

    sap.ui.define([
      'sap/m/Token',
      'sap/m/HBox',
      'sap/m/Text'
    ], function (Token, HBox) {
      const hbox = new HBox();
      hbox.insertItem(new Token({
        text: "text"
      }, 0));
      hbox.placeAt("content")
    
      const found = hbox.getItems()[0]
      hbox.removeItem(found)
    
    });
    

    I would like you to consider using proper MVC and not having to manipulate items in FlexBox directly.

    Thanks