【问题标题】:Bind dxSelectBox on button click event在按钮单击事件上绑定 dxSelectBox
【发布时间】:2015-02-20 11:54:37
【问题描述】:

我正在使用 DevExtreme MVVM 架构,根据我在按钮单击事件时的场景,我需要绑定 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 有空值... 我是这个环境的新手,我不知道我在哪里做错了..

【问题讨论】:

    标签: javascript jquery mvvm devexpress devextreme


    【解决方案1】:

    恐怕你忘了设置'value'选项,例如:

    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;
    

    【讨论】:

    • 感谢您花时间在此...仅在按钮单击 [display()] 处,绑定值直到它为空..
    • 不确定我是否理解正确,模型可以通过 onClick 处理程序的参数访问,有关更多详细信息,请参阅js.devexpress.com/Documentation/ApiReference/UI_Widgets/… - e.model
    • 您可以使用 dxSelectBox 的 onChange 处理程序:js.devexpress.com/Documentation/ApiReference/UI_Widgets/…
    • 是否可以更新数据源(themesArray)并在显示函数中分配它,无论更新的数据源是否绑定。
    • 简单来说,我需要通过 On Change 绑定更新的数据源在 dxSelectBox 中,或者单击任何可能的按钮...即使我厌倦了 onChange 但更新的数据源不会受到影响..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    相关资源
    最近更新 更多