KnockoutJS observableArray with template and foreach

落爺英雄遲暮 提交于 2019-12-11 19:48:48

问题


I'm trying to bind a Knockout observableArray to my UI using a foreach and checkboxes and then create an array based on what is checked.

I'm getting this error: Uncaught ReferenceError: Unable to process binding "template: function () . . . ."

Here is my HTML:

<dl data-bind="template: { name: 'QuarterTemplate', foreach: Quarter, templateOptions: { selections: SelectedQuarters } }"></dl>

<script id="QuarterTemplate" type="text/html">
<dd>
    <label>
        <input type="checkbox" data-bind="attr: { value: quarter }, checked: $item.selections" />
        <a data-bind="text: quarter" ></a>
    </label>
</dd>
</script>

Here is my Knockout ViewModel:

function ViewModel() {

this.Quarter = ko.observableArray([
    { quarter: "Q1" },
    { quarter: "Q2" },
    { quarter: "Q3" },
    { quarter: "Q4" }
]);

this.SelectedQuarters = ko.observableArray();

this.SelectedQuarters.subscribe(function () {
    console.log(this.SelectedQuarters());
});

}

$(document).ready(function () {

    ko.applyBindings(new ViewModel());

});

I also have a fiddle set up:

http://jsfiddle.net/SpRLP/1/

Ultimately what I want to see in the console is something like this:

Q1

Q1,Q3

Q1,Q3,Q2

Q1,Q3,Q2,Q4

Q1,Q2,Q4


回答1:


templateOptions is only available when using the jQuery Templates plugin. When using KO native templating, it is most common to use $root or $parent to bind in this way. Here is some documentation on these context variables.

So, it would look like:

<dl data-bind="template: { name: 'QuarterTemplate', foreach: Quarter }"></dl>

<script id="QuarterTemplate" type="text/html">
    <dd>
        <label>
            <input type="checkbox" data-bind="attr: { value: quarter }, checked: $parent.SelectedQuarters" />
            <a data-bind="text: quarter" ></a>
            </label>
</dd>
</script>

Here is an updated fiddle: http://jsfiddle.net/rniemeyer/tY5TF/




回答2:


Thanks. This helped me get my ObservableArray checkboxes working. I adapted my code to your example, and have more information in the JS 'class' that you can show.

// Define a "Quarter" class
function Quarter(id, name) {
    return {
        id : ko.observable(id),
        name : ko.observable(name)
    };
}

var viewModel = {
    quarters : ko.observableArray([
    new Quarter("Q1", "First Quarter"),
    new Quarter("Q2", "Second Quarter"),
    new Quarter("Q3", "Third Quarter"),
    new Quarter("Q4", "Fourth Quarter"),
    ]),
    
    selectedQuarters : ko.observableArray(["Q1", "Q3"])
};

ko.applyBindings(viewModel);
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script>
<ul data-bind="template: { name: 'QuarterTemplate', foreach: quarters }"></ul>

<script id="QuarterTemplate" type="text/html">
    <li>
        <input type="checkbox" data-bind="checkedValue: id, checked: $parent.selectedQuarters" />
        <span data-bind="text: name"></span>
    </li>
</script>

<pre data-bind="text: ko.toJSON(selectedQuarters, null, 2)"></pre>


来源:https://stackoverflow.com/questions/22512290/knockoutjs-observablearray-with-template-and-foreach

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!