gpt4 book ai didi

javascript - 在 OpenUI5 中运行时创建按钮

转载 作者:行者123 更新时间:2023-12-02 15:07:09 26 4
gpt4 key购买 nike

我是 SAPUI5/OpenUI5 的新手,正在编写一个实验性小项目,我想在其中为长度可变的列表中的每个项目创建一个按钮。

这些按钮必须指向同一个事件,但甚至必须通过参数、元素或类似方式知道按下了哪个按钮。

我在文档中找不到如何在基于列表的 View 中显示按钮的信息。我找不到在运行时创建它们的方法 <FlexBox>元素。

有任何灯光或链接可以帮助我吗?

最佳答案

这是可行的。

//this is a common btn click handler
var btnHandler = function(evt) {
var obtn = evt.getSource();
//now you have access to the respective button
var customData = obtn.getCustomData()[0].getValue();
sap.m.MessageToast.show("button Clicked:" + customData)
};

var oFlexBox = new sap.m.FlexBox();

for (var i = 0; i < 5; i++) {
var btn = new sap.m.Button({
text: "Button" + i,
press: btnHandler,
//add your custom data here.. this is an aggregation which means you can add as many customDatas as required.
customData: new sap.ui.core.CustomData({
key: "key",
value: i
})
});
oFlexBox.addItem(btn);
}

oFlexBox.placeAt('content');

Working JSFiddle here

关于javascript - 在 OpenUI5 中运行时创建按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35048983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com