Sunday, 15 September 2013

javascript - Add function to the view model in order to set the OptionText in a select element -



javascript - Add function to the view model in order to set the OptionText in a select element -

i have next view model:

var viewmodel = function (list) { var self = this; self.selected = ko.observable(""); self.items = ko.observablearray(list); }

and html:

<select data-bind="options: items, optionsvalue: 'id', optionstext: function(item) { homecoming item.id + ' - ' + item.name }, value: selected">

note code below working perfectly.

i refectoring , optimize moving optionstext function within view model.

so tried (unsuccessfully) that:

view model:

var viewmodel = function (list) { var self = this; self.selected = ko.observable(""); self.items = ko.observablearray(list); self.displayname = function (item) { homecoming item.id + ' - ' + item.name; } }

html:

<select data-bind="options: items, optionsvalue: 'id', optionstext: displayname (item???), value: selected">

the problem cannot grab current item beingness rendered, cannot send function...

i know if each item view model move displayname function item view model (making computed function).

anyone know how it? or if possible it?

you need provide reference function knockout able pass current item:

optionstext: displayname

html:

<select data-bind="options: items, optionsvalue: 'id', optionstext: displayname, value: selected">

working fiddle

javascript html knockout.js

No comments:

Post a Comment