在自定义控件UI5中绑定Multicombobox的selectedItems

问题描述 投票:0回答:1

我想将来自控制器中JsonModel的selectedItems绑定到自定义控件。

自定义控件

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}" />

其中selectedItems>/disease在控制器中定义:

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

问题是此自定义控件中的Multicombobox不包含selectedItems的任何值。

javascript sap sapui5
1个回答
0
投票

好的,我感谢How can I set 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', this.getSelectedKeys());
    }
...

我现在将模型路径作为字符串传递。XML

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

希望这是正确的方法。

© www.soinside.com 2019 - 2024. All rights reserved.