UI5 oData和两个依赖选择

时间:2014-02-05 18:22:04

标签: sapui5

我正在尝试使用SAPUI5和XML创建两个动态选择列表作为开发视图的范例。

以下是我的oData模型示例:

  1. / EmployeeSet - 返回两个实体:
    • Pernr ='00000001'
    • Pernr ='00000002'
  2. / EmployeeSet('00000001') - 返回一个实体:
    • Pernr ='00000001'Name ='Jacob'
  3. / EmployeeSet('00000002') - 返回一个实体:
    • Pernr ='00000002'Name ='Matthew'
  4. / EmployeeSet('00000001')/ KidsListSet - 返回两个实体:
    • PernrKid ='00000010'Name ='Sophia'
    • PernrKid ='00000011'Name ='James'。
  5. / EmployeeSet('00000002')/ KidsListSet - 返回三个实体
    • PernrKid ='00000020'Name ='Alexander'
    • PernrKid ='00000021'Name ='Zoe'
    • PernrKid ='00000022'Name ='Oliver'
  6. Select1按预期工作,默认值为,点击操作我可以看到两个值。

    两个选择:

    <Select id="select1" width="100%" items="{ path: '/EmployeeSet' } change="handleSelect1">
    <core:Item key="{Pernr}" text="{Name}" />
    </Select>
    
    <Select id="select2" width="100%" items="{ path: '/KidsListSet' }>
    <core:Item key="{PernrKid}" text="{Name}" />
    </Select>
    

    handleAssigment函数的代码:

    handleAssigments : function(evt){
      var context = evt.getParameter("selectedItem").getBindingContext();
      var Path = context.getPath() + '/KidsListSet';
      var select2 = this.getView().byId("select2");
    
    return select2.bindAggregation("items", Path, new sap.ui.core.Item({ key : "{PernrKid}", text : "{Name}" }));
    },
    

    当我更改select1中的值时,select2会更新,我会看到两个或三个条目(基于第一个选择)

    但是我几乎没有问题:

    1. 如何预先填充select2的默认值(例如PernrKid ='00000010'Name ='Sophia')

    2. 也许可以在视图控制器中不创建新的选择项的情况下进行数据绑定?

    3. 谢谢!

      更新时间:06/02/2014

      我如何设置oData模型:

       // create root view
              var oView = sap.ui.view({
                  id: "app",
                  viewName: "view.App",
                  type: "JS",
                  viewData: { component : this }
              });
      
      
              // Load oData Model
              var sServiceUrl = "/sap/opu/odata/sap/YKIDS/";
              var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true, "", "");
      
              oView.setModel(oModel);
      

      ...

1 个答案:

答案 0 :(得分:1)

如果没有选择显示占位符或在选择中显示空白,显示实际值可能误导用户认为它们已被设置会不会更好?

或者,我不确定如何在视图中执行此操作,但您可以侦听select1的绑定onInit控制器并从select1的绑定的第一行更新select2

onInit: function(){    
var oSelect1 = this.getView().byId("select1");
var oBinding = oSelect1.getBinding("items");

var handler = function(oEvent) {

    //release the handler
    oBinding.detachDataReceived(handler);

    //get the first row of select1
    var aContexts = oEvent.oSource.getContexts();
    var row1 = aContexts[0];

    //get path to first rows kid list
    var Path = row1.getPath() + '/KidsListSet';

    //put values for first rows path into model either via select2 or the model
    eg  oModel.bindList(path)
    or select2.bindAggregation("items", Path ....

};
... set up model
var oModel = new ....

//attach handler
oBinding.attachDataReceived(handler);