Connect chosen options from Multicombobox in personalized UI5 control

I am looking to connect selectedItems, which are sourced from JsonModel in the controller, to a custom control.

Custom control

sap.ui.define([
  'sap/ui/core/Control',
  'sap/m/MultiComboBox',
  ], function (Control, MultiComboBox) {
  return Control.extend('drex.control.TokenizedMultiComboBox', {
    metadata: {
      properties: {
        selectedKeys: { type: 'string[]', defaultValue: [] }
      },
      aggregations: {
        combo: { type: 'sap.m.MultiComboBox', multiple: false },
      },
    },

    init: function (allItems) {
      Control.prototype.init.apply(this, arguments);
    },

    onAfterRendering: function() {
      const combo = this.getAggregation('combo');
      const selectedKeys = this.getSelectedKeys();
      if (selectedKeys.length) {
        combo.setSelectedKeys([selectedKeys]);
      }
    },

    renderer: function (rm, oControl) {
      rm.write('<div');
      rm.writeControlData(oControl);
      rm.write('>');
      rm.write('<div>');
      rm.renderControl(oControl.getAggregation('combo'));
      rm.write('</div>');
      rm.write('</div>');
    },
  })
})

XML

<drex:TokenizedMultiComboBox
    selectedKeys="{selectedItems>/disease}" />

where selectedItems>/disease is declared in the controller:

this.getView().setModel(new JSONModel(), 'selectedItems');

The issue lies in the fact that the Multicombobox within the custom control does not display any values from selectedItems.

Answer №1

I managed to find the solution with the help of this post about setting selected items in a sapui5 MultiComboBox

...
metadata: {
      properties: {
        selectedKeys: { type: 'string', defaultValue: '' }
      },
      aggregations: {
        combo: { type: 'sap.m.MultiComboBox', multiple: false },
      },
    },

onAfterRendering: function() {
      const combo = this.getAggregation('combo');
      const selectedKeys = this.getSelectedKeys();
      combo.bindProperty('selectedKeys', selectedKeys);
    }
...

Instead of passing the model path as an object, I am now using it as a string in XML.

<drex:TokenizedMultiComboBox
    selectedKeys="selectedItems>/disease" />

I believe this is the correct approach to take.

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

React blank state - State remains undefined after calling setState

I took out the imports because they are not causing any issues When I render, I set my state and it logs correctly in the console. However, when I try to map it, it comes back as null and gives me an error stating that there are no elements in the "allInf ...

Altering the input type of a cloned element in Internet Explorer results in the loss of the value

When I have checkbox inputs displayed in a modal with preset value attributes, upon clicking "OK", I clone them and change their input types to hidden, then append them to a div in the document body. However, when trying to retrieve their values using jQue ...

Alternative method for handling web requests in case JavaScript is not enabled

Issue: i) I am developing a JSF2 application and need to implement a tab control on a page. When a user clicks on a tab, the content for the panel below should be loaded from an xhtml file on the server using an ajax call. ii) I want this functionality ...

Moving objects with Three.JS

Seems like I might just be overlooking a simple solution here. I have a setup with a spotlight pointing backward from the camera towards a plane geometry, as well as some randomly scattered boxes. My goal is to create a basic top-down demo. I move the came ...

Mistake in maintaining hydration with styled-components and React Context

I encountered an issue where I have two theme variants in my app - dark and light. You can view the sandbox example here ThemeContext.ts export const ThemeContext = createContext<{ theme: AppThemeInterface, setTheme: Dispatch<SetStateAction< ...

How to utilize a prepared statement in MySQL using NodeJS for inserting or updating data in a specific table?

Below is the code snippet I need assistance with: connection.query({ sql: 'CREATE TABLE ? ( `wage` FLOAT NOT NULL , `monday` FLOAT NOT NULL , `tuesday` FLOAT NOT NULL , `wednesday` FLOAT NOT NULL , `thursday` FLOAT NOT NULL , `friday`) ENGINE = InnoDB ...

Iterating through an array of objects within a Vuejs template

Consider the JSON data below: { "games": [ { "id": 1, "init_date": "2020-02-11T07:47:33.627+0000", "players_in_game": [ { "id": 1, "player": { "id": 1, "player": "Jack Bauer", ...

Implementing user-driven filtering in a React table

When a user clicks on the submit button, data will be loaded. If no filter is applied, all data will be displayed. const submit = async (e: SyntheticEvent) => { e.preventDefault(); const param = { ...(certificateNo && ...

Variations in key-codes between Android Nexus and Samsung mobile devices

https://i.sstatic.net/42gV2.png As I work on developing a "Pin Code Entry" functionality for an Ionic app, I encounter challenges with different key-codes on various Android devices. The goal is to create a login feature where users input digits into 5 fi ...

Arranging hierarchical data in JavaScript

After populating the hierarchy data, it is structured as follows: Here is a screenshot: I am looking to sort this data. Currently, I have a flat data object for rendering purposes. For example, my rendering object looks like this: renderedobjects=[ {1,. ...

The declaration of the 'type' in NestJS goes unused, according to ts(6133)

I have a code snippet where the keyword 'type' is highlighted in red and triggering an error message: 'type' is declared but its value is never read.ts(6133) The snippet of my code looks like this: @ManyToMany(type => RoleEntity, ro ...

Tips for addressing the error message "Cannot PUT /":

I have been working on updating a local database using MongoDB for my project. Here is the code snippet I am using to update the data. The second part involves editing that redirects the updated data. I am not encountering any errors, so I am unable to i ...

Is it possible to specify a parameter when passing a callback function?

As part of my web application, I am passing a callback function as a parameter within different functions. In certain cases, I need the callback to behave differently. Is it possible to pass a parameter to this callback? someMethod(callback); otherMethod ...

Is it possible to trigger the alert message by utilizing this code snippet?

Is it possible to display a message using this function? var start_database = function() { alert('hello'); }; window.setTimeout(start_database, 1000); ...

The value of req.session.returnTo is not defined

I have implemented passport for user authentication using discord oauth2. I want the users to be redirected back to the original page they came from instead of being directed to the home page or a dashboard. Even though I tried saving the URL in the sessi ...

Utilize the Feefo JSON data and convert it to a JavaScript array for rendering as an HTML

Currently diving into the world of Javascript and JSON, but feeling a bit overwhelmed by it all. Attempting to pull data from a JSON array, I've managed to reach this stage $(function(){ var $reviews = $('#reviews'); $.ajax({ ...

Troubleshooting issues with JavaScript progress bar functionality

I have implemented a progress bar using HTML5, JavaScript and Ajax to showcase file uploads in PHP. The issue I am facing is that the progress bar is not displaying the progress correctly. In addition to that, the echo statements in the PHP code are no ...

Tips for avoiding HTML injections in HTML tooltips

I'm attempting to create a tooltip using HTML, but I need to escape specific HTML elements within it. So far, my attempts have been unsuccessful. Take a look at the example I've provided: http://jsfiddle.net/wrsantos/q3o1e4ut/1/ In this example ...

Best Practices for Converting TypeScript to JavaScript

What is the recommended approach to converting this JavaScript code into TypeScript? JAVASCRIPT: function MyClass() { var self = this, var1 = "abc", var2 = "xyz"; // Public self.method1 = function () { return "somethin ...

Discover the process of creating a dynamic mailto link using AJAX and HTML

One of my tasks involves extracting email addresses from an XML document using AJAX, and then displaying them on a webpage as clickable links for sending emails. Here is a snippet of JavaScript code I am working with: emailobj = listings[i].getElementsBy ...