2017-03-24 81 views
0

我有一個多選元素 HTML:綁定淘汰賽不劍道多選工作

<li> 
     <select id="doc" data-role="multiselect" 
      data-placeholder="Choose...'" 
      data-value-primitive="true" 
      data-value-field="name" 
      data-text-field="name" 
      data-bind="value: sel, source: list"></select> 
     <hr/> 
     <span data-bind="text: sel"></span> 
    </li> 

我只在控制檯中看到的價值,而不是在跨度

回答

1

function tab() { 
 
    var tabs = ""; 
 

 
    for (var i = 0; i < stringify.level; i++) { 
 
     tabs += "\t"; 
 
    } 
 

 
    return tabs; 
 
    } 
 

 
function stringify(items) { 
 
    var item, 
 
    itemString, 
 
    levelString = ""; 
 

 
    for (var i = 0; i < items.length; i++) { 
 
    item = items[i]; 
 

 
    if (!item.items) { 
 
     itemString = kendo.stringify(item); 
 
    } else { 
 
     stringify.level++; 
 
     var subnodes = stringify(item.items); 
 
     stringify.level--; 
 

 
     delete item.items; 
 

 
     itemString = kendo.stringify(item); 
 

 
     itemString = itemString.substring(0, itemString.length - 1); 
 

 
     itemString += ",\"items\":[\r\n" + subnodes + tab() + "]}"; 
 
    } 
 

 
    levelString += tab() + itemString; 
 

 
    if (i != items.length - 1) { 
 
     levelString += ","; 
 
    } 
 

 
    levelString += "\r\n"; 
 
    } 
 

 
    return levelString; 
 
} 
 

 
stringify.level = 1; 
 

 
$(document).ready(function() { 
 
    var Node = kendo.data.Node; 
 
    var viewModel = kendo.observable({ 
 
    colors: [{ 
 
     name: "Red", 
 
     value: "#f00" 
 
     }, 
 
     { 
 
     name: "Green", 
 
     value: "#0f0" 
 
     }, 
 
     { 
 
     name: "Blue", 
 
     value: "#00f" 
 
     } 
 
    ], 
 
    multiSelectValue: [], 
 
    displayMultiSelectValue: function() { 
 
     var multiSelectValue = this.get("multiSelectValue"); 
 
     return kendo.stringify(multiSelectValue); 
 
    } 
 
    }); 
 

 
    kendo.bind($("div.demo-section"), viewModel); 
 
});
<script src="//kendo.cdn.telerik.com/2017.1.223/js/jquery.min.js"></script> 
 
<script src="//kendo.cdn.telerik.com/2017.1.223/js/kendo.all.min.js"></script> 
 
<link href="//kendo.cdn.telerik.com/2017.1.223/styles/kendo.material.mobile.min.css" rel="stylesheet" /> 
 
<link href="//kendo.cdn.telerik.com/2017.1.223/styles/kendo.material.min.css" rel="stylesheet" /> 
 
<link href="//kendo.cdn.telerik.com/2017.1.223/styles/kendo.common-material.min.css" rel="stylesheet" /> 
 
<div id="example"> 
 
    <div class="demo-section k-content wide"> 
 
    <h4>Current view model state</h4> 
 
    <pre class="prettyprint"> 
 
{ 
 
    multiSelectValue: <span data-bind="text: displayMultiSelectValue"></span> 
 
} 
 
       </pre> 
 
    </div> 
 
    <div class="demo-section k-content wide"> 
 

 
    <ul class="fieldlist"> 
 
     <li> 
 
     <h4>MultiSelect:</h4> 
 
     <select data-role="multiselect" multiple="multiple" data-placeholder="choose..." data-value-primitive="true" data-value-field="name" data-text-field="name" data-bind="source: colors, value: multiSelectValue"></select> 
 
     </li> 
 
     <li data-bind="text: multiSelectValue"></li> 
 
     <li>Should be above</li> 
 
    </ul> 
 
    </div> 
 
</div>

I edited the Kendo demo更像是你的代碼,它在span中顯示[object Object]

<ul class="fieldlist"> 
     <li> 
     <h4>MultiSelect:</h4> 
     <select data-role="multiselect" 
     multiple="multiple" 
     data-placeholder="choose..." 
     data-value-primitive="true" 
     data-value-field="name" 
     data-text-field="name" 
     data-bind="source: colors, value: multiSelectValue"></select> 
     </li> 
     <li data-bind="text: multiSelectValue"></li> 
     <li>Should be above</li> 
    </ul> 

的問題可能涉及到toString轉換你的value的。

+0

正確的答案,但我試圖不使用該庫。 我會使用kendo的MVVM淘汰賽 – Federico

+1

查看更新的答案。 –

+0

檢查你的鏈接,代碼是錯誤的 – Federico