我使用Kendo UI MVVM跟踪工作代码 - Fiddle
这里复选框的模板中有一个绑定
<input type="checkbox" name="selection" data-bind="checked: isChecked"/>
它与模型的isChecked
属性绑定。
现在,我需要在用户点击复选框时通过提醒已选中/未选中状态和用户名称来显示提醒。
我尝试使用data-bind =“checked:showAlert()”但是没有用。
我们如何实现这一目标?
BODY
<script id="selection-table-template" type="text/x-kendo-template">
<tr>
<td data-bind="text: name"></td>
<td data-bind="text: age"></td>
<td>
<input type="checkbox" name="selection" data-bind="checked: isChecked"/>
</td>
</tr>
</script>
<script id="row-template" type="text/x-kendo-template">
<tr data-bind="visible: isChecked">
<td data-bind="text: name"></td>
<td data-bind="text: age"></td>
</tr>
</script>
<table id="selectionTable">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody data-template="selection-table-template" data-bind="source: employees"/>
</table>
<br />
<hr />
<table id="resultTable">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody data-template="row-template" data-bind="source: employees"/>
</table>
的Javascript
var viewModel = kendo.observable({
employees: [
{ name: "Lijo", age: "28", isChecked: true },
{ name: "Binu", age: "33", isChecked: true },
{ name: "Kiran", age: "29", isChecked: true }
]
});
$(document).ready(function () {
kendo.bind($("body"), viewModel);
});
参考
答案 0 :(得分:5)
您可以对更改事件进行数据绑定:
HTML:
<input type="checkbox" name="selection"
data-bind="checked: isChecked, events: { change: printIsChecked }"/>
查看型号:
var viewModel = kendo.observable({
employees: [{
name: "Lijo",
age: "28",
isChecked: true
}, {
name: "Binu",
age: "33",
isChecked: true
}, {
name: "Kiran",
age: "29",
isChecked: true
}],
printIsChecked: function(e) {
$("#out2").html("via event-binding on input: " + e.data.name + " is checked: " + e.data.isChecked);
}
});
或将更改事件处理程序绑定到observable(不更改您的Html):
var viewModel = kendo.observable({
employees: [{
name: "Lijo",
age: "28",
isChecked: true
}, {
name: "Binu",
age: "33",
isChecked: true
}, {
name: "Kiran",
age: "29",
isChecked: true
}],
printIsChecked: function (e) {
var changedItem = e.items[0];
// note: might need to check e.field === "isChecked"
// if other fields might change or if you add/remove items from employees
$("#out").html("via Observable.change: " + changedItem.name + " is checked: " + changedItem.isChecked);
}
});
viewModel.employees.bind("change", viewModel.printIsChecked);
(demo)