2012-07-23 54 views
0

有一个奇怪的问题,一切正常,直到我指定optionsValue。你可以看到我的问题在这里设置Knockoutjs级联下拉菜单中的OptionValue

工作 http://jsfiddle.net/abritez/ttyhE/2/

不起作用 http://jsfiddle.net/abritez/ttyhE/4/

所有你需要做的就是尽量与选择运行它认为是评论,而你尝试级联时会看到问题。

JSON数据

var productCategories = [{ 
"name": "Comapany A", 
"abbr": "cA", 
"disiplineList": [{ 
    "name": "Math", 
    "abbr": "math", 
    "courseList": [{ 
     "name": "Algebra", 
     "abbr": "alg" 
    }] 
}, { 
    "name": "English", 
    "abbr": "eng", 
    "courseList": [{ 
     "name": "Phonics", 
     "abbr": "phon" 
    }] 
}] 
}, { 
"name": "Company B", 
"abbr": "cB", 
"disiplineList": [{ 
    "name": "Gym", 
    "abbr": "gym", 
    "courseList": [{ 
     "name": "Kick Ball", 
     "abbr": "kb" 
    }] 
}] 
}]; 

HTML代码

<table width='100%'> 
<tbody data-bind='foreach: lines'> 
    <tr> 
     <td> 

      <select data-bind='options: productCategories, optionsText: "name", optionsCaption: "Select...", value: company'> </select> 

     </td> 
    </tr> 
    <tr> 
     <td data-bind="with: company"> 
      <select data-bind='options: disiplineList, optionsText: "name", optionsCaption: "Select...", value: disipline'> </select> 
     </td> 
    </tr> 
    <tr> 
     <td data-bind="with: disipline"> 
      <select data-bind='options: courseList, optionsText: "name", value: product'></select> 
     </td> 
    </tr> 

</tbody> 
</table> 

Java脚本(knockoutjs)

function ProductLine(){ 
        self.company = ko.observable(); 
        self.disipline = ko.observable(); 
        self.product = ko.observable(); 

        // Whenever the category changes, reset the product selection 
        self.company.subscribe(function() { 
         self.disipline(undefined); 
         self.product(undefined); 
        }); 
       } 

       function Product(){ 
        self.lines = ko.observableArray([new ProductLine()]); // Put one line in by default 
       } 

       ko.applyBindings(new Product());​ 

+0

不使用* optionsValue *他们的使用是尴尬的,不是很有价值..你不需要它。告诉我们你真正的问题。 * optionsValue *仅用于数字,当您使用observableArray构建* select *时。像这样:[链接](http://jsfiddle.net/rniemeyer/QKLpe/) – 2012-07-23 18:41:36

+0

所有我想要做的是有价值是文本的缩写。因此,例如,“美国”将具有“美国”的价值,并且“纽约市”将具有“美国联邦”的价值。 – abritez 2012-07-23 19:04:57

回答

1

我仍然不明白这一点。

但是,如果你想显示的姓名和简称 U可以:

<select data-bind='options: productCategories, 
     optionsText: function(item) {return item.name + " - " + item.abbr}, optionsCaption: "Select...", value: company'> </select> 
如果u要显示

简称您可以:

<td data-bind="with: company"> 
          <select data-bind='options: disiplineList, optionsText: "name", optionsCaption: "Select...", value: disipline'> </select> 
          <span data-bind="text: abbr"/> <-- like this 
         </td> 
+0

谢谢我添加了一个不工作http://jsfiddle.net/abritez/ttyhE/4/的版本的URL,使其更清晰地表明我正在尝试做什么。 – abritez 2012-07-23 19:50:06

+0

您无法将字符串设置为optionsValue。我试着说的是,你不需要它来做任何实际的应用。 * optionsText *和* optionsValue *为您带来整个列表* optionsText *为* Strings *和optionsValue为* Integers *。和* Value *将选定的对象设置为一个变量。 – 2012-07-23 20:19:38

+0

好吧,我现在明白了。谢谢! – abritez 2012-07-23 20:54:12