knockout.js - Initializing a Bootstrap accordion menu in Knockout -


here example of accordion menu should should use data in format compatible json. have data structure, binding between data , html doesn't work. corrections should make display multilevel structure in 2-level menu?

var confitems = {    "children":ko.observablearray()  };  var childrenlength = 3;  console.log("a",confitems);  (var = 0; < childrenlength; i++) {    confitems.children.push({      "idhash": ko.observable("#col-" + (i + 1)),      "id": ko.observable("col-"+ (i + 1)),      "displaylabel": ko.observable("item " + (i + 1)),      "children": ko.observablearray()    });    console.log("b",confitems);    (var j = 0; j < childrenlength; j++) {      confitems.children()[i].children().push({        "idhash": ko.observable("#col-" + (i + 1) + "-" + (j + 1)),        "id": ko.observable("col-" + (i + 1) + "-" + (j + 1)),        "displaylabel": ko.observable("item " + (i + 1) + "." + (j + 1)),        "children": ko.observablearray()      });      console.log("c",confitems);    }    console.log("d",confitems);  }  console.log(confitems);  var viewmodel = function() {    this.tree = ko.observable(confitems);  };  ko.applybindings(new viewmodel());
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">  <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>  <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>    <div class="panel-group">    <div id="accordion" class="panel panel-default" data-bind="foreach: { data: tree.children, as: child}">      <div class="panel-heading">        <h4 class="panel-title">  									<a data-toggle="collapse" data-parent="#accordion"  										data-bind="text:child.displaylabel, attr:{href:child.idhash}">first level</a>  								</h4>      </div>      <div data-bind="attr:{id:child.id}" class="panel-collapse collapse">        <div class="panel-body" data-bind="foreach: { data: child.children, as: child}">          <div class="panel-heading">            <a data-bind="text:child.displaylabel">second level</a>          </div>        </div>      </div>    </div>  </div>

you should:

  1. separate foreach arguments commas , @roy j wrote below, child should in quotes like: "foreach: { data: tree.children, as: 'child'}" instead of foreach: { data: tree.children as: child}

  2. change this.tree = ko.observable(confitems); this.tree = confitems;

check fiddle - fiddle


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -