jquery - how to bind data to dropdown dynamically using knockout.js? -
i new knockout.js. wasn't able bind data api dropdown using knock out js.
my json data api , dropdown is:
[{ contactid: 0, firstname: "aaa", lastname: "bbb", mobilenumber: null, startdate: "0001-01-01t00:00:00", enddate: "0001-01-01t00:00:00" }, { contactid: 0, firstname: "ccc", lastname: "ddd", mobilenumber: null, startdate: "0001-01-01t00:00:00", enddate: "0001-01-01t00:00:00" } ] <select id="selectmenu1" name="" data-theme="c" data-bind="optionscaption: 'choose...'"> </select> i bind firstname,lastname,contactid dropdown , display firstname , lastname text , contactid value field item. please give suggestions regarding this?
you need use options binding, need specify:
- your array of items in
options(see in doc example 3) - you need set
optionsvalue: 'contactid'have contactid value - you need specify function in
optionstextbuilds select texts (see in doc example 4)
so final binding like:
<select id="selectmenu1" name="" data-theme="c" data-bind="options: contacts, optionsvalue: 'contactid', optionstext: function(i) { return i.firstname + ' ' + i.lastname }, optionscaption: 'choose...'"> </select> demo jsfiddle.
Comments
Post a Comment