LCJ
LCJ

Reputation: 22652

Kendo-UI data-bind to function is not working

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?

enter image description here

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

  1. How to display only selected records in the result table
  2. MVVM / Custom binding
  3. MVVM / Event binding
  4. Kendo MVVM Overview
  5. Value binding

Upvotes: 3

Views: 10044

Answers (1)

Lars H&#246;ppner
Lars H&#246;ppner

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

Related Questions