简体   繁体   English

我的 select 元素没有填充 model 数据

[英]My select element doesn't get populated with model data

I'm writing a small OpenUI5 application.我正在编写一个小型 OpenUI5 应用程序。 I would like to attach a model (list with key/text values) to a sap.m.Select element (drop-down box).我想将 model(带有键/文本值的列表)附加到sap.m.Select元素(下拉框)。 However, regardless of whether I use static data for the model or fetch the data through ajax, the select element remains empty, no drop-down is displayed. However, regardless of whether I use static data for the model or fetch the data through ajax, the select element remains empty, no drop-down is displayed. I thought I had followed the examples/tutorials but I can't see what the problem is.我以为我已经按照示例/教程进行操作,但我看不出问题出在哪里。

index.html:索引.html:

<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Q Ui5 POC</title>
    <script id="sap-ui-bootstrap" src="resources/sap-ui-core.js"
      data-sap-ui-theme="sap_belize"
      data-sap-ui-libs="sap.m"
      data-sap-ui-compatVersion="edge"
      data-sap-ui-async="true"
      data-sap-ui-onInit="module:de/xx/qpoc/index"
      data-sap-ui-resourceroots='{ "de.xx.qpoc": "./" }'
    ></script>
  </head>
  <body id="content" class="sapUiBody"></body>
</html>

index.js: index.js:

sap.ui.define([
  "sap/ui/core/mvc/XMLView",
], function (XMLView) {
  "use strict";

  XMLView.create({
    id: "qpocMainView",
    viewName: "de.xx.qpoc.view.App",
  }).then(function(oView) {
    oView.placeAt("content");
  });
});

App.view.xml: App.view.xml:

<mvc:View controllerName="de.xx.qpoc.controller.App"
  xmlns="sap.m"
  xmlns:layout="sap.ui.layout"
  xmlns:core="sap.ui.core"
  xmlns:mvc="sap.ui.core.mvc"
  displayBlock="true">
  <layout:VerticalLayout>
    <Select
      enabled="true"
      editable="false"
      forceSelection="false"
      items="{
        path: '/clientList',
        sorter: { path: 'text' }
      }">
      <core:Item key="{key}" text="{text}" />
    </Select>
  </layout:VerticalLayout>
</mvc:View>

App.controller.js: App.controller.js:

sap.ui.define([
  "sap/ui/core/mvc/Controller",
  "sap/ui/model/json/JSONModel",
], function (Controller, JSONModel) {
  "use strict";

  return Controller.extend("de.xx.qpoc.controller.App", {
    onInit: function() {
      var viewData = {
        "clientList": [
          { key: 'key1', text: 'text1'},
          { key: 'key2', text: 'text2'},
        ]
      };
      var oModel = new JSONModel(viewData);
      this.getView ().setModel(oModel);
    }
  });
});

I have removed all non-relevant UI elements and application logic.我已经删除了所有不相关的 UI 元素和应用程序逻辑。 My understanding is that I do the binding in the onInit method of the controller.我的理解是我在onInit的 onInit 方法中进行绑定。 And that I can reference keys of the JSON structure I'm passing to the JSONModel constructor in the UI element ( /clientList ).我可以引用 JSON 结构的键,我在 UI 元素 ( /clientList ) 中传递给 JSONModel 构造函数。

The generated UI looks like this:生成的 UI 如下所示:

空选择元素

Remove those two lines in your XML view or set them to true (default value):在 XML 视图中删除这两行或将它们设置为true (默认值):

  • editable="false" → makes the control non-interactive editable="false" → 使控件非交互
  • forceSelection="false" → none of the options will be selected preliminarily forceSelection="false" → 不会初步选择任何选项

You do have data bound.你确实有数据绑定。 It's just the settings that prevented them from displaying.只是阻止它们显示的设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM