我正在尝试获取kendo网格中所选行的行值。请参阅demo here。例如,在按钮单击时,我希望将所选行的所有名称或ID作为数组或逗号分隔的字符串。我怎样才能做到这一点?感谢。
剑道网格
$('#getNames').click(function(){
//how to get all the names of selected rows?
alert('you have selected these people:')
});
$("#grid").kendoGrid({
dataSource: {
type: "odata",
transport: {
read: "http://demos.telerik.com/kendo-ui/service/Northwind.svc/Customers"
},
pageSize: 10
},
groupable: true,
sortable: true,
pageable: {
refresh: true,
pageSizes: true,
buttonCount: 5
},
columns: [
{
field: "", title: "", template: "<input type='checkbox' name='selectcandidate' />"
},
{
field: "ContactName",
title: "Contact Name",
width: 200
}, {
field: "ContactTitle",
title: "Contact Title",
width: 250
}, {
field: "CompanyName",
title: "Company Name"
}, {
field: "Country",
width: 150
}]
});
答案 0 :(得分:9)
你应该这样做:
// Get reference to Grid object
var grid = $("#grid").data("kendoGrid");
// Get selected rows
var sel = grid.select();
// Get data item for each
var items = [];
$.each (sel, function(idx, row) {
var item = grid.dataItem(row);
items.push(item);
});
// Show results
console.log("items", items);
唯一的问题是,您需要一次检索一个数据项,然后根据需要构建一个包含所有数据的数组。
在此处查看:http://jsfiddle.net/OnaBai/XNcmt/71/
编辑如果您想获取点击复选框的那些行,那么您应该这样做:
// Get reference to Grid object
var grid = $("#grid").data("kendoGrid");
// Get selected rows
var sel = $("input:checked", grid.tbody).closest("tr");
// Get data item for each
var items = [];
$.each (sel, function(idx, row) {
var item = grid.dataItem(row);
items.push(item);
});
alert ("selected: " + JSON.stringify(items));