为什么我的 table 行到对话框绑定失败?

Why is my table row to dialog binding failing?

我正在尝试使用 JSView 重新创建此示例:

https://embed.plnkr.co/BA9T4Z0QdsZrqkooWTs2/

数据绑定到table,然后当点击一行时,它将行数据绑定到对话框。

我试图在我自己的项目中遵循这个例子,但我无法让它工作,所以我尝试在 JSView 中逐步重新创建上面的例子。

https://embed.plnkr.co/jvaQyVgvjNP261lVBFaz/(忽略 App.view.xml 文件)。

我的控制器的第 34 行 getBindingContext("userList") 未定义,即使我使用的是正确的型号名称。

这与示例控制器的第 31 行形成对比,其中 getBindingContext("list") returns 一个对象。

我做错了什么?

在XML示例中,item press设置在列列表项上,在JS示例中设置在table。 getSource() then returns the table instead of the line.您可以通过更改新闻事件来修复它:

var oTable = new sap.m.Table({
    id: "UserTable",
    columns: [
        new sap.m.Column({
            header: new sap.m.Label({
                text: "User ID"
            })
        }),
        new sap.m.Column({
            header: new sap.m.Label({
                text: "Name"
            })
        })          
    ]
});

var template = new sap.m.ColumnListItem({
    type: "Active",
    visible: true,
    press: [oController.onItemPress, oController],
    cells: [
        new sap.m.Text({
            text: "{userList>Userid}"
        }),
        new sap.m.Text({
            text: "{userList>Name}"
        })
    ]
});

其实你不必改变事件,在Table上使用itemPress事件也可以。只是参数不同。要获取所选项目及其上下文,请使用:

event.getParameter("listItem"); // returns: pressed list item

而不是

event.getSource(); // returns: source control that fired the event (here: sap.m.Table)

More info on the itemPress event