1

I've got this JSON string

{"plaats":["Rottevalle","Rotterdam"]}

And I like to get "Rottevalle and Rotterdam" as an item in my selectbox with Select2

I've got this code.

$("#plaats").select2({
    minimumInputLength: 3,
    tags: [],
    ajax: {
        url: 'plaatsen.php',
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                q: params.term, // search term
                page: params.page
            };
        },
        processResults: function (data) {
            return {
                results: $.map(data, function(obj) {
                    return { id: obj.plaats, text: obj.plaats };
                })
            };
        }
    }
});

Problem is that I don't understand how I get the plaats data back within processResults.

1 Answer 1

1

The issue is that you are passing the full response into $.map instead of the plaats key of that.

$("#plaats").select2({
    minimumInputLength: 3,
    tags: [],
    ajax: {
        url: 'plaatsen.php',
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                q: params.term, // search term
                page: params.page
            };
        },
        processResults: function (data) {
            return {
                results: $.map(data.plaats, function(obj) {
                    return { id: obj, text: obj };
                })
            };
        }
    }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.