KoGrid 行选择绑定

KoGrid Row Selection Binding

我正在使用 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 推送到数组吗?

在您的 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>