【问题标题】:Select2.js not working in dropdownSelect2.js 在下拉列表中不起作用
【发布时间】:2013-12-06 00:32:33
【问题描述】:

我正在使用 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 更改为 Text_Name 我会得到 cannot convert to upperCase strong> 未定义。

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" }
    ]

【问题讨论】:

    标签: knockout.js durandal jquery-select2


    【解决方案1】:

    这是一个 select2 约定。 See the documentation:

    默认实现期望对象具有文本属性 即返回。

    要更改此行为,您应该指定函数 formatResultformatSelection 并指定新的文本属性:

    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>
    

    【讨论】:

    • Hi thx Margabit 它的 worging 很好。但是搜索功能是 nt wrkng。我的html是这样的
    • 请发布另一个关于这个新问题的问题,以便我们写出正确的答案;)
    猜你喜欢
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 2018-06-19
    • 2017-06-27
    相关资源
    最近更新 更多