我有一个带有复选框列的kendo网格。我在检查相应的复选框时一直试图获取dataitem行。单击一个按钮,我只需要获取JSon中的已检查行。这是我一直在玩的JSfiddle。它只获取Id而不是行数据。我一直在尝试修改,以便它可以返回整个行数据。
var crudServiceBaseUrl = "http://demos.kendoui.com/service",
dataSource = new kendo.data.DataSource({
transport: {
read: {
url: crudServiceBaseUrl + "/Products",
dataType: "jsonp"
},
update: {
url: crudServiceBaseUrl + "/Products/Update",
dataType: "jsonp"
},
destroy: {
url: crudServiceBaseUrl + "/Products/Destroy",
dataType: "jsonp"
},
create: {
url: crudServiceBaseUrl + "/Products/Create",
dataType: "jsonp"
},
parameterMap: function (options, operation) {
if (operation !== "read" && options.models) {
return {
models: kendo.stringify(options.models)
};
}
}
},
batch: true,
pageSize: 20,
schema: {
model: {
id: "ProductID",
fields: {
ProductID: {
editable: false,
nullable: true
},
ProductName: {
validation: {
required: true
}
},
UnitPrice: {
type: "number",
validation: {
required: true,
min: 1
}
},
Discontinued: {
type: "boolean"
},
UnitsInStock: {
type: "number",
validation: {
min: 0,
required: true
}
}
}
}
}
});
//Grid definition
var grid = $("#grid").kendoGrid({
dataSource: dataSource,
pageable: true,
height: 430,
//define dataBound event handler
toolbar: ["create"],
columns: [
//define template column with checkbox and attach click event handler
{ template: "<input type='checkbox' class='checkbox' />" },
"ProductName", {
field: "UnitPrice",
title: "Unit Price",
format: "{0:c}",
width: "100px"
}, {
field: "UnitsInStock",
title: "Units In Stock",
width: "100px"
}, {
field: "Discontinued",
width: "100px"
}, {
command: ["edit", "destroy"],
title: " ",
width: "172px"
}
],
editable: "inline"
}).data("kendoGrid");
//bind click event to the checkbox
grid.table.on("change", ".checkbox" , selectRow);
$("#showSelection").bind("click", function () {
var items = [];
for(var i in checkedrows){
if(checkedrows[i]){
items.push([i]);
}
}
alert(JSON.stringify(itemss));
});
});
var checkedrows = {};
var itemss =[];
//on click of the checkbox:
function selectRow() {
var checked = this.checked,
row = $(this).closest("tr"),
grid = $("#grid").data("kendoGrid"),
dItem = grid.dataItem(row);
checkedrows[dItem.id] = checked;
if (checked) {
itemss.push(dItem)
//-select the row
}
else
{
itemss.pop(dataItem);
}
}
可以通过dataItem访问行get,但是如何根据索引获取dataItem。谢谢。
答案 0 :(得分:8)
要获得选中的复选框,我使用此
var grid = $("#Grid").data("kendoGrid");
grid.tbody.find("input:checked").closest("tr").each(function (index) {
whatever you need done.
});
答案 1 :(得分:1)
您可以按uid
存储所选项目,然后在需要时从数据源获取。
选择处理程序:
function selectRow() {
var checked = this.checked,
row = $(this).closest("tr"),
grid = $("#grid").data("kendoGrid"),
dataItem = grid.dataItem(row);
checkedrows[dataItem.uid] = checked;
}
获取序列化的项目数组:
function getSerializedSelectedRows() {
var items = [],
item,
grid = $("#grid").data("kendoGrid");
for (var uid in checkedrows) {
if (checkedrows.hasOwnProperty(uid)) {
if (checkedrows[uid]) {
item = grid.dataSource.getByUid(uid);
items.push(item);
}
}
}
return JSON.stringify(items);
}
(demo)
答案 2 :(得分:1)
2014年接受的答案是正确的,但现在api有所改善。您可以使用以下代码段来获取数据项。请注意,也将选中已检查的行,从而从grid.select()调用返回。
//get the grid
var grid = $("#grid").data("kendoGrid");
// array to store the dataItems
var selected = [];
//get all selected rows (those which have the checkbox checked)
grid.select().each(function(){
//push the dataItem into the array
selected.push(grid.dataItem(this));
});
答案 3 :(得分:0)
您可以查看
var gridData2 = $("#CustomerAccountManagerKendoGrid2").data("kendoGrid").dataSource.data();
var gEmpID2 = GetSelectedEmpID(gridData2);
function GetSelectedEmpID(gridData) {
for (var i = 0; i < gridData.length; i++) {
if (gridData[i].SelectStatus == true) {
return gridData[i].GEmployeeGenInfoID;
}
}
}
您还可以看到this