【问题标题】:how to bind to items within an observable array in knockout?如何在淘汰赛中绑定到可观察数组中的项目?
【发布时间】:2013-03-20 22:45:16
【问题描述】:

我有以下可观察数组:

self.Profiles =ko.observableArray( ko.utils.arrayMap(initialData, function (profile) {
                return {
                    StartDate : formatDateOnly(profile.StartDate),
                    EndDate : formatDateOnly(profile.EndDate),
                    ProfileID :profile.ID,
                    ProfileName : profile.Name,
                    ProjectName : profile.ProjectName,
                    ReadingListID : profile.ReadingListID,
                    ReadingListName : profile.ReadingListName

                };
            }));

我想将一个下拉列表绑定到配置文件以显示配置文件名称,如果下拉列表的值发生变化,那么我想用所选 profileID 的新对应值更新 span 元素。

<table id="readingListApplyToProfile" class="fullWidthTable">
        <tr>
            <td>
                Profile:
            </td>
            <td>
                <select id="cboProfile" name="cboProfile" data-bind="options: Profiles, optionsText: 'ProfileName', 'optionsCaption': 'Select profile...', optionsValue:'ProfileID'"></select>
            </td>
        </tr>
        <tr>
            <td>
                End Date:
            </td>
            <td>
                <span data-bind="'text':EndDate"></span>
            </td>
        </tr>
    </table>

我无法更新跨度,因为跨度元素不知道下拉列表的值,谁能帮助我。我完全迷路了。

【问题讨论】:

  • 也许将 select 的值绑定到接受 ProfileID 并输出正确 Profile 的 ProfileName 的 computedObservable 上,然后将 spans 的文本绑定到相同的 computedObservable 上会起作用
  • 这可能会起作用,但我不想为所有属性执行相同的代码,我希望有人能给我一个更好的解决方案。谢谢
  • 有道理。这是一个完整的猜测,因为我不太擅长 computedObservables,但是否可以输出整个 Profile 对象,并绑定到 span 上的特定属性?喜欢data-bind="text: getProfile.ProfileName" 或类似的东西?
  • 你有多少这样的下拉菜单?这只是一个下拉菜单和一个跨度吗?
  • 是的,它只有一个下拉菜单

标签: knockout.js


【解决方案1】:

您缺少的是下拉列表中的值绑定。这是我创建的小提琴。

http://jsfiddle.net/sujesharukil/sBZvb/1/

<select id="cboProfile" name="cboProfile" data-bind="options: Profiles, optionsText: 'ProfileName', 'optionsCaption': 'Select profile...', optionsValue:'ProfileID', value: selectedProfileId ">

这是视图模型

var myViewModel = function()
{
    var self = this;
        this.Profiles =ko.observableArray([{
                    StartDate : '02/01/2012',
                    EndDate : '01/01/2013',
                    ProfileID :10,
                    ProfileName : 'Some Name',
                    ProjectName : 'Some Project',
                    ReadingListID : 100,
                    ReadingListName : 'Some List',
                },
                {
                    StartDate : '12/01/2012',
                    EndDate : '02/27/2013',
                    ProfileID :12,
                    ProfileName : 'New Name',
                    ProjectName : 'New Project',
                    ReadingListID : 200,
                }]);
    this.selectedProfileId = ko.observable({}); //this stores the selected id

}

ko.applyBindings(new myViewModel());

希望对您有所帮助。

苏杰

【讨论】:

    【解决方案2】:

    我有一个 computedObservable 的想法,它接受 ProfileID 并输出与该 ID 对应的 Profile,然后将 span 绑定到输出对象的各种属性。令人惊讶的是,它奏效了。我正在摆弄 Sujesh Arukil 的小提琴来尝试我的想法,所以模型非常相似。

    显示多个跨度的工作示例:http://jsfiddle.net/jonhopkins/fgZNQ/2/

    模型:

    var myViewModel = function()
    {
        var self = this;
            self.Profiles =ko.observableArray([{
                        StartDate : '02/01/2012',
                        EndDate : '01/01/2013',
                        ProfileID :10,
                        ProfileName : 'Some Name',
                        ProjectName : 'Some Project',
                        ReadingListID : 100,
                        ReadingListName : 'Some List',
                    },
                    {
                        StartDate : '12/01/2012',
                        EndDate : '02/27/2013',
                        ProfileID :12,
                        ProfileName : 'New Name',
                        ProjectName : 'New Project',
                        ReadingListID : 200,
                    }]);
    
        self.selectedProfileId = ko.observable();
    
        self.getProfile = ko.computed({
            read: function() {
                for (var i = 0; i < self.Profiles().length; i++) {
                    if (self.Profiles()[i].ProfileID == self.selectedProfileId()) {
                        return self.Profiles()[i];
                    }
                }
                // in case there was no match, output a blank Profile
                return [{
                        StartDate : '',
                        EndDate : '',
                        ProfileID : '',
                        ProfileName : '',
                        ProjectName : '',
                        ReadingListID : '',
                        ReadingListName : ''
                }];
            },
            write: function(value) {
                self.selectedProfileId(value);
            },
            owner: self
        });
    }
    
    ko.applyBindings(new myViewModel());
    

    编辑: Sujesh 提出了一个更好的 computedObservable 版本。

    self.getProfile = ko.computed(function(){
        var profile = ko.utils.arrayFirst(self.Profiles(), function(prof){
            return prof.ProfileID == self.selectedProfileId();
        });
    
        return profile || {};
    });
    

    HTML:

    <table id="readingListApplyToProfile" class="fullWidthTable">
        <tr>
            <td>
                Profile:
            </td>
            <td>
                <select id="cboProfile" name="cboProfile" data-bind="options: Profiles, optionsText: 'ProfileName', 'optionsCaption': 'Select profile...', optionsValue:'ProfileID', value: getProfile "></select>
            </td>
        </tr>
        <tr>
            <td>
                End Date:
            </td>
            <td>
                <span data-bind="text: getProfile().ProfileName"></span>
            </td>
        </tr>
    </table>
    

    【讨论】:

      猜你喜欢
      • 2013-04-26
      • 2012-09-08
      • 2019-05-16
      • 2013-03-12
      • 2014-09-28
      • 2013-05-19
      • 2014-08-22
      • 2013-03-22
      相关资源
      最近更新 更多