sap.m.Dialog在第二次打开时显示重复ID错误

时间:2017-11-20 06:24:54

标签: dialog fragment sapui5 sap-fiori

点击按钮并填写一些信息后,我打开一个对话框。当我再次打开对话框时,它会显示错误:

 Error: adding element with duplicate id '__xmlview1--__item0'

以下是代码: (编辑:每次创建对话框时保存创建对话框实例)

        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() {
            view.byId("panel").setVisible(true);

            user = view.byId("movie_name").getValue();
            var label = view.byId("movieName");
            label.setText(user);

            screenDate=view.byId("screeningDate").getValue();
            var date = view.byId("__date");
            var dateObject = new Date(screenDate);
            var dateFormat = sap.ui.core.format.DateFormat.getDateInstance({pattern : "MMM dd,YYYY" }); 
            var dateFormatted = dateFormat.format(dateObject);
            date.setText(dateFormatted);


            rating=view.byId("langRating");
            var radioRating= view.byId("movieRating").getSelectedButton().getText();
            if(radioRating == "Universal")
           { radioRating='(U)'; }
            else if(radioRating == "Adult")
            {
                radioRating='(A)';
            }
            else
            {
                    radioRating='(U/A)';
            }


            rating=view.byId("langRating");
            rating.setText(radioRating);

                MessageToast.show(user);
                createDialog.close();
            },
            closeDialog: function() {
                createDialog.close();
            }
        };
        // This is when the dialog event is fired, things are fine here
        if (!createDialog) {
            createDialog = sap.ui.xmlfragment(view.getId(), "Admin.view.Dialog", oDummyController);
        }

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

片段:

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

                <Dialog  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="yearOfRelease" 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="screeningDate"/>
                        </HBox>
                        <HBox alignItems="Center">
                            <Label text="Movie Rating"/>
                            <RadioButtonGroup width="100%" columns="3" selectedIndex="-1" id="movieRating">
                                <buttons>
                                    <RadioButton  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="enableBooking" 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>

请帮助我,因为我是Javascript和SAPUI5的新手

1 个答案:

答案 0 :(得分:1)

您似乎一遍又一遍地创建相同的对话框,这会导致重复的ID错误。

检查var createDialog = view.byId("CreateDialog");是否确实返回了以前的对话框。

对话框创建

确保您可以将其保存在实例变量中:

if (this._createDialog) {
  this._createDialog = sap.ui.xmlfragment(view.getId(), "Admin.view.Dialog", oDummyController);
}

另一个解决方案是每次重新创建它并在每次关闭后继续销毁它:

<Dialog afterClose="onAfterClose">

onAfterClose: function(oEv) {
  oEv.getSoruce().destroy();
}

这就是我所说的自我毁灭:D

ID冲突

使用视图ID前缀:

创建片段时
sap.ui.xmlfragment(this.getView().getId(), "my.Fragment", this);

您必须确保视图控件与片段之间没有任何ID冲突。像__item0这样的ID类似于自动生成的UI5 ID(__[type][count])。在示例片段中省略ui5内部前缀__就足够了。

如果你必须像你一样广泛使用id(在大多数情况下你不必这样做),你最好为片段使用单独的ID

sap.ui.xmlfragment("fragment", "my.Fragment", this);

并使用

进行查询
sap.ui.core.Fragment.byId("fragment", "controlId");

BR 克里斯