jeudi 31 mars 2016

Getting information out of an array knockout js

I have a foreach loop from where I get cash and orig id . I am using a checkbox after the p tags and the checkbox only returns true or false.

<div data-bind="foreach : info">
<p data-bind="$data.cash"></p>
<p data-bind="$data.orig_id"></p>
<input type="checkbox"  data-bind="Switch: $root.on_off"/>
</div>

What I want to do is use the checkbox to change something in the database, so basically I need to get the orig_id of that checkbox. so I was thinking maybe if I add click binding it might give me the orig_id of which every array I get from the for each function, but did not work ofcourse. So my question is how can I get the orig_id each time the person clicks the switch box.

I tried doing something like this on the js, so I can get the orig ID from the checkbox input field.

self.sendCheckBoxInfo = function( data, event){
            alert(data.orig_id);
        }

<div data-bind="foreach : info">
<p data-bind="$data.cash"></p>
<p data-bind="$data.orig_id"></p>
<input type="checkbox"  data-bind="Switch: $root.on_off, click :    $root.sendCheckBoxInfo"  />

IF needed here is the Switch databind code

ko.bindingHandlers.Switch = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        $elem = $(element);
        $(element).bootstrapSwitch();
        $(element).bootstrapSwitch('setState', ko.utils.unwrapObservable(valueAccessor())); // Set intial state
        $elem.on('switch-change', function (e, data) {
            valueAccessor()(data.value);
        }); // Update the model when changed.
    },
    update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var vStatus = $(element).bootstrapSwitch('state');
        var vmStatus = ko.utils.unwrapObservable(valueAccessor());
        if (vStatus != vmStatus) {
            $(element).bootstrapSwitch('setState', vmStatus);
        }
    }




Aucun commentaire:

Enregistrer un commentaire