简体   繁体   English

KnockoutJS:创建绑定处理程序以将值更改为枚举

[英]KnockoutJS : Create binding handler to change value to enum

I have a knockout custom binding that takes a value and returns an enum. 我有一个淘汰赛自定义绑定,它接受一个值并返回一个枚举。 This is for user readability. 这是为了用户可读性。 The problem is that when passing in the parameter for this binding, it just comes back blank. 问题在于,当传入此绑定的参数时,它只是变回空白。 I've tried passing in the parameter as an observable, ex: PurchaseOrderStatus[Status()]. 我尝试将参数作为可观察的参数传递,例如:PurchaseOrderStatus [Status()]。

The view: 风景:

 <td style="vertical-align: top">
            <label>Status</label><br />
            <span data-bind="text: PurchaseOrderStatus[Status]"></span>
        </td>

The javascript : javascript:

PurchaseOrderStatus = function() { };
PurchaseOrderStatus.prototype = {
Closed: 67, 
Deleted: 68, 
Finalized: 70, 
Open: 79}
PurchaseOrderStatus.registerEnum('PurchaseOrderStatus', false);

The way it should work is that if the value is 70, then the user sees Finalized. 它的工作方式是,如果该值为70,则用户将看到Finalized。

how about something like this. 这样的事情怎么样

 function model() { var self = this; this.PurchaseOrderStatus = ko.observable({ '67': 'Closed', '68': 'Deleted', '70': 'Finalized', '79': 'Open' }); this.status = ko.observable('68'); } var mymodel = new model(); $(document).ready(function() { ko.applyBindings(mymodel); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span data-bind="text: PurchaseOrderStatus()[status()]"></span> 

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

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