我正在使用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调用?
或者我不能这样做,必须使用afterSelectionChange
选项手动将所选的ID推送到数组吗?
答案 0 :(得分:2)
在gridOptions
中,您传递了对mySelectedData
可观察数组的引用,以用作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>
&#13;