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

sap.m.Dialog Shows Duplicate ID Error on 2nd Open

我在单击按钮并向其中填充一些信息后打开了一个对话框。当我再次打开对话框时,它显示错误:

 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

的新手

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

检查 var createDialog = view.byId("CreateDialog"); 是否真的 returns 您之前的对话。

对话框创建

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

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-internal 前缀 __ 就足够了。

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

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

并使用

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

BR 克里斯