3

カスタム セル テンプレートを使用して KoGrid セルにドロップダウン リストを表示しようとしていますが、正常に動作しない理由がわかりません。

を使用した実際のドロップダウン リストの例がoptions, optionsText, optionsValue and optionsCaptionあり、バインディングは正常に機能します。しかし、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());
4

1 に答える 1

3

celltemplate 内にいる場合は$userViewModel、「$root」ビューモデルにアクセスするために を使用する必要があります。

ドキュメントから:

$userViewModel: {{ko binding context}}// グリッドのインスタンス化に使用したビューモデルへのアクセサー。

したがって、次のように記述する必要があります。

<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 のデモ。

于 2013-04-09T09:22:56.633 に答える