Sapui5 如何在对话框中以文本形式获取选定值

Sapui5 如何在对话框中以文本形式获取选定值,sapui5,Sapui5,我有一个简单的表格如下: 我将从后端获取员工列表(即员工姓名)。当我们从下拉列表中选择任何一个员工姓名时,将打开一个对话框,我的控制器如下所示: onEmployeechange:function(){ this.oDialog=新建sap.m.对话框({ 标题:“员工名单”, contentWidth:“40px”, 内容高度:“300px”, 内容:[ 新sap.m.Text({ 宽度:“100%”, text:“Employee Name”//这里我想从简单表单中获取所选的员工姓名,作

我有一个简单的表格如下:


我将从后端获取员工列表(即员工姓名)。当我们从下拉列表中选择任何一个员工姓名时,将打开一个对话框,我的控制器如下所示:

onEmployeechange:function(){
this.oDialog=新建sap.m.对话框({
标题:“员工名单”,
contentWidth:“40px”,
内容高度:“300px”,
内容:[
新sap.m.Text({
宽度:“100%”,
text:“Employee Name”//这里我想从简单表单中获取所选的员工姓名,作为对话框中的文本
}),
新的sap.m.Text({宽度:“100%”,文本:“城市”}),
新sap.m.FlexBox({
辩护内容:“中心”,
项目:[
新建sap.m.Select(“cityId”{
宽度:“60%”,
项目:{
路径:“/Employee/City”,
模板:new sap.ui.core.Item({
键:“{key}”,
文本:“{value}”
})
}
})
]
}),
],
});
}
我想实现上面的图像


任何帮助或指导链接都将受到感谢`

添加oEvent参数后,您可以访问选定的值,甚至可以访问键(如果需要)。 我想这是你的要求。如果这不是您需要的,请澄清


我假设您的JSONModel数据与此类似:

        var oEmployee = {
            "EmployeeList": [{
                "key": "ram",
                "value": "ram"
            }, {
                "key": "raj",
                "value": "raj"
            }, {
                "key": "rani",
                "value": "rani"
            }],

            "City": [{
                "key": "BS",
                "value": "Brescia"
            }, {
                "key": "BG",
                "value": "Bergamo"
            }]
        };
因此,您的对话框应该是:

    onEmployeechange: function (oEvent) {
        var sPath = oEvent.getParameter("selectedItem").getBindingContext("Employee").getPath();
        if (!this.oDialog) {
            this.oDialog = new sap.m.Dialog({
                title: "EmployeeList",
                contentWidth: "40px",
                contentHeight: "300px",
                content: [
                    new sap.m.Text({
                        width: "100%",
                        text: {
                            path: "Employee>value"
                        }
                    }),
                    new sap.m.Text({
                        width: "100%",
                        text: "City"
                    }),
                    new sap.m.FlexBox({
                        justifyContent: "Center",
                        items: [
                            new sap.m.Select("cityId", {
                                width: "100%",
                                items: {
                                    path: "/City",
                                    model: "Employee",
                                    template: new sap.ui.core.Item({
                                        key: "{Employee>key}",
                                        text: "{Employee>value}"
                                    })
                                }
                            })
                        ]
                    })
                ],
                beginButton: new sap.m.Button({
                    type: sap.m.ButtonType.Emphasized,
                    text: "OK",
                    press: function () {
                        this.oDialog.close();
                    }.bind(this)
                }),
                endButton: new sap.m.Button({
                    text: "Close",
                    press: function () {
                        this.oDialog.close();
                    }.bind(this)
                })
            });
            this.getView().addDependent(this.oDialog);
        }
        this.oDialog.bindElement({ path: sPath, model: "Employee" });
        this.oDialog.open();
    },
一些提示:

  • 使用模块的AMD定义(在本例中为
    文本
    按钮
    对话框
  • 重复使用
    对话框
  • 对于复杂的对话框,更喜欢XML片段而不是js,或者直接在XML视图中将对话框添加为依赖项
  • 使用
    bindElement
编辑 结果


是的,这是我的要求,但当我尝试以oEvent.getSource(…).getSelectedItem(…).getName的形式获取错误时,我尝试使用getText()代替getName()根据我的要求在对话框中将所选名称作为文本获取,但只显示第一个所选名称下一个所选名称不更新确保所有项都有不同的键。我只有不同的键我尝试获取错误,因为无法读取未定义项的属性“getPath”。可能您必须根据需要调整代码(不同的模型名称?)。您是否尝试调试错误?路径是否未定义或其他原因?我在此对话框和路径中有不同的模型名称:“Employee>value”,而且由于未定义员工,我也收到了错误…因此请使用您的模型名称和属性:)我只能猜测你的数据,但机制是上面描述的如果你想看看它是如何工作的,我建议你创建一个新的空应用程序,复制上面的代码,用我提供给你的数据在控制器的onInit中初始化一个JSONModel,并做一些测试。然后将其应用到你的应用程序中
    onEmployeechange: function (oEvent) {
        var sPath = oEvent.getParameter("selectedItem").getBindingContext("Employee").getPath();
        if (!this.oDialog) {
            this.oDialog = new sap.m.Dialog({
                title: "EmployeeList",
                contentWidth: "40px",
                contentHeight: "300px",
                content: [
                    new sap.m.Text({
                        width: "100%",
                        text: {
                            path: "Employee>value"
                        }
                    }),
                    new sap.m.Text({
                        width: "100%",
                        text: "City"
                    }),
                    new sap.m.FlexBox({
                        justifyContent: "Center",
                        items: [
                            new sap.m.Select("cityId", {
                                width: "100%",
                                items: {
                                    path: "/City",
                                    model: "Employee",
                                    template: new sap.ui.core.Item({
                                        key: "{Employee>key}",
                                        text: "{Employee>value}"
                                    })
                                }
                            })
                        ]
                    })
                ],
                beginButton: new sap.m.Button({
                    type: sap.m.ButtonType.Emphasized,
                    text: "OK",
                    press: function () {
                        this.oDialog.close();
                    }.bind(this)
                }),
                endButton: new sap.m.Button({
                    text: "Close",
                    press: function () {
                        this.oDialog.close();
                    }.bind(this)
                })
            });
            this.getView().addDependent(this.oDialog);
        }
        this.oDialog.bindElement({ path: sPath, model: "Employee" });
        this.oDialog.open();
    },