select2: "text is undefined" when getting json using ajax

Posted by user3046715 on Stack Overflow See other posts from Stack Overflow or by user3046715
Published on 2014-01-16T11:15:57Z Indexed on 2014/08/24 4:20 UTC
Read the original article Hit count: 180

Filed under:
|
|
|
|

I'm having an issue when getting json results back to select2. My json does not return a result that has a "text" field so need to format the result so that select2 accepts "Name".

This code works if the text field in the json is set to "text" but in this case, I cannot change the formatting of the json result (code outside my control).

$("#e1").select2({                                
    formatNoMatches: function(term) {return term +" does not match any items." },
    ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
    url: "localhost:1111/Items.json",
    dataType: 'jsonp',
    cache: true,
    quietMillis: 200,
    data: function (term, page) {
            return {
                q: term, // search term
                p: page,
                s: 15               
            };
        },
    results: function (data, page) { // parse the results into the format expected by Select2.          
        var numPages = Math.ceil(data.total / 15);                
        return {results: data.Data, numPages: numPages};
        }
    }      
});

I have looked into the documentation and found some statements you can put into the results such as

text: 'Name',

but I am still getting "text is undefined".

Thanks for any help.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery