3

I'm using select2 to display a dynamic list of names. The names are fetch from the server as the user types, by resemblance, but in case a name is not found, I display a "Suggest" link which adds the name to the database.

The "Suggest" link appears in the select's notification area (where "Searching..." appears when querying the server). When the user presses "Suggest", a request is made that, as said, adds the suggested name to the database. Now, when that request responds, I want the name to be added to the select2's list, selected, and the drop box closed.

I'm nothing being successful in this endeavor =) I tried using 'val', but I'm either not doing it right or that's not the way to go. Here's a code excerpt from what I have right now:

$('.' + 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>";
    },
});
4

2 回答 2

2

只是在您的代码的帮助下修复了它。这是我的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);
});
于 2014-07-16T12:53:55.543 回答
-1
//...
.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');
})
//...
于 2013-08-13T07:41:43.280 回答