【发布时间】:2018-03-12 21:03:50
【问题描述】:
我有一个正在使用的 Telerik UI 网格,但我无法让它在复选框列中完全正常运行。
我想要将它绑定到数据集中的布尔字段。我想用复选框编辑这个项目。只要选择或取消选中复选框,我想在行上执行一些数学。
我搜索了 Stackexchange,发现了一些有用的代码位,但不是 100%。 I've created a JSFiddle to demonstrate the odd behavior.
网格定义如下:
<script type="text/x-kendo-template" id="checkboxTemplate">
<input type="checkbox" #=Discontinued ? checked="checked" : "" # class="chkbx" />
</script>
<div
id="LineItemsGrid"
data-role="grid"
data-editable="true"
data-bind="source: LineItems"
style="height: 470px"
data-navigatable="true"
data-columns="[
{ 'field': 'Discontinued', 'title':'<center>Discontinued</center>', 'width': 95, 'template': kendo.template($('#checkboxTemplate').html()) },
{ 'field': 'ProductName', 'title':'<center>Product Name</center>', 'width': 90 },
{ 'field': 'UnitPrice', 'title':'<center>Unit Price</center>', 'width': 90 },
{ 'field': 'UnitsInStock', 'title':'<center>Units In Stock</center>', 'width': 90 },
{ 'field': 'TotalInventory', 'title':'<center>Total Inventory</center>', 'width': 90 }
]">
</div>
没什么太花哨的。处理复选框的javascript在这里:
$(document).on('change', 'input:checkbox', function (e) {
var grid = $("#LineItemsGrid").data("kendoGrid");
var row = $(this).closest("tr");
var rowIdx = $("tr", grid.tbody).index(row);
var colIdx = $("td", row).index(this);
var checkval = $(this).prop("checked");
var items = LineItemsSource.data();
// set data - this causes the move to (0,0)
items[rowIdx].set("Discontinued", checkval);
if(checkval)
{
var total = items[rowIdx].get("UnitPrice") * items[rowIdx].get("UnitsInStock");
items[rowIdx].set("TotalInventory",total);
}
else
{
items[rowIdx].set("TotalInventory",0);
}
})
如果您单击复选框,当您 .set() 将值设置为复选框时,它会将插入符号移动到 (0,0)。当您单击复选框旁边的单元格时,它会将复选框向左移动一点并且不会选中它。如果您此时单击它,它会进行编辑并且不会移动到 (0,0)。
我绝对肯定有一种更优雅的方法可以做到这一点,但我没有找到任何线索。
【问题讨论】:
标签: checkbox mvvm telerik grid