How to use Select2 with JSON via Ajax request?

后端 未结 8 1842
遥遥无期
遥遥无期 2020-11-29 15:58

My Select2 3.4.5 is not working with JSON data.

Here is my input box on HTML:



        
8条回答
  •  無奈伤痛
    2020-11-29 16:40

    for select2 v4.0.0 slightly different

    $(".itemSearch").select2({
        tags: true,
        multiple: true,
        tokenSeparators: [',', ' '],
        minimumInputLength: 2,
        minimumResultsForSearch: 10,
        ajax: {
            url: URL,
            dataType: "json",
            type: "GET",
            data: function (params) {
    
                var queryParameters = {
                    term: params.term
                }
                return queryParameters;
            },
            processResults: function (data) {
                return {
                    results: $.map(data, function (item) {
                        return {
                            text: item.tag_value,
                            id: item.tag_id
                        }
                    })
                };
            }
        }
    });
    

提交回复
热议问题