如何从SAPUI5片段获取Control byId

时间:2017-11-16 07:28:24

标签: javascript xml dialog sapui5

我有一个片段中的对话框。因此,在输入详细信息并单击提交按钮后,填充到输入中的文本应显示为MessageToast。但是我收到了错误:'无法读取属性' getValue'未定义'。

以下是代码:

    onAddMovie: function() {
        var view = this.getView();
        var createDialog = view.byId("CreateDialog");
        var oDummyController = {
            // This is when I clicked the Submit button in dialog
            submitDialog: function() {
            var user = sap.ui.core.Fragment.byId("createDialog", "movie_name").getValue();
                MessageToast.show(user);
                createDialog.close();
            },
            closeDialog: function() {
                createDialog.close();
            }
        };

        if (!createDialog) {
            createDialog = sap.ui.xmlfragment(view.getId(), "Admin.view.Dialog", oDummyController);
        }
        view.addDependent(createDialog);
        createDialog.open();
        if (!createDialog.isOpen()) {
            //do sth
        }
    },

上面是显示对话框的功能,按下提交按钮后,输入中的文本应显示在MessageToast中。

XML:

        <core:FragmentDefinition  xmlns="sap.m" xmlns:core="sap.ui.core" xmlns:l="sap.ui.layout">

        <Dialog id="createDialog" title="Input Movie Details" width="100%" class="sapuiMediumMargin" confirm="handleClose" close="handleClose">
            <l:VerticalLayout class="sapUiContentPadding" width="100%">
            <l:content>
                <Input width="100%" placeholder="Movie Name" id="movie_name"/>
                <HBox alignItems="Center" renderType="Bare">
                    <Label text="Year of Release" width="50%"/>
                    <ActionSelect selectedItem="Element sap.ui.core.ListItem#__item0" selectedKey="item1" class="sapUiLargeMarginBegin" selectedItemId="__item0" id="__select0" width="50%">
                        <items>
                            <core:ListItem text="2017" key="item1" id="__item0"/>
                            <core:ListItem text="2016" key="item2" id="__item1"/>
                            <core:ListItem text="2015" key="item3" id="__item2"/></items>
                    </ActionSelect>
                </HBox>
                <HBox alignItems="Center" renderType="Bare">
                    <Label text="Date of Screening" width="50%"/>
                    <DatePicker class="sapUiLargeMarginBegin" width="50%" id="__picker0"/>
                </HBox>
                <HBox alignItems="Center">
                    <Label text="Movie Rating"/>
                    <RadioButtonGroup width="100%" columns="3" selectedIndex="-1" id="__group0">
                        <buttons>
                            <RadioButton selected="true" groupName="__group0" text="Universal" id="__button0"/>
                            <RadioButton groupName="__group0" text="Adult" id="__button1"/>
                            <RadioButton groupName="__group0" text="U/A" id="__button2"/></buttons>
                    </RadioButtonGroup>
                </HBox>
                        <HBox alignItems="Center" width="100%" renderType="Bare">
                    <Label text="Enable Booking" width="70%"/>
                <CheckBox id="__box0" width="30%" textDirection="LTR"/>
            </HBox>
                <FlexBox alignItems="End" alignContent="Center" justifyContent="End" class="sapUiTinyMarginTop">


                    <SegmentedButton selectedButton="__button3" id="__button21">
                            <buttons>
                                <Button text="Submit" id="__submit" press="submitDialog"/>
                                <Button text="Cancel" id="__button41" press="closeDialog"/></buttons>
                        </SegmentedButton>
                            </FlexBox>
                </l:content>
            </l:VerticalLayout>
        </Dialog>

    </core:FragmentDefinition>

3 个答案:

答案 0 :(得分:1)

而不是使用:

CSP14312: Resource violated directive 'connect-src 'self' http://steamcommunity.com https://steamcommunity.com https://api.steampowered.com/ http://localhost:27060 http://store.steampowered.com/ https://store.steampowered.com/' in Content-Security-Policy: https://raw.githubusercontent.com/bahamas10/css-color-names/master/css-color-names.json. Resource will be blocked.

我用过:

 var view = this.getView();
var user = sap.ui.core.Fragment.byId("createDialog", "movie_name").getValue();

它有效!!

答案 1 :(得分:1)

由于您正在使用视图ID

创建对话框
sap.ui.xmlfragment(view.getId(), "Admin.view.Dialog", oDummyController);

该片段中的所有控件ID都将以视图ID(this.getView().getId()为前缀,例如__xmlview1)。

这有一个很大的优势,即所有片段控件都可以通过this.getView().getId()访问,因此真的'感觉'就像它们是视图的一部分,可以这样对待(特别是在使用片段构建视图代码时很有用) )。

这就是你需要使用

的原因
this.getView().byId("movie_name");

最终将查询ID为__xmlview1--movie_name的控件(--被SAPUI5用作分隔符。)

byId方法的一般行为

一般来说,byId方法的行为如下:

this.getView().byId(sId) === sap.ui.getCore().byId(sViewId + '--' + sId));
sViewId + '--' + sId === this.getView().createId(sId)
sap.ui.core.Fragment.byId(sFragmentId, sId) === sap.ui.getCore().byId(sFragmentId + '--' + sId));

<强> HINT

请注意视图控件与片段之间可能存在的ID冲突!

答案 2 :(得分:0)

片段

<core:FragmentDefinition xmlns="sap.m" xmlns:core="sap.ui.core" xmlns:l="sap.ui.layout">
<Dialog id="createDialog" title="Input Movie Details" width="100%" class="sapuiMediumMargin" confirm="handleClose" close="handleClose">
    <l:VerticalLayout class="sapUiContentPadding" width="100%">
        <l:content>
            <Input width="100%" placeholder="Movie Name" id="movie_name"/>
            <HBox alignItems="Center" renderType="Bare">
                <Label text="Year of Release" width="50%"/>
                <ActionSelect selectedKey="item1" class="sapUiLargeMarginBegin" id="select0" width="50%">
                    <items>
                        <core:ListItem text="2017" key="item1" id="item0"/>
                        <core:ListItem text="2016" key="item2" id="item1"/>
                        <core:ListItem text="2015" key="item3" id="item2"/></items>
                </ActionSelect>
            </HBox>
            <HBox alignItems="Center" renderType="Bare">
                <Label text="Date of Screening" width="50%"/>
                <DatePicker class="sapUiLargeMarginBegin" width="50%" id="picker0"/>
            </HBox>
            <HBox alignItems="Center">
                <Label text="Movie Rating"/>
                <RadioButtonGroup width="100%" columns="3" selectedIndex="-1" id="group0">
                    <buttons>
                        <RadioButton selected="true" groupName="group0" text="Universal" id="button0"/>
                        <RadioButton groupName="group0" text="Adult" id="button1"/>
                        <RadioButton groupName="group0" text="U/A" id="button2"/></buttons>
                </RadioButtonGroup>
            </HBox>
            <HBox alignItems="Center" width="100%" renderType="Bare">
                <Label text="Enable Booking" width="70%"/>
                <CheckBox id="box0" width="30%" textDirection="LTR"/>
            </HBox>
            <FlexBox alignItems="End" alignContent="Center" justifyContent="End" class="sapUiTinyMarginTop">
                <SegmentedButton id="button21">
                    <buttons>
                        <Button text="Submit" id="submit" press="submitDialog"/>
                        <Button text="Cancel" id="button41" press="closeDialog"/></buttons>
                </SegmentedButton>
            </FlexBox>
        </l:content>
    </l:VerticalLayout>
</Dialog>

Controller
sap.ui.define([
    "sap/ui/core/mvc/Controller"
], function(Controller) {
    "use strict";
return Controller.extend("test001test001.controller.View1", {
    _oNewProcessDialog:null,
    onAddMovie: function() {
    this._getNewProcessDialog().open();
    },
    submitDialog:function(){

    sap.m.MessageToast.show(sap.ui.getCore().byId("movie_name").getValue());
    },
    closeDialog: function() {
            this._getNewProcessDialog().close();
        },

        _getNewProcessDialog: function() {
        // create dialog lazily
        if (!this._oNewProcessDialog) {
            // create dialog via fragment factory
            this._oNewProcessDialog = sap.ui.xmlfragment("test001test001.view.CreateDialog", this);
            // connect dialog to view (models, lifecycle)
            this.getView().addDependent(this._oNewProcessDialog);
            jQuery.sap.syncStyleClass("sapUiSizeCompact", this.getView(), this._oNewProcessDialog);
        }
        return this._oNewProcessDialog;
    },
});

});

Wiew

<Button press="onAddMovie"></Button>