gpt4 book ai didi

javascript - 在按钮单击事件上绑定(bind) dxSelectBox

转载 作者:行者123 更新时间:2023-11-28 07:44:29 27 4
gpt4 key购买 nike

我正在使用 DevExtreme MVVM 架构,根据我的场景,当按钮单击事件时,我需要绑定(bind) dxSelectBox(组合框)。

HTML 代码:

<div data-bind="dxButton:{onClick:display,text:'Click Me'}"></div>

<div data-bind="dxSelectBox:{dataSource: themes, displayExpr: 'name' }"></div>

JS 代码:

var themesArray = [
{ themeId: 1, name: "Android (Dunkel)" },
{ themeId: 2, name: "Desktop" },
{ themeId: 3, name: "iOS" },
{ themeId: 4, name: "Windows 8" },
{ themeId: 5, name: "Windows Phone 8" },
{ themeId: 6, name: "Tizen" }
];

var themes = new DevExpress.data.DataSource(themesArray);

var viewModel = {
themes: '',
display: function () {
console.log(themesArray);
themes: themesArray
}
};
return viewModel;

提示:dxSelectBox 具有空值...我是这个环境的新手,我不知道我哪里做错了..

最佳答案

恐怕您忘记设置“值”选项,例如:

data-bind="dxSelectBox: { dataSource: [ { val: true, text: 'Yes' }, { val: false, text: 'No' }], valueExpr: 'val', displayExpr: 'text', value: visible }"

根据您的上下文:

var viewModel = {
themes: [ ...array of themes... ],
selectedThemeId: 1,
display: function () {
console.log(themesArray);
themes: themesArray
}
};

data-bind="dxSelectBox: { dataSource: themes, valueExpr: 'themeId', displayExpr: 'name', value: selectedThemeId }"

我厌倦了以下代码:

 var themesArray = [
{ themeId: 1, name: "Android (Dunkel)" },
{ themeId: 2, name: "Desktop" },
{ themeId: 3, name: "iOS" },
{ themeId: 4, name: "Windows 8" },
{ themeId: 5, name: "Windows Phone 8" },
{ themeId: 6, name: "Tizen" }
];

var modifiedthemesArray = [
{ themeId: 1, name: "a (Dunkel)" },
{ themeId: 2, name: "b" },
{ themeId: 3, name: "c" },
{ themeId: 4, name: "Windows 8" },
{ themeId: 5, name: "Windows Phone 8" },
{ themeId: 6, name: "Tizen" }
];

var themes = new DevExpress.data.DataSource(themesArray);

var viewModel = {
themes: themesArray,
selectedThemeId: 1,
display: function (e) {
console.log(e);
themes: modifiedthemesArray
}
};
return viewModel;

您可以使用observableArray更改集合对象。我厌倦了以下代码,它对我有用:

 var themesArray = [
{ themeId: 1, name: "Android (Dunkel)" },
{ themeId: 2, name: "Desktop" },
{ themeId: 3, name: "iOS" },
{ themeId: 4, name: "Windows 8" },
{ themeId: 5, name: "Windows Phone 8" },
{ themeId: 6, name: "Tizen" }
];

var viewModel = {
themes: ko.observableArray(),
display: function() {
console.log(themesArray);
viewModel.themes(themesArray);
}
};

return viewModel;

关于javascript - 在按钮单击事件上绑定(bind) dxSelectBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27601739/

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