Click on checkbox in rows fires event bind on row knockout binding
Asked Answered
G

1

13

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 .

Gehenna answered 4/1, 2013 at 12:47 Comment(0)
P
25

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/

Prae answered 4/1, 2013 at 14:31 Comment(2)
Thanks! I was trying it with the clickBubble:false, but I didn't realize I had to explicitly handle the click event. Sort of thought the checked binding would cover it.Evangelize
I've been triying to fix this for hours. Thanks a lot!Pedometer

© 2022 - 2024 — McMap. All rights reserved.