关闭后如何清除dialog/xmlfragment内容? [重复]

问题描述 投票:0回答:3

我的对话框定义为文档

  onOpenDialog : function () {
     var oView = this.getView();
     var oDialog = oView.byId("helloDialog");
     // create dialog lazily
     if (!oDialog) {
        // create dialog via fragment factory
        oDialog = sap.ui.xmlfragment(oView.getId(), "sap.ui.demo.wt.view.HelloDialog");
        oView.addDependent(oDialog);
     }


     oDialog.open();
  }

假设这个对话框有很多 Input/Select/ComboBox 等等,用户在其中输入,关闭,导航到另一个主项和详细信息页面,再次打开这个对话框实例,信息仍然在这里。如果我想在每次用户关闭时清除信息/输入怎么办?

关闭后销毁此对话框是解决此问题的唯一方法吗?

sapui5
3个回答
3
投票

对话 XML

<Dialog afterClose="dialogAfterclose" >
    <beginButton>
        <Button text="yes" press="confirmOk"/>
    </beginButton>
    <endButton>
        <Button text="no" press="confirmCancel"/>
    </endButton>
</Dialog>

创建对话框

if(!this._oDialog){
    this._oDialog = sap.ui.xmlfragment("idFragment","Path_to_your_Dialog", this);           
}

你需要使用

destroy()
sap.ui.core.Element

dialogAfterclose: function(oEvent) {
    this._oDialog.destroy();
}

根据你的代码

onOpenDialog : function () {
  var oView = this.getView();
  if (!this._oDialog) {
      this._oDialog = sap.ui.xmlfragment(oView.getId(), "sap.ui.demo.wt.view.HelloDialog");
      oView.addDependent(this._oDialog);
  }
  this._oDialog.open();
},     
dialogAfterclose: function(oEvent) {//function called after Dialog is closed
   this._oDialog.destroy();//destroy only the content inside the Dialog
},
confirmOk: function(oEvent) {
    this._oDialog.close();//Just close the Dialog, Dialog afterClose() will be called and destroy the Dialog content.
}

参考:sap.ui.core.Element - destroy()


2
投票

我发现我的错误是由两个原因造成的:

1。 忘记在 destory() 之后设置 undefined

confirmCancel = function() {
   this._oAddAssignDialog.destroy();    
   this._oAddAssignDialog = undefined;
}

2。 此对话框用于在不同视图中重用的表中,表视图 ID 应设置为不同。

//view 1
<mvc:XMLView id="modifyTableView" viewName="xxx.view.AssignTable"/>
//view 2
<mvc:XMLView id="detailTableView" viewName="xxx.view.AssignTable"/>

这样

oView.getId()
不会在不同的控制器中生成相同的id。


-1
投票
/***********************************/
onOpenAddEmployeeDialog: function () {
    var oView = self.getView()
    var oDialog = oView.byId('addEmployeeFragment');

    if ((oDialog = !null)) {
       var oDialog = sap.ui.xmlfragment(oView.getId(), 'sap.ui.view.AddEmployeeFragment'
       oView.addDependent(oDialog);
    }

    var dialogModel = new JSONModel();
    oDialog.setModel(dialogModel, 'dialog');
    oDialog.open();

},
/***********************************/           


/***********************************/
 dialogAfterClose: function () {
    var oView = self.getView()
    var oDialog = oView.byId('addEmployeeFragment');

    //clear dialog Data
    var oDialogData = oDialog.getModel('dialog').getData();
    Object.getOwnPropertyNames(oDialogData).forEach(function(d) {
         oDialogData[d] = 
     });

   dialogModel.setData(oDialogData);
   oDialog.close();
   oDialog.destroy();
},
/***********************************/

*AddEmployee.fragment.xml

<core:FragmentDefinition
  xmlns:core="sap.ui.core" xmlns:m="sap.m"
  xmlns="sap.ui.layout.form"
  xmlns:l="sap.ui.layout">
  <m:Dialog id="addEmployeeFragment"  afterClose="dialogAfterClose" title="Add Employee" contentWidth="500px" contentHeight="600px">
    <m:content>
      <Form editable="true">
        <FormContainer>
            <FormElement label="Actual Entrance Time">
              <fields>
                <m:TimePicker
                  id="empAEDTP1"
                  value="{dialog>/actualStartTime}"
                  valueFormat="HH:mm"
                  minutesStep="5"
                  displayFormat="HH:mm"
                  change="handleChange"/>
              </fields>
            </FormElement>
            <FormElement label="Actual Exit Time">
              <fields>
                <m:TimePicker
                  id="empAEDTP2"
                  value="{dialog>/actualEndTime}"
                  valueFormat="HH:mm"
                  displayFormat="HH:mm"
                  minutesStep="5"
                  change="handleChange"/>
              </fields>
            </FormElement>
            <FormElement label="Note">
              <fields>
                <m:Input value="{dialog>/note}"></m:Input>
              </fields>
            </FormElement>
          </formElements>
        </FormContainer>
        <layout>
          <ResponsiveGridLayout/>
        </layout>
      </Form>
    </m:content>
    <m:endButton>
      <m:Button type="Accept" text="Save" icon="sap-icon://add" press="onSaveEmployee" />
    </m:endButton>
    <m:beginButton>
      <m:Button type="Reject" text="Cancel" icon="sap-icon://cancel" press="dialogAfterClose" />
    </m:beginButton>
  </m:Dialog>
</core:FragmentDefinition>
© www.soinside.com 2019 - 2024. All rights reserved.