Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click on checkbox in rows fires event bind on row knockout binding

I have a scenario where the Click event is bind to <tr> and checked value is bind to checkbox inside the <td? inside <tr> . Now the problem is When the row is clicked the event is fired thats fine . But in case of I click the checkbox the observable value changes for row as well and fires the row click event . How to avoid this scenario

<tbody data-bind="foreach:Mails">
    <tr data-bind="click:$root.navigateToMail">
        <td style="width: 15px">
            <input type="checkbox" data-bind="checked: isSelected">
            @*<input type="checkbox">*@
        </td>
        <td data-bind="text: From">
        </td>
        <td data-bind="text: To">
        </td>
        <td data-bind="text: Subject">
        </td>
        <td data-bind="text: MailDate">
        </td>
    </tr>
</tbody>

The events are :

 ko.utils.arrayForEach(data.mails, function (mail) {
                    mail.isSelected = ko.observable(false);
                    mail.isSelected.subscribe(function (myvalue) {
                        console.log(myvalue);
                    });
                });
 self.navigateToMail = function (mail) {
        location.hash = mail.Folder() + '/' + mail.Id();
    };

I trimmed of unnecessary codes . Just have put where the problem has occurred .

like image 593
Joy Avatar asked Jan 04 '13 12:01

Joy


1 Answers

You will essentially need to cancel the event bubbling on click.

Here's an example of how you can do that:

<div data-bind="click: parentClick">
    <input type="checkbox" data-bind="checked: isSelected, click: function(){return true}, clickBubble: false">
</div>​

See here: http://jsfiddle.net/2M9XP/1/

like image 171
badsyntax Avatar answered Sep 19 '22 09:09

badsyntax