简体   繁体   English

具有下拉值的KnockoutJS CSS绑定

[英]KnockoutJS CSS binding with a dropdown value

Fiddle here: http://jsfiddle.net/0gmbbv5w/ 在这里提琴: http : //jsfiddle.net/0gmbbv5w/

I've got an object that I pull from the database and bind to a <select> 我有一个从数据库中提取并绑定到<select>

var NoticeType = function (noticeType) {
    this.NoticeTypeId = ko.observable(noticeType.NoticeTypeId);
    this.NoticeLevel = ko.observable(noticeType.NoticeLevel);
    this.FriendlyName = noticeType.FriendlyNoticeLevel;
}

These will be populated in my viewmodel like so 这些将像这样填充在我的视图模型中

NoticeType: new NoticeType({
    NoticeTypeId: 2,
    NoticeLevel: 'info',
    FriendlyNoticeLevel: 'Informational'
})

The NoticeTypeId is being stored in the database, and the NoticeLevel is a CSS class that is bound to an element. NoticeTypeId将存储在数据库中, NoticeLevel是绑定到元素的CSS类。 'FriendlyName` is what shows as the option text. “ FriendlyName”是显示为选项文本的内容。

When adding a new notice, when selecting the type, the NoticeLevel should change the CSS class when the dropdown changes. 在添加新通知时,在选择类型时,当下拉列表更改时,NotificationLevel应该更改CSS类。

<select data-bind="options: $parent.noticeTypes, optionsText: 'FriendlyName', value: noticeType"></select>


<div class="row-fluid">
    <div class="span12">
        <div class="alert" data-bind="css: alertLevel">
            <h4>Just a reminder!</h4>
            <span>This is a sample of what your notice will look like. The header is the Subject, and this text is the Message</span>
        </div>
    </div>
</div>

The only way I can figure out to bind the CSS class when selecting it, is to remove the optionsValue binding. 我选择绑定CSS类的唯一方法是删除optionsValue绑定。 My problem comes from when selecting an item to edit , the binding doesn't work properly, so the NoticeType when editing always shows as the "basic" notice type because it can't map the data back. 我的问题来自选择要编辑的项目时,绑定无法正常工作,因此编辑时的NoticeType始终显示为“基本”通知类型,因为它无法将数据映射回去。

Is there some way I can accomplish this? 有什么办法可以做到这一点? Or am I missing something obvious here? 还是我在这里缺少明显的东西? When adding a new one, or selecting one to edit and changing the select value, it will update the CSS class properly. 当添加一个新的或选择一个进行编辑并更改选择值时,它将正确更新CSS类。 When initially selecting one to edit, it does not properly bind the CSS class. 最初选择一个进行编辑时,它无法正确绑定CSS类。

self.editNotice = function (item) {
    self.noticeToAdd(new NoticeToAdd(ko.toJS(item)));
}

var NoticeToAdd = function (notice) {

    var self = this;

    if (!notice) {
        notice = {};
    }

    this.noticeId = ko.observable(notice.NoticeId || notice.noticeId);
    this.subject = ko.observable(notice.Subject || notice.subject);
    this.body = ko.observable(notice.Body || notice.body);
    this.publishDate = ko.observable(notice.PublishDate || notice.publishDate);
    this.expireDate = ko.observable(notice.ExpireDate || notice.expireDate);
    this.sticky = ko.observable(notice.Sticky || notice.sticky);
    this.includeDismiss = ko.observable(notice.IncludeDismissAction || notice.includeDismiss || true);
    this.noticeType = ko.observable(notice.NoticeType || notice.noticeType);
    this.showDismissal = ko.observable(false);

    //CSS binding
    this.alertLevel = ko.pureComputed(function() { 
        return self.noticeType() ? 'alert-' + self.noticeType().NoticeLevel() : 'alert-basic';
    });

    this.ableToAddNotice = ko.pureComputed(function () {
        return $.trim(self.body()) != "";
    });

}

The noticeType used as the value of the options binding needs to be one of the objects in the noticeTypes array. 用作options绑定valuenoticeType必须是noticeTypes数组中的对象之一。 Right now you're telling the <select> to have the selected item be something not in its list. 现在,您要告诉<select>使其所选项目不在其列表中。 This is causing the <select> to default to the first value in the list, the "basic" option. 这导致<select>默认为列表中的第一个值,即“基本”选项。

One solution would be find the object in the noticeTypes array that "equals" the existing noticeType and use that as the value . 一种解决方案是在noticeTypes数组中找到“等于”现有noticeType并将其用作value

Your noticeType objects in test data are not the same objects as in your noticeTypes observable array (instead they are different objects with the same data). 测试数据中的noticeType对象与noticeTypes可观察数组中的对象不同(相反,它们是具有相同数据的不同对象)。

You will need to grab exact objects from noticeTypes array: 您将需要从noticeTypes数组中获取确切的对象:

//Test data!
var successType = ko.utils.arrayFirst(self.noticeTypes(), function (type) {
    return type.NoticeLevel() === 'success';
});
var warningType = ko.utils.arrayFirst(self.noticeTypes(), function (type) {
    return type.NoticeLevel() === 'warning';
});
var infoType = ko.utils.arrayFirst(self.noticeTypes(), function (type) {
    return type.NoticeLevel() === 'info';
});
var dangerType = ko.utils.arrayFirst(self.noticeTypes(), function (type) {
    return type.NoticeLevel() === 'danger';
});

And then you can initialise your test data like this: 然后,您可以像这样初始化测试数据:

self.notices.push(new NoticeToAdd({
    NoticeId: 1,
    Subject: 'Notice this!',
    Body: '<strong>YO!</strong> this is a supre cool notice man',
    NoticeType: successType
}));
self.notices.push(new NoticeToAdd({
    NoticeId: 2,
    Subject: 'Notice this!',
    Body: '<strong>YO!</strong> this is a supre cool notice man',
    NoticeType: warningType
}));
self.notices.push(new NoticeToAdd({
    NoticeId: 3,
    Subject: 'Notice this!',
    Body: '<strong>YO!</strong> this is a supre cool notice man',
    NoticeType: infoType
}));
self.notices.push(new NoticeToAdd({
    NoticeId: 4,
    Subject: 'Notice this!',
    Body: '<strong>YO!</strong> this is a supre cool notice man',
    NoticeType: dangerType
}));
self.notices.push(new NoticeToAdd({
    NoticeId: 5,
    Subject: 'Notice this!',
    Body: '<strong>YO!</strong> this is a supre cool notice man'
}));

See updated fiddle : 参见更新的小提琴

Obviously with proper data coming from the server this should be much simpler: 显然,使用来自服务器的适当数据,这应该更加简单:

var someType = ko.utils.arrayFirst(self.noticeTypes(), function (type) {
    return type.NoticeTypeId() === NoticeTypeIdFromTheServer;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM