2017-10-20 96 views
0

我使用KoGrid插件有一個選擇複選框的挖空網格。到目前爲止,我有以下代碼。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推送到一個數組?

回答

2

在你gridOptions你傳遞的引用,你mySelectedData觀察到數組作爲SelectedItems使用,所以你應該能夠使用原來的mySelectedData變量,而不是SelectedItems;它們是等價的。當網格選擇改變時,將更新mySelectedData

function columnDefsVM() { 
 
    var self = this; 
 
    this.myData = ko.observableArray([ 
 
    \t {TestEventId: 1, Name: 'Object 1'}, 
 
    \t {TestEventId: 2, Name: 'Object 2'}, 
 
    \t {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/[email protected]/koGrid-2.0.6.debug.js"></script> 
 
<link href="https://cdn.jsdelivr.net/gh/Knockout-Contrib/[email protected]/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>