javascript - Drop-down list not working in a Knockout KoGrid -
i'm trying display drop-down list in kogrid cell using custom cell template , have no ideea why it's not working should.
i have example of working drop-down list using options, optionstext, optionsvalue , optionscaption
, bindings work should. similar drop-down in kogrid not display elements. question missing/doing wrong , how can fix problem?
link jsfiddle: http://jsfiddle.net/axywz/6/
html:
<p> working drop-down list: <select data-bind="options: data, optionstext: 'name', optionsvalue: 'id', optionscaption: '-', value: selecteditemid"></select> </p> <p> drop-down list not working in kogrid: <div class="grid" data-bind="kogrid: gridoptions"></div> </p> <pre data-bind="text: ko.tojson($root, null, 2)"></pre> <script type="text/html" id="template"> <select data-bind="options: $root.data, optionstext: 'name', optionsvalue: 'id', optionscaption: '-', value: $parent.entity[$data.field]"></select> </script>
javascript:
function item(id, name) { this.id = ko.observable(id); this.name = ko.observable(name); this.parentid = ko.observable(); } function viewmodel() { this.selecteditemid = ko.observable(); this.data = ko.observablearray([ new item(1, 'aaa'), new item(2, 'sss'), new item(10, 'xxx'), new item(14, 'zzz') ]); this.gridoptions = { data: this.data, canselectrows: false, columndefs: [ { field: 'id', displayname: 'id' }, { field: 'name', displayname: 'name' }, { field: 'parentid', displayname: 'parent', celltemplate: $.trim($('#template').html()) }, ] }; } ko.applybindings(new viewmodel());
when inside celltemplate need use $userviewmodel
access "$root" viewmodel.
from documentation:
$userviewmodel: {{ko binding context}}// accessor viewmodel used instantiate grid.
so need write:
<script type="text/html" id="template"> <select data-bind="options: $userviewmodel.data, optionstext: 'name', optionsvalue: 'id', optionscaption: '-', value: $parent.entity[$data.field]"> </select> </script>
demo jsfiddle.
Comments
Post a Comment