我想将来自控制器中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的任何值。
好的,我感谢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" />
希望这是正确的方法。