当然,Jasmine,这对我有用。我在页面上有多个网格,并且有一个硬编码的用户 ID 需要
被替换,但这应该给其他人一个开始......
var gridResult = $("#1f2e9791-21e1-4d9a-8b9a-4b08a99fee11 .grid").kendoGrid({
dataSource: {
type: "json",
transport: {
read:
{
//call api for data
url: 'http://localhost:61242/api/meds'
}
},
schema: {
model: {
//define schema fields
fields: {
Date: { type: "date" },
Medicine: { type: "string" },
Dosage: { type: "string" }
}
}
},
},
reorderable: true,
sortable: true,
pageable: false,
resizable: true,
//dynamically get the height from the outer container (using gridster)
height: $("#1f2e9791-21e1-4d9a-8b9a-4b08a99fee11").height() - 30,
scrollable: true,
columnReorder: function (e) {
var that = this;
setTimeout(function () {
//save columns on reorder
SaveColsGrid1(kendo.stringify(that.columns));
}, 5);
},
dataBound: function (e) {
//retrieve columns
GetColumns('1f2e9791-21e1-4d9a-8b9a-4b08a99fee11');
}
});
save 方法,它发布一个 MVC 我的 MVC 控制器
function SaveColsGrid1(data) {
$.ajax({
url: '@Url.Action("SaveColsGrid1")',
type: 'post',
contentType: 'application/json',
data: data
});
}
检索列并重新排序
function GetColumns(gridId) {
var sessionId = $("#inSession").val();
$.ajax({
url: 'http://localhost:61242/api/usercolumn/1|' + gridId,
type: 'GET',
contentType: 'application/json',
data: gridId,
async: false,
success: function (data) {
if (data && data.length > 0) {
var classname = "#" + gridId + " .grid"
var realGrid = $(classname).data("kendoGrid");
var visibleIndex = -1;
for (var i = 0; i < data.length; i++) {
var column = data[i];
// 1. Set correct order first as visibility and width both depend on this.
var existingIndex = -1;
if (typeof column.Field !== 'undefined') {
existingIndex = findFieldIndex(realGrid, column.Field);
}
else if (typeof column.commandName !== 'undefined') {
existingIndex = findCommandIndex(realGrid, column.commandName);
}
if (existingIndex > -1 && existingIndex != i) // Different index
{ // Need to reorder
//alert('hi2');
realGrid.reorderColumn(i, realGrid.columns[existingIndex]);
}
// 2. Set visibility state
var isHidden = (typeof column.hidden === 'undefined') ? false : column.hidden;
if (isHidden) {
realGrid.hideColumn(i);
}
else {
realGrid.showColumn(i);
++visibleIndex;
}
// 3. Set width
var width = (typeof column.width === 'undefined') ? null : column.width;
if (width != null) {
realGrid.columns[i].width = width; // This sets value, whilst rest redraws
realGrid.thead.prev().find('col:eq(' + visibleIndex + ')').width(width);
realGrid.table.find('>colgroup col:eq(' + visibleIndex + ')').width(width);
}
}
}
},
error: function () {
alert('Error occured');
},
});
}
//get column index of the existing item
function findFieldIndex(realGrid, field) {
var existingIndex = -1;
for (var i = 0; i < realGrid.columns.length; i++) {
if (realGrid.columns[i].field == field) {
existingIndex = i;
break;
}
}
return existingIndex;
}
function findCommandIndex(realGrid, commandName) {
//var existingIndex = -1;
//for(var idx = 0; idx < realGrid.columns.length; ++idx)
//{
// if(typeof realGrid.columns[idx].command !== 'undefined'
// && realGrid.columns[idx].command.name == commandName)
// {
// existingIndex = idx;
// break;
// }
//}
//return existingIndex;
}
});