如何在 kendo 网格中手动设置列值
how to manually set column value in kendo grid
我可以在控制台上设置备注,但我不知道如何在用户保存更改后将备注值设置到网格中。
是否可以手动将值设置到 kendo 网格中?
示例输出
|Remark |User Name|Phone Number|Country
[unable]|username length|ad |0123456789 |UK
[enable]| |admin2 |0123456222 |US
[enable]| |admin3 |0123333339 |CN
用户编辑后 table - 输出
|Remark |User Name|Phone Number|Country
[enable]| |admin1 |0123456789 |UK
[enable]| |admin2 |0123456222 |US
[enable]| |admin3 |0123333339 |CN
代码段中提供了项目示例。
var defaultData = [{
reason: "",
userName: "ad",
phoneNumber: "0123456789",
country: "UK"
}, {
reason: "",
userName: "admin2",
phoneNumber: "0123456222",
country: "US"
}, {
reason: "",
userName: "admin3",
phoneNumber: "0123333339",
country: "CN"
}];
var defaultColumns = [{
field: "",
width: "40px",
template: "<input name='Discontinued' id='remarkCheckBox' class='checkbox' #= (reason.length > 0)? 'disabled=disabled' : ''# type='checkbox' />"
}, {
field: "reason",
title: "Remark",
attributes: {
style: "color:\#cc0000"
}
}, {
field: "userName",
title: "User Name"
}, {
field: "phoneNumber",
title: "Phone Number"
}, {
field: "country",
title: "Country"
}];
var viewModel = kendo.observable({
onClick: function() {
loadImportGrid(defaultData, defaultColumns);
},
});
function loadImportGrid(defaultData, defaultColumns) {
var grid = $("#grid").kendoGrid({
columns: defaultColumns,
dataSource: {
data: defaultData
},
dataBound: function() {
grid = $("#grid").data("kendoGrid");
grid.saveChanges();
},
saveChanges: function() {
getRemark();
},
toolbar: ["save"],
selectable: "row",
scrollable: true,
sortable: true,
editable: true
});
}
function validation(objectList) {
if (!objectList.userName || objectList.userName.length < 4) {
invalidRecordFormat = "username length";
return invalidRecordFormat;
}
if (!objectList.country || objectList.country === " ") {
invalidRecordFormat = "country invalid";
return invalidRecordFormat;
}
invalidRecordFormat = "";
return invalidRecordFormat;
}
function getRemark() {
var data = $("#grid").data("kendoGrid").dataSource._data;
for (var i = 0; i < data.length; i++) {
console.log(validation(data[i]));
}
}
kendo.bind($("#importFile"), viewModel);
html * {
font-size: small;
font-family: Arial !important;
}
.uploadLabel {
color: white;
background-color: #008efe;
border-style: solid;
border-width: 1px 1px 1px 1px;
width: 100px;
height: 30px;
text-align: center;
border-radius: 3px;
display: block;
line-height: 250%;
}
#importUserFile {
opacity: 0;
position: absolute;
z-index: -1;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.914/styles/kendo.common-bootstrap.min.css" />
<link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.914/styles/kendo.bootstrap.min.css" />
<link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.914/styles/kendo.bootstrap.mobile.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2016.3.914/js/kendo.all.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2016.2.607/js/kendo.all.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="importFile">
<label class="uploadLabel" for="importUserFile">Browse</label>
<input name="file" id="importUserFile" data-bind="events:{click:onClick}" />
</div>
<div id="grid"></div>
</body>
</html>
根据验证是否正确,这就是您更新第一列值的方法。
for (var i = 0; i < data.length; i++) {
//access record using for loop - here i is value from loop
var firstItem = $('#grid').data().kendoGrid.dataSource.data()[i];
//set col reason value value
firstItem["reason"]="username length";
//refresh the grid
$('#grid').data('kendoGrid').refresh();
}
另一种方法是使用数据项设置方法
如本文所述post
dataItem.set() 方法非常慢,因为它每次都会触发一个事件。即使是 100 个列表也很慢。
要处理更大的更新然后使用
dataItem[field] = value
缺点是不会应用脏标记并且网格不会反映更改。
$('#grid').data('kendoGrid').refresh();
始终使用模型中的 set()
方法来更改值。不需要refresh()
,例如:
var dataItem = $("#grid").data("kendoGrid").dataSource.data()[0];
dataItem.set("reason", "new value");
我可以在控制台上设置备注,但我不知道如何在用户保存更改后将备注值设置到网格中。
是否可以手动将值设置到 kendo 网格中?
示例输出
|Remark |User Name|Phone Number|Country
[unable]|username length|ad |0123456789 |UK
[enable]| |admin2 |0123456222 |US
[enable]| |admin3 |0123333339 |CN
用户编辑后 table - 输出
|Remark |User Name|Phone Number|Country
[enable]| |admin1 |0123456789 |UK
[enable]| |admin2 |0123456222 |US
[enable]| |admin3 |0123333339 |CN
代码段中提供了项目示例。
var defaultData = [{
reason: "",
userName: "ad",
phoneNumber: "0123456789",
country: "UK"
}, {
reason: "",
userName: "admin2",
phoneNumber: "0123456222",
country: "US"
}, {
reason: "",
userName: "admin3",
phoneNumber: "0123333339",
country: "CN"
}];
var defaultColumns = [{
field: "",
width: "40px",
template: "<input name='Discontinued' id='remarkCheckBox' class='checkbox' #= (reason.length > 0)? 'disabled=disabled' : ''# type='checkbox' />"
}, {
field: "reason",
title: "Remark",
attributes: {
style: "color:\#cc0000"
}
}, {
field: "userName",
title: "User Name"
}, {
field: "phoneNumber",
title: "Phone Number"
}, {
field: "country",
title: "Country"
}];
var viewModel = kendo.observable({
onClick: function() {
loadImportGrid(defaultData, defaultColumns);
},
});
function loadImportGrid(defaultData, defaultColumns) {
var grid = $("#grid").kendoGrid({
columns: defaultColumns,
dataSource: {
data: defaultData
},
dataBound: function() {
grid = $("#grid").data("kendoGrid");
grid.saveChanges();
},
saveChanges: function() {
getRemark();
},
toolbar: ["save"],
selectable: "row",
scrollable: true,
sortable: true,
editable: true
});
}
function validation(objectList) {
if (!objectList.userName || objectList.userName.length < 4) {
invalidRecordFormat = "username length";
return invalidRecordFormat;
}
if (!objectList.country || objectList.country === " ") {
invalidRecordFormat = "country invalid";
return invalidRecordFormat;
}
invalidRecordFormat = "";
return invalidRecordFormat;
}
function getRemark() {
var data = $("#grid").data("kendoGrid").dataSource._data;
for (var i = 0; i < data.length; i++) {
console.log(validation(data[i]));
}
}
kendo.bind($("#importFile"), viewModel);
html * {
font-size: small;
font-family: Arial !important;
}
.uploadLabel {
color: white;
background-color: #008efe;
border-style: solid;
border-width: 1px 1px 1px 1px;
width: 100px;
height: 30px;
text-align: center;
border-radius: 3px;
display: block;
line-height: 250%;
}
#importUserFile {
opacity: 0;
position: absolute;
z-index: -1;
}
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.914/styles/kendo.common-bootstrap.min.css" />
<link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.914/styles/kendo.bootstrap.min.css" />
<link rel="stylesheet" href="//kendo.cdn.telerik.com/2016.3.914/styles/kendo.bootstrap.mobile.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2016.3.914/js/kendo.all.min.js"></script>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://kendo.cdn.telerik.com/2016.2.607/js/kendo.all.min.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<div id="importFile">
<label class="uploadLabel" for="importUserFile">Browse</label>
<input name="file" id="importUserFile" data-bind="events:{click:onClick}" />
</div>
<div id="grid"></div>
</body>
</html>
根据验证是否正确,这就是您更新第一列值的方法。
for (var i = 0; i < data.length; i++) {
//access record using for loop - here i is value from loop
var firstItem = $('#grid').data().kendoGrid.dataSource.data()[i];
//set col reason value value
firstItem["reason"]="username length";
//refresh the grid
$('#grid').data('kendoGrid').refresh();
}
另一种方法是使用数据项设置方法 如本文所述post
dataItem.set() 方法非常慢,因为它每次都会触发一个事件。即使是 100 个列表也很慢。
要处理更大的更新然后使用
dataItem[field] = value
缺点是不会应用脏标记并且网格不会反映更改。
$('#grid').data('kendoGrid').refresh();
始终使用模型中的 set()
方法来更改值。不需要refresh()
,例如:
var dataItem = $("#grid").data("kendoGrid").dataSource.data()[0];
dataItem.set("reason", "new value");