为什么按byId找不到控件?

时间:2018-10-09 07:25:05

标签: sapui5

我正在尝试通过sap.m.Input调用来获取byId控件,如下所示:

enter image description here

如您在上面看到的,我在打电话:

this.byId("CharInput-001-ZPM_TEST_01-ZPM_TEST_5")

使用正确的id,并得到未定义。我究竟做错了什么?

提示: id是在运行时自动生成的,这意味着我单击一个按钮,sap.m.Input字段将自动生成,并将其放置在 SimpleForm 控件中。

enter image description here

通过 id 调用SimpleForm,可以找到该控件:

enter image description here

片段:

sap.ui.define([
    "sap/m/Dialog",
    "sap/m/Text",
    "sap/m/Button"
], function (Dialog, Text, Button) {
    "use strict";

    const fnNumList = () => {
        return new sap.m.ObjectListItem({
            number: "{ValueHelp>NumValueFrom}",
            numberUnit: "{ValueHelp>UnitOfMeasurement}"
        });
    };

    const fnCharList = () => {
        return new sap.m.StandardListItem({
            description: "{ValueHelp>CharValue}"
        });
    };

    const fnCurrList = () => {
        return new sap.m.ObjectListItem({
            number: "{ValueHelp>CurrValueFrom}",
            numberUnit: "{ValueHelp>CurrCurrencyFrom}"
        });
    };

    const fnDateList = () => {

        const oDatePicker = new sap.m.DatePicker({
            dateValue: "{ValueHelp>DateValueFrom}",
            valueFormat: "dd-MM-yyyy",
            displayFormat: "dd-MM-yyyy",
            editable: false
        });

        return new sap.m.CustomListItem({
            content: oDatePicker
        });
    };

    const fnTimeList = () => {
        const oTimePicker = new sap.m.TimePicker({
            valueFormat: "HH:mm:ss",
            displayFormat: "HH:mm:ss",
            support2400: true,
            dateValue: "{ValueHelp>TimeValueFrom}",
            editable: false
        });

        return new sap.m.CustomListItem({
            content: oTimePicker
        });
    };

    const fnDetermineListType = (oValueHelp) => {
        switch (oValueHelp.DataType) {
        case "NUM":
            return fnNumList();
        case "DATE":
            return fnDateList();
        case "TIME":
            return fnTimeList;
        case "CURR":
            return fnCurrList();
        default:
            return fnCharList();
        }
    };

    const fnCreateList = (oValueHelp) => {
        const oNewList = new sap.m.List({
            includeItemInSelection: true,
            mode: sap.m.ListMode.SingleSelect
        });

        oNewList.bindItems({
            path: "ValueHelp>/",
            template: fnDetermineListType(oValueHelp)
        });

        return oNewList;
    };

    const fnDestroyDialog = (oDialog) => {
        oDialog.close();
        oDialog.destroy();
    };

    return {

        showSingle: function (aCharsHelpValues, fnGetSelectedObject, sValueHelpField) {
            const oList = fnCreateList(aCharsHelpValues[0]);
            const oDialog = new Dialog({
                title: aCharsHelpValues[0].CharValue,
                content: oList,
                beginButton: new sap.m.Button({
                    text: "OK",
                    press: function () {
                        fnGetSelectedObject(oList.getSelectedItem().getBindingContext("ValueHelp").getObject(), sValueHelpField);
                        fnDestroyDialog(oDialog);
                    }
                }),
                endButton: new sap.m.Button({
                    text: "Close",
                    press: function () {
                        fnDestroyDialog(oDialog);
                    }
                })
            });

            return oDialog;
        }
    };

});

更新 输入是通过以下方式生成的:

    _buildCharacInputId: function (oData) {
        return "CharInput:" + oData.Classtype + ":" + oData.Classnum + ":" + oData.NameChar;
    },

_determineCtrlForChars: function (oData) {
            const sId = this._buildCharacInputId(oData);
            const self = this;
            switch (oData.DataType) {
            case "CHAR":
                return new sap.m.Input(sId, {
                    maxLength: oData.NumberDigits,
                    type: sap.m.InputType.Text,
                    showValueHelp: oData.WithValues,
                    valueHelpRequest: self._onCharValueHelp.bind(self)
                });
            case "CURR":
                return new sap.m.MaskInput(sId, {
                    mask: oData.Template.replace(/_/g, "9") + (oData.Currency ? " ".concat(oData.Currency) : "")
                });
            case "DATE":
                return new sap.m.DatePicker(sId, {
                    valueFormat: "dd-MM-yyyy",
                    displayFormat: "dd-MM-yyyy"
                });
            case "NUM":
                return new sap.m.MaskInput(sId, {
                    mask: oData.Template.replace(/_/g, "9") + (oData.UnitTExt ? " ".concat(oData.UnitTExt) : "")
                });
            case "TIME":
                return new sap.m.TimePicker(sId, {
                    valueFormat: "HH:mm:ss",
                    displayFormat: "HH:mm:ss",
                    support2400: true
                });
            default:
                throw "Not supported type. Please contact app developer.";
            }
        },

1 个答案:

答案 0 :(得分:1)

该ID不会在View范围内生成,因此它将没有正确的DOM ID。

控制器中的以下代码创建一个CharInput-001-ZPM_TEST_01-ZPM_TEST_5之类的ID:

_buildCharacInputId: function (oData) {
    return "CharInput:" + oData.Classtype + ":" + oData.Classnum + ":" + oData.NameChar;
},
//...
const sId = this._buildCharacInputId(oData);

使用createId()将为视图添加前缀,因此ID为__xmlview1--CharInput-001-ZPM_TEST_01-ZPM_TEST_5

//...
const sId = this.createId(this._buildCharacInputId(oData));