淘汰赛:下拉列表中的每个选项中的表格

时间:2012-11-30 11:05:17

标签: javascript html knockout.js

我希望在<option> <select>中每个<span>都有两列的表格 我试过这个JSFIDDLE-source。但<span data-bind="text : countryName"></span> <span data-bind="text : selectedCountry"></span> 中的绑定无法正常工作

{{1}}

有什么想法吗?或另一种解决方案

修改
enter image description here

1 个答案:

答案 0 :(得分:1)

虽然您无法设定内容,但您可以使用类似Twitter的优秀Bootstrap框架来获得类似的效果。

http://jsfiddle.net/marrok/QdjPt/

<div class="btn-group">
  <a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
    <span class="name" data-bind="text: selectedItem().name"></span>
    <span class="desc" data-bind="text: selectedItem().desc"></span>    <span class="caret"></span>
  </a>

  <ul class="list dropdown-menu" data-bind="foreach: items ">
      <!-- I should be able to use 'click' here but I'm not sure why it doesn't work. -->
      <li class="item" data-bind="event: {mouseover : function(){$root.selectedIndex($index())}}">      
          <span class="name" data-bind="text: name"></span>
          <span class="desc" data-bind="text: desc"></span>
      </li>
  </ul>
</div>

使用Javascript:

var Thing = function(name, desc) {
    var self = this;
    self.name = ko.observable(name);
    self.desc = ko.observable(desc);
};

var AppModel = function() {
    var self = this;
    self.items = ko.observableArray([
        new Thing(1, "Thing One"),
        new Thing(2, "Thing Two"),
        new Thing(3, "Thing Three"),
        new Thing(4, "Thing Four")]);
    self.selectedIndex = ko.observable(0);
    self.selectedItem = ko.computed(function() {
        return self.items()[self.selectedIndex()];
    })
};

ko.applyBindings(new AppModel());​