首页 > 解决方案 > 如何在一个自定义控件中处理来自不同模型的两组

问题描述

目标:

我想使用预定义的搜索字段将两个模型(数据集)传递给自定义控件,稍后我可以在其中执行过滤。

我是 OpenUi5 的新手,所以我可能在这里做错和愚蠢的事情。我从一个简化的任务开始,将数据从前端传递到我的自定义控件并遇到麻烦。

简化想法的背景:

创建一个带有聚合的自定义控件foo,它的值将从视图中提供。还要创建另一个聚合元素 _searchField,它将使用视图提供的数据进行填充。每次用户在 _searchField 中输入时触发 onSuggestTerm。

自定义控制代码:

  function (Control) {

    var DropDownListInput = Control.extend('xx.control.DropDownListInput', {
      metadata: {
        defaultAggregation: 'foo',
        aggregations: {
          foo: { type: 'sap.m.SuggestionItem', multiple: true, singularName: 'suggestionItem' },
          _searchField: { type: 'sap.m.SearchField', multiple: false, visibility: 'hidden' }
        }
      }
    });

    DropDownListInput.prototype.init = function () {
      var that = this;

      this.onSuggestTerm = function (event) {
        var oSource = event.getSource();
        var oBinding = that.getAggregation('foo');

        oBinding.filter(new sap.ui.model.Filter({
          filters: new sap.ui.model.Filter('DISEASE_TERM', sap.ui.model.FilterOperator.Contains, ' Other')
        }));
        oBinding.attachEventOnce('dataReceived', function () {
          oSource.suggest();
        });
      };

      this.setAggregation('_searchField', new sap.m.SearchField({
        id: 'UNIQUEID1',
        enableSuggestions: true,
        suggestionItems: that.getAggregation('foo'),
        suggest: that.onSuggestTerm
      }));
    };

    return DropDownListInput;
  }, /* bExport= */true);

我没有在这里提供用于控制的渲染器功能,但它存在,这是其中最重要的摘录:

 oRM.write('<div');
      oRM.writeControlData(oControl);
      oRM.write('>');
      oRM.renderControl(oControl.getAggregation('_searchField'));
oRM.write('</div>');

从 xml 前端将数据传递给该控件:

<xx:DropDownListInput
  id="diseaseTermUNIQUE"
    foo='{path: db2>/RC_DISEASE_TERM/}'>
      <foo>
        <SuggestionItem text="{db2>DISEASE_TERM}"
           key="{db2>DISEASE_TERM}" />
      </foo>
</xx:DropDownListInput>

代码无法运行并出现此错误Cannot route to target: [object Object] -

我不知道这里出了什么问题..

标签: javascriptsapui5

解决方案


问题是您忘记在路径中提供单引号: foo="{path: 'db2>/RC_DISEASE_TERM/'}"


推荐阅读