Reputation: 22652
I have following working code using Kendo UI MVVM - Fiddle
Here there is a binding in the template for the checkbox
<input type="checkbox" name="selection" data-bind="checked: isChecked"/>
It is bound to isChecked
property of the model.
Now I need to show an alert when user clicks on the checkbox by alerting the checked / unchecked state and Name of user.
I tried with data-bind="checked: showAlert()" but that didnt work.
How can we achieve this?
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);
});
REFERENCES
Upvotes: 3
Views: 10044
Reputation: 18402
You can either data-bind the change event:
Html:
<input type="checkbox" name="selection"
data-bind="checked: isChecked, events: { change: printIsChecked }"/>
View model:
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);
}
});
or bind a change event handler to the observable (without changing your 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)
Upvotes: 5