sap.m.MultiComboBox: 如何从所有选中的项目中获取文本?

sap.m.MultiComboBox: How to get texts from all selected items?

selectionFinish之后,我想从列表中获取所有选中的项目文本。获取所有选定项目的通常方法是这样的:

this.byId("idExampleMultiComboBox").getSelectedItems();

问题: 它 returns 类型 sap.ui.core.Item 的对象与所有 aggregations/associations。我只需要选中选项的文字。

多组合框:

<MultiComboBox id="idExampleMultiComboBox"
  items="{modelExample>/}"
  selectionFinish=".onSelectionFinish">
  <core:ListItem text="{modelExample>Option}" />
</MultiComboBox>
let modelExample = {
  0: { "Option": "ExampleOption1" },
  1: { "Option": "ExampleOption2" }
};

一种方法:

onSelectionFinish: function(oEvent){
  let aSelectedCriteria = [];
  let i = 0;
  while (i < oEvent.getSource().getSelectedItems().length) {
    aSelectedCriteria.push(oEvent.getSource().getSelectedItems()[i].getText())
    i = i + 1;
  }
},

问题:如何从列表中获取选定的项目作为文本?

我的建议:使用Array.prototype.map方法创建一个包含所有文本的数组。这只是你的方法更方便的方法。

onSelectionFinish: function(oEvent) {
  const aSelectedItems = oEvent.getParameter("selectedItems");
  const aSelectedTexts = aSelectedItems.map(oItem => oItem.getText());
},

另一种方法是只获取选定的键,然后循环遍历您的模型。

onSelectionFinish: function(oEvent) {
  // There are multiple ways to retrieve your model
  const oModel = oEvent.getSource().getBinding("items").getModel();
  const aSelectedKeys = oEvent.getSource().getSelectedKeys();
  const aSelectedCriteria = aSelectedKeys.map(sKey => oModel.getProperty("/" + sKey);
},