1
votes

I have a array structure defined in viewModel which i am binding it to view. The code for view is:

<div id="configurepopup">
        <div>
            <fieldset>
                <legend></legend>
                <div>
                    <label>
                        <b>Show Columns</b></label></div>
                <br />
                <ul data-bind="foreach: gridOptions.columns">
                    <li>
                        <label data-bind="attr:{ 'for': 'chk' + $index()}">
                        </label>
                        <input type="checkbox" data-bind="attr:{'id':'chk' + $index(), checked: checked}" /><span data-bind="text:header"></span></li>
                </ul>
            </fieldset>
        </div>
    </div>

ViewModel:

function ProductStandardSearchVM() {
    var self = this;
    self.gridOptions = {
        columns: [{
            header: 'Catalog Number',
            dataMember: 'productName',
            checked: ko.observable(true)
        }, {
            header: 'Description',
            dataMember: 'shortDesc',
            checked: ko.observable(true)
        }, {
            header: 'List Price',
            dataMember: 'listprice',
            checked: ko.observable(true)
        }, {
            header: 'Quantity',
            dataMember: 'minOrdQty',
            checked: ko.observable(true)
        },
        {
            header: 'Old Material No',
            dataMember: 'oldMatNum',
            checked: ko.observable(true)
        }, {
            header: 'View',
            dataMember: 'view',
            checked: ko.observable(true)
        }]
    };
};
var test= new ProductStandardSearchVM();
ko.applyBindings(test, document.getElementById("configurepopup"));

The jsfiddle for the same: http://jsfiddle.net/ynV35/3/

jsfiddle works perfectly fine. I am doing the same in my project as well, i get a reference error:

Uncaught Error: Unable to parse bindings. Message: ReferenceError: $index is not defined; Bindings value: attr:{'id':'chk' + $index()}

I tried changing the knockout with the latest version, but not able to solve. Any help would be appreciated.

Thanks in advance.

2
Please post your actual code! Are you sure that your binding using the $index is inside in a forach binding? Do you have a valid html markup, are your html tags are properly closed? - nemesv
Yes, i have posted the actual code. i checked with html tags, every tag is closed properly. - user2439903

2 Answers

1
votes

Did you try to use $index instead of $index()?

Also please look on this post

0
votes

$index is introduced in Knockout as mentioned in the KnockoutJS documentation