如何将模型json绑定到控制器中的comboBox



我想在控制器内(而不是在xml视图中(绑定一个模型json和一个comboBox。我制作了一个名为types.Json的Json模型,并将其传递给控制器,但当我将创建的模型中的值绑定到组合框时,并没有显示任何内容。

你能帮帮我吗?我在这里找不到问题。

下面是我代码的一部分,我认为谁对这个问题很重要。

  • 我的模型JSON-名称:types.JSON

    "types": [
    {
    "text": "Férias",
    "key": "01"
    },
    {
    "text": "something",
    "key": "02"
    },
    ]
    
  • 我的控制器-名称:Page.CONTROLLER.js

    handleAppointmentCreate:函数(oEvent({var oStartDate=oEvent.getParameter("startDate"(,oEndDate=oEvent.getParameter("endDate"(,oCalendarRow=oEvent.getParameter("calendarRow"(,oEmpId=oCalendarRow.getKey((,

    _oYearStartDate = oStartDate.getFullYear(),
    _oMonthStartDate = oStartDate.getMonth() + 1,
    _oDateStartDate = oStartDate.getDate(),
    _oYearEndDate = oEndDate.getFullYear(),
    _oMonthEndDate = oEndDate.getMonth() + 1,
    _oDateEndDate = oEndDate.getDate(),
    _HourStart = oStartDate.getHours(),
    _oMinStart = oStartDate.getMinutes(),
    _oSecStart = oStartDate.getSeconds(),
    _oHourEnd = oEndDate.getHours(),
    _oMinEnd = oEndDate.getMinutes(),
    _oSecEnd = oEndDate.getSeconds(),
    
    sStartDate = _oYearStartDate + "-" + _oMonthStartDate + "-" + _oDateStartDate,
    sEndDate = _oYearEndDate + "-" + _oMonthEndDate + "-" + _oDateEndDate,
    sStartHour = _HourStart + ":" + _oMinStart + ":" + _oSecStart,
    sEndHour = _oHourEnd + ":" + _oMinEnd + ":" + _oSecEnd,
    sIdEmp = oEmpId;
    var dataModel = this.getOwnerComponent().getModel("Model"); 
    this.getView().setModel(dataModel, "DataModel"); 
    if (!this.oConfirmDialog) {
    this.oConfirmDialog = new Dialog({
    type: DialogType.Message,
    title: "Novo agendamento",
    content: [
    new HorizontalLayout({
    content: [
    new VerticalLayout({
    width: "120px",
    content: [
    new Text({ text: "Id de funcionário: " }),
    new Text({ text: "Data de inicio: " }),
    new Text({ text: "Data de término: " }),
    new Text({ text: "Hora de inicio: " }),
    new Text({ text: "Hora de término: " })
    ]
    }),
    new VerticalLayout({
    content: [
    new Text({text: sIdEmp }),
    new Text({ text: sStartDate }),
    new Text({ text: sEndDate }),
    new Text({ text: sStartHour }),
    new Text({ text: sEndHour })
    ]
    })
    ]
    }),
    new TextArea("confirmationTitle", {
    width: "100%",
    placeholder: "Adicione o titulo do agendamento"
    //required:"true" - não pode ter
    }),
    new TextArea("confirmationDetails", {
    width: "100%",
    placeholder: "Adicione detalhes do agendamento"
    //required:"true" - não pode ter
    }),
    new sap.m.ComboBox({
    items: {
    path: "DataModel>/types",   
    template: {
    Type: "sap.ui.core.ListItem",
    text: "{DataModel>Key}",
    enabled: true
    }
    }
    })
    ],
    beginButton: new Button({
    type: ButtonType.Emphasized,
    text: "Submeter",
    press: function () {
    var sIdEmp1 = sIdEmp,
    sStartDate1 = sStartDate,
    sEndDate1 = sEndDate,
    sStartHour1 = sStartHour,
    sEndHour1 = sEndHour,
    sTitle = Core.byId("confirmationTitle").getValue(),
    sDetails = Core.byId("confirmationDetails").getValue();
    this.addAppointment(sIdEmp1, sStartDate1, sEndDate1, sStartHour1, sEndHour1, sTitle, sDetails);
    this.oConfirmDialog.close();
    }.bind(this)
    }),
    endButton: new Button({
    text: "Cancelar",
    press: function () {
    this.oConfirmDialog.close();
    }.bind(this)
    })
    });
    }
    this.oConfirmDialog.open();
    },
    

首先,有一个拼写错误。在你的模型中,你有一个字符串";键";,但是当创建组合框时;Key";,用大写字母K。请确保在两个位置都写相同的。

如果这不能解决您的问题,请尝试使用sap.ui.core.Item而不是sap.ui.core.ListItem,因为这是聚合接受的控件:

new sap.m.ComboBox({
items: {
path: "DataModel>/types",
template: new sap.ui.core.Item({
key: "{DataModel>key}",
text: "{DataModel>text}"
})
},
enabled: true
})

此外,请记住,当您将得到13到200个结果时,只建议使用ComboBox。如果要显示的选项少于13个,请改用sap.m.Select

最新更新