有没有办法从Qooxdoo中的数组初始化SelectBox中的项目



这看起来很基本,但似乎找不到实现它的方法:我想从数组初始化SelectBox

var array = ["item1","item2"...]

而不是必须循环通过ListItems

var selectBox = new qx.ui.form.SelectBox();
var test = ["item1", "item2"];
for (var i = 0; i < test.length; i++){
    var tempItem = new qx.ui.form.ListItem(test[i]);
    selectBox.add(tempItem);
}

在Qooxdoo有办法做到这一点吗?

首先是一个更优雅的循环版本:

var selectBox = new qx.ui.form.SelectBox();
test = ["item1", "item2"];
test.forEach(function(obj) {
    selectBox.add(new qx.ui.form.ListItem(obj));
}, this);

但您应该查看Qooxdoo中的数据绑定文档(文档链接)。当使用这个时,你有一个这样的解决方案:

var selectBox = new qx.ui.form.SelectBox();
test = ["item1", "item2"];
new qx.data.controller.List(new qx.data.Array(test), selectBox);

当使用控制器时,您可以获得一些更有趣的功能,如轻松地将更改事件绑定到其他小部件等。

最新更新