2017-08-04 63 views
0

幫助請幫助。我有22個物體,5個父母和17個孩子。 我無法得到ui li的孩子。使用數據綁定 敲出js。僅顯示5位父母。提前致謝。獲取列表的孩子ui li dropdown knockout.js數據綁定

enter image description here

enter image description here

Index.js

self._routes = ko.observableArray([]); 
    self._child = ko.observableArray([]); 
    for (var i = 0; i < backend.user().UserScreens.length; i++) { 
     var str = backend.user().UserScreens[i].addOn; //add icon 
     str = str.substring(0, str.length - 1); //trim last character ',' 
     if (backend.user().UserScreens[i].isParent || backend.user().UserScreens[i] == null) { 
      self._routes.push({ 
       route: backend.user().UserScreens[i].linkURL, 
       title: backend.user().UserScreens[i].description, 
       moduleId: 'modules/' + backend.user().UserScreens[i].linkURL.replace(/[\n\r]+/g, '').replace(/\s{2,10}/g, ' ') + '/index', 
       nav: true, 
       _icon: 'fa ' + str, 
       _id: '#' + backend.user().UserScreens[i].id 
      }); 
     } 
     else { 
      self._child.push({ 
       childRoute: backend.user().UserScreens[i].linkURL, 
       childtitle: backend.user().UserScreens[i].description, 
       childmoduleId: 'modules/' + backend.user().UserScreens[i].linkURL.replace(/[\n\r]+/g, '').replace(/\s{2,10}/g, ' ') + '/index', 
       childNav: true, 
       childIcon: 'fa ' + str, 
       childId: '#' + backend.user().UserScreens[i].id 
      }); 
     } 
    } 

的Index.html

<ul class="nav"> 
       <li data-bind="foreach: _routes"> 
        <a data-bind="attr: { href: _id }"><span data-bind="attr: {class: _icon}"></span> 
         <span class="sidebar-title" data-bind="text: title"></span><span class="caret"></span> 
        </a> 
        <ul data-bind="foreach: _child, attr: {id: childId}" class="nav sub-nav"> 
         <li><a href="#" data-bind="attr: {href: '#' + childRoute}"> 
          <span data-bind="attr: {class: _icon}"></span> 
          <span data-bind="text: childtitle"></span></a> 
         </li> 
        </ul> 
       </li> 
      </ul> 

回答

0

你的模型顯示_routes_child在同一水平這就是爲什麼。

self._routes = ko.observableArray([]); 
self._child = ko.observableArray([]); 

因此您的child的foreach將翻倒的child屬性不存在。該解決將是從路線的foreach刪除child或添加$parent

<ul class="nav"> 
    <li data-bind="foreach: _routes"> 
     <a data-bind="attr: { href: _id }"><span data-bind="attr: {class: _icon}"></span> 
      <span class="sidebar-title" data-bind="text: title"></span><span class="caret"></span> 
     </a> 
     <!-- ko foreach: $parent._child --> 
     <ul data-bind="attr: {id: childId}" class="nav sub-nav"> 
      <li><a href="#" data-bind="attr: {href: '#' + childRoute}"> 
       <span data-bind="attr: {class: _icon}"></span> 
       <span data-bind="text: childtitle"></span></a> 
      </li> 
     </ul> 
     <!-- /ko --> 
    </li> 
</ul> 

但是,這將顯示所有兒童的foreach路線,但我不這麼看這條路線與孩子之間的任何相關性可能是你尋找的東西。

+0

謝謝先生。新手在knockout.js。 :) –