I need to bind a dropdown
to an enum
. For this I've made a key_value_pair
class and I manually go through each enum
option to add it. Is there a more elegant way to achieve this?
HTML:
<select data-bind="options: regions, value: region, optionsValue: 'key', optionsText: 'value'"></select>
TypeScript:
class key_value_pair<key_type, value_type>
{
key: key_type;
value: value_type;
constructor(key: key_type, value: value_type)
{
this.key = key;
this.value = value;
}
}
class calculator
{
regions: KnockoutObservableArray<key_value_pair<number, string>>;
region: KnockoutObservable<API.region>;
constructor()
{
this.regions = ko.observableArray();
this.regions.push(new key_value_pair(API.region.US, API.region[API.region.US]));
this.regions.push(new key_value_pair(API.region.Europe, API.region[API.region.Europe]));
this.regions.push(new key_value_pair(API.region.Korea, API.region[API.region.Korea]));
this.regions.push(new key_value_pair(API.region.Taiwan, API.region[API.region.Taiwan]));
this.regions.push(new key_value_pair(API.region.China, API.region[API.region.China]));
this.region = ko.observable();
}
}