How to bind radio button in knockoutjs from object properties

303 views Asked by At

I have a list of object in witch I want the object to be selected as part of the radio button. I am using knockout but not to sure where I am wrong with this

Here my javascript

function Convocation(sessionID, description, defValue) {

    var self = this;
    this.ConvocationID = ko.observable(sessionID);
    this.ConvDesc = ko.observable(description);
    this.isDefault = ko.observable();
    if (defValue == true) {
        self.isDefault(sessionID);
    }
    this.changeDefault = function (value) {
        if (value == true) {
            self.isDefault(self.ConvocationID());
        } else {
            self.isDefault();
        }
    }
}

function ViewModel() {
    var self = this;

    this.Convocations = ko.observableArray();
    this.radioSelected = ko.observable();

    this.updateRadio = function () {
        self.radioSelected(this.ConvocationID());
    }

    // Get information
    this.Initialize = function () {
        $.ajax({
            url: "/Convocations/GetConvocationList",
            dataType: 'json',
            //data: { id: id },
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    self.Convocations.push(new Convocation(data[i].sessionCode, data[i].desc, data[i].isDefault));
                }
            }
        });
    }
}

And this is my html

                <tbody data-bind="foreach: Convocations">
                    <tr>
                        <td><input type="radio" name="default" data-bind="value: $data.ConvocationID, checked:$parent.radioSelected, click: $parent.updateRadio" /></td>
                        <td><span data-bind="text: $data.ConvocationID"></span></td>
                        <td><span data-bind="text: $data.ConvDesc"></span></td>
                    </tr>
                </tbody>

I am just curious how to bind the radio button to the property of each convocations

0

There are 0 answers