我使用的是Select2.js,KnockoutJs,Durandal 2.0
var ViewModel =
[
{ id: "AL", text: "Alabama" },
{ id: "AK", text: "Alaska" },
{ id: "AZ", text: "Arizona" },
{ id: "AR", text: "Arkansas" },
{ id: "CA", text: "California" },
{ id: "CO", text: "Colorado" }
]
var stateQuery = function (query) {
var states = [];
ko.utils.arrayForEach(states, function (state) {
if (state.text.search(new RegExp(query.term, 'i')) >= 0) {
states.push(state);
}
});
虽然在下拉列表中绑定它工作正常。但如果我将文本更改为 Text_Name ,我将无法转换为 upperCase of undefined。
var ViewModel =
[
{ id: "AL", Text_Name: "Alabama" },
{ id: "AK", Text_Name: "Alaska" },
{ id: "AZ", Text_Name: "Arizona" },
{ id: "AR", Text_Name: "Arkansas" },
{ id: "CA", Text_Name: "California" },
{ id: "CO", Text_Name: "Colorado" }
]
答案 0 :(得分:1)
这是一个select2惯例。 See the documentation:
默认实现要求对象具有text属性 返回。
要更改此行为,您应指定函数formatResult
和formatSelection
并指定新的文本属性:
function format(item) {
return item.Text_Name
}
如果您使用的是Knockout绑定处理程序:
<select data-bind="options: states, optionsValue: 'id', optionsText: 'text', value: states, select2: { formatSelection: format, formatResult: format }"></select>