gpt4 book ai didi

javascript - 下拉列表在 Knockout KoGrid 中不起作用

转载 作者:行者123 更新时间:2023-11-29 10:47:58 26 4
gpt4 key购买 nike

我正在尝试使用自定义单元格模板在 KoGrid 单元格中显示下拉列表,但我不知道为什么它不能正常工作。

我有一个使用 options、optionsText、optionsValue 和 optionsCaption 的工作下拉列表示例,并且绑定(bind)正常工作。但是 KoGrid 中类似的下拉列表不显示任何元素。我的问题是我错过了什么/做错了什么,我该如何解决这个问题?

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());

最佳答案

当您在单元格模板中时,您需要使用 $userViewModel 来访问您的“$root” View 模型。

来自documentation :

$userViewModel: {{ko binding context}}// accessor to your viewmodel that you used to instantiate the grid.

所以你需要这样写:

<script type="text/html" id="template">
<select data-bind="options: $userViewModel.data,
optionsText: 'name', optionsValue: 'id',
optionsCaption: '-', value: $parent.entity[$data.field]">
</select>
</script>

演示 JSFiddle.

关于javascript - 下拉列表在 Knockout KoGrid 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15897397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com