2016-07-26 13 views
0

ドロップダウンメニューバーを作成しようとしています。クリックすると、ドロップダウンが表示され、アイコンが右矢印から下矢印に変わります。しかし、Knockout JSはバインディング内のspanタグを削除し続けます。どうすればこの問題を回避できますか?アイコンを削除せずにアイコンをバインディングに追加するにはどうすればよいですか?

これは私のhtmlです:

<!-- Recursively traverse the nested structure --> 
<ul data-bind="template: {name: 'document_index_template', foreach: children}"><l><a href="#">Document Index</a></l></ul> 

<script type="text/html" id="document_index_template"> 
    <li class="collapsible-child" > 
     <a data-bind="text: label, click: function(){isExpanded(!isExpanded())}" href="#"><span class="glyphicon glyphicon-triangle-right" ></span></a> 

     <ul class="collapsible-child" data-bind='template: { name: "document_index_template", foreach: visibleChildren}'></ul> 
    </li> 
</script> 

そして、これは私のViewModelです:

var document_type = 'loan'; 
var key = 'comparison'; 
define(['jquery', 'knockout'], function($, ko){ 

    var structureRequest = getStructure(); 
    structureRequest.then(function(data){ 
     window.treeNode = new TreeNode(data); 
     ko.applyBindings(window.treeNode); 
    }); 

    function TreeNode(data){ 
     var self = this; 

     self.key = ko.observable(data.key); 
     self.label = ko.observable(data.label); 
     self.children = ko.observableArray([]); 

     $.each(data.children, function(index, child){ 
      self.children.push(new TreeNode(child)); 
     }); 

     self.isExpanded = ko.observable(false); 

     self.visibleChildren = ko.computed(function(){ 
      if(self.isExpanded()){ 
       return self.children(); 
      }else{ 
       return []; 
      } 
     }); 
    } 

    function getStructure() { 
     var url = "../structure/api/0?document_type=" + document_type + "&key=" + key; 
     return $.ajax({ 
      url: url, 
      type: "GET", 
      dataType: "json" 
     }); 
    } 
}); 

答えて

1

textデータバインドは、ノードをきれいにし、テキストノードを挿入します。

<a data-bind="click: function(){ isExpanded(!isExpanded()) }" href="#"> 
    <span data-bind="text: label"></span> 
    <span class="glyphicon glyphicon-triangle-right"></span> 
</a> 

を参考のために:あなたは、テキスト結合する1つのレベルを下に移動する必要がありますテキストノード:私たちは、正確に一人の子供があることが必要

:ここでノックアウト源からのコメントです。子ノードがない場合、複数のノードがある場合、またはノードがテキストノードでない場合は、すべてをクリアして1つのテキストノードを作成します。

https://github.com/knockout/knockout/blob/master/src/utils.js#L433

関連する問題