我需要使用kendo网格来编辑数据。问题是返回响应中的每一项都是字符串,但包含其他类型的值(例如value = "true“或Value = "32%”或Value = "0:standard,1:advanced")。
因此,我需要在网格上设置模板,以对应字符串中的不同数据类型。因此,对于true/false,我必须有复选框,对于32%,它应该提供文本框,但是对于验证百分比,对于数组响应,它需要一个下拉列表。
我成功地使用编辑器设置了下拉和文本框选项,但我无法使复选框以任何方式正确处理。复选框按预期显示,但无论我尝试什么,都不会在保存网格后将数据绑定到网格。(无论值如何,始终不检查它)
下面是列"value“的代码片段,我使用的模板(item.type === "3”是布尔型)。
field: 'value',
title: 'value',
headerAttributes: {
'class': 'table-header-cell'
},
template: function (item) {
if (item.type === "3") {
var boolValue = (/true/i).test(item.value);
item.value = boolValue;
return '<input id="' + item.name+ '" type="checkbox" #= value ? \'checked="checked"\' : "" # class="chkbx" />';
} else {
return ''; //this will follow for other types
}
},
提前谢谢。
发布于 2018-06-19 09:29:05
当模板定义是函数时,您不需要使用#标记来区分标记和javascript,就像使用kendo的模板语言或字符串直接定义kendo模板时一样。
这是因为函数内部始终是javascript,而#标记仅是kendo模板语言中的指令。
因此,将模板简化为:
template: function (item) {
return '<input class="chkbx" id="' + item.name + '" type="checkbox"' + (item.value ? 'checked="checked"' : '') + '/>';
}
为了简单起见,我忽略了其他数据类型的处理。
然后,您需要添加代码来将复选框更改推入网格的数据源:
$("#grid").on("click", ".chkbx", function () {
var $checkBox = $(this),
checked = $checkBox.is(":checked"),
dataItem = grid.dataItem($checkBox.closest("tr"));
dataItem.set("value", checked);
});
这是我目前在生产代码中使用的一种技术。
还可以使用模板中的kendo绑定,以获得更优雅的解决方案,而不是显式的单击处理程序,但为了解决这个问题,我需要进行更多的实验。
https://stackoverflow.com/questions/50932140
复制相似问题