Uncaught TypeError с Select2 версии 4.0.6-rc.0 и grails 2.5.5 - PullRequest
0 голосов
/ 05 апреля 2019

В представлении Grails у меня есть следующий фрагмент кода для заполнения источника данных select2

$(document).ready(function() {
    $('.model-tags-select2').select2({
        //placeholder: "Search existing tags and enter new tag",
        tags: true,
        multiple: true,
        ajax: {
                url: $.jummp.createLink("modelTag", "fetchTagsForSelect2"),
                dataType: 'json',
                type: "GET",
                cache: true,
                async: true,
                processData: true,
                data: function (params) {
                    var queryParameters = {
                        search: params.term
                    };
                    return queryParameters;
                },
                processResults: function (data) {
                    console.log(data);
                    return {
                        results: data
                    };
                }
            }
        });
    });

ModelTagController.groovy

def fetchTagsForSelect2() {
    String term = params.get("search").encodeAsHTML()
    List<String> tags = modelTagService.search(term)
    List result = []
    tags.eachWithIndex { String value, Long index ->
        result.add(["id": index, "text": value])
    }
    println result
    def rt = ["results": result] as JSON
    println rt
    render rt
}

Когда я набрал 'C' в опции выбора в представлении, операторы печати дали следующие результаты.

{"results":[{"id":0,"text":"Cannot be produced"}]}
[[id:0, text:Cannot be produced]]
{"results":[{"id":0,"text":"Cannot be produced"}]}
[[id:0, text:Cannot be produced]]
{"results":[{"id":0,"text":"Cannot be produced"}]}
[[id:0, text:Reproducible partially], [id:1, text:Cannot be produced], 
[id:2, text:Reproducible], [id:3, text:New Approach]]
{"results":[{"id":0,"text":"Reproducible partially"}, 
{"id":1,"text":"Cannot be produced"},{"id":2,"text":"Reproducible"}, 
{"id":3,"text":"New Approach"}]}

console.log(data) показал, что сервер вернул правильный формат данных для выбора. Смотрите скриншот ниже. enter image description here

К сожалению, я не могу знать причину возникновения ошибки, как на следующем снимке экрана. enter image description here

Если я раскомментировал или удалил placeholder свойство select2, я столкнулся с другой ошибкой jQuery. Смотрите следующее изображение. enter image description here

Вопросы:

  1. Вы уже сталкивались с подобной ситуацией в своей работе?

  2. Может, кто-нибудь из вас подскажет, как решить эти проблемы?

...