0

我正在使用 KoGrid 插件来创建一个带有选择复选框的剔除网格。到目前为止,我有以下代码。

    function columnDefsVM() {
    var self = this;
    this.myData = ko.observableArray(GlobalJson);
    this.mySelectedData = ko.observableArray(SelectedJson);

    this.gridOptions = {
        data: self.myData,
        columnDefs: [{ field: 'TestEventId', displayName: 'Name' }],
        selectedItems: self.mySelectedData,
        enablePaging: false,
    };
}
ko.applyBindings(new columnDefsVM());

我只是想知道如何访问该selectedItems属性,以便可以将选定的值传递给 ajax 调用?

或者我可以不这样做并且必须使用该选项手动将选定的 Id 推送到数组afterSelectionChange

4

1 回答 1

2

在您gridOptions中,您传递对mySelectedData可观察数组的引用以用作 the ,SelectedItems因此您应该能够使用原始mySelectedData变量而不是SelectedItems; 它们是等价的。mySelectedData将在网格选择更改时更新。

function columnDefsVM() {
    var self = this;
    this.myData = ko.observableArray([
    	{TestEventId: 1, Name: 'Object 1'},
    	{TestEventId: 2, Name: 'Object 2'},
    	{TestEventId: 3, Name: 'Object 3'},
    ]);
    this.mySelectedData = ko.observableArray([]);

    this.gridOptions = {
        data: self.myData,
        columnDefs: [{ field: 'TestEventId', displayName: 'Name' }],
        selectedItems: self.mySelectedData,
        enablePaging: false,
    };
}
ko.applyBindings(new columnDefsVM());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/Knockout-Contrib/KoGrid@2.0.6/koGrid-2.0.6.debug.js"></script>
<link href="https://cdn.jsdelivr.net/gh/Knockout-Contrib/KoGrid@2.0.6/KoGrid.css" rel="stylesheet" />

<label>mySelectedData: </label><span data-bind="text: ko.toJSON(mySelectedData)"></span>
<br/>
<br/>
<div style="height:200px;" data-bind="koGrid: gridOptions"></div>

于 2017-10-20T16:28:04.580 回答