【问题标题】:How to dynamically add elements to select2 when using ajax使用ajax时如何动态添加元素到select2
【发布时间】:2013-08-13 03:19:11
【问题描述】:

我正在使用 select2 来显示名称的动态列表。当用户键入时,这些名称是从服务器获取的,相似,但如果找不到名称,我会显示一个“建议”链接,将名称添加到数据库中。

“建议”链接出现在选择的通知区域(查询服务器时出现“正在搜索...”的位置)。当用户按下“建议”时,会发出一个请求,如前所述,将建议的名称添加到数据库中。现在,当该请求响应时,我希望将名称添加到 select2 的列表中,选中,然后关闭下拉框。

我在这项努力中没有成功 =) 我尝试使用“val”,但我要么做得不对,要么这不是要走的路。这是我现在所拥有的代码摘录:

$('.' + id + '-select2', nextFrame).select2({
    ajax: {
        url: url,
        dataType: 'json',
        results: function(data) {
            var results = [], it;
            for (it in data) {
                results.push({
                    id: data[it]['pid'],
                    text: data[it]['name']
                });
            }

            return { results: results }
        },
        data: function(text) {
            return { name: text }
        }
    },

    formatNoMatches: function(text) {

        // Add the AJAX behavior to the anchor, but do it asynchronously
        // This way we give time to select2 to render the layout before we configure it
        setTimeout(function() {
            $('#' + id + '-ajax-anchor').on('click', function(e) {
                var target = $(e.target);

                $.ajax({
                    url: target.attr('href'),
                    type: 'POST',
                    dataType: 'json',
                    data: {
                        name: text
                    },
                })
                .done(function(response) {
                    /*
                        Need help here !!
                        1) Add the response to the list
                        2) Select it
                        3) Close
                    */
                })
                ;

                e.preventDefault();
            });
        }, 1);

        return "No match, <a id='" + id + "-ajax-anchor' href='" + url + "'>suggest</a>";
    },
});

【问题讨论】:

    标签: javascript jquery jquery-select2


    【解决方案1】:

    只是在您的代码的帮助下修复了它。这是我的done() 部分:

    done(function(resp) {
      d = $(s2).select2("data"); // s2 is this select2 object. in your case $('.' + id + '-select2', nextFrame);
      $(s2).select2("close"); // first close the opened list. select2 doesn't close "result not found".
      d.push({id: resp.id, title: resp.title}); // now add ajax response to our current select2 data. in my case response contains count, id and title of the added/suggested
      $(s2).select2("data", d);
    });
    

    【讨论】:

    • 这是很久以前的事了 =) 但该代码有效吗?如果是,我会接受答案。
    • 是的,你的代码对我很有帮助。我刚刚实现了一个标签选择组件(使用 select2),可以远程添加新标签。
    • 我很高兴=)如果我没记错的话,我当时也解决了这个问题,但我想我忘了在这里发布。我会接受你的回答。
    【解决方案2】:
    //...
    .done(function (response) {
        var myValue = response.value;
        var myText = response.text;
    
        var o = new Option(myText, myValue);
        $(o).html(myText);
        $("select").append(o);
        $("select option[value='" + myValue + "']").attr('selected', 'selected');
    })
    //...
    

    【讨论】:

    • 它不起作用... Select2 用一个 div 和一个 ul 列表替换了“select”元素,我尝试让你的解决方案适应这个事实,但没有成功。
    猜你喜欢
    • 2021-06-15
    • 1970-01-01
    • 2014-10-15
    • 2013-02-08
    • 2014-10-15
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多