select2 4.0 AJAX预填怎么样?

Oli*_*ons 5 javascript ajax jquery json select2

我已经读过这个,这个,这个并根据文档中最重要的,但它们都不适合我.我正在尝试使用AJAX select2.我正在尝试制作一个通用的"选择"项目.

因此,对于具有data-select2-json属性的所有元素,我使用值中select2的ajax URL 应用a data-select2-json.

$('[data-select2-json!=""][data-select2-json]').each(function() {
    var url = $(this).attr('data-select2-json');
    var pg_size = $(this).attr('data-select2-page-size') | 30;
    $(this).select2({
        language: language_code,
        tokenSeparators: [',', ' '],
        ajax: {
            url: url,
            dataType: 'json',
            delay: 300,
            data: function (params) {
                return {
                    q: params.term, // -> q=[search term]
                    page: params.page // -> page=[no page]
                };
            },
            processResults: function (data, params) {
                params.page = params.page || 1;
                console.log(data.items);
                return {
                    results: data.items,
                    pagination: {
                      more: (params.page * pg_size) < data.total
                    }
                };
            },
            cache: true
        },
        // let our custom formatter work
        escapeMarkup: function (markup) { return markup; },
        minimumInputLength: 1,
        templateResult: formatRepo,
        templateSelection: formatRepoSelection
    });
});
Run Code Online (Sandbox Code Playgroud)

它运作良好!

效果很好

服务器发送的JSON总是这样格式化:

{"items":
    [{"item": "Fran\u00e7ais", "id": 1},
     {"item": "Finlandais", "id": 5},
     {"item": "Chinois simplifi\u00e9", "id": 15}
    ],
"total": 3}
Run Code Online (Sandbox Code Playgroud)

它非常接近您可以在文档中找到的AJAX示例.我的问题是AJAX initiatilize:我想要在HTML中添加值:

<select multiple="multiple" class="form-control"
        data-select2-json="/fr/chez-moi/tags/langues"
        multiple="multiple"
        name="known_languages">
    <option value="1" selected="selected">Français</option>
    <option value="2" selected="selected">Chinois simplifié</option>
</select>
Run Code Online (Sandbox Code Playgroud)

然后用select2(= $(this).select2({..上面的代码)初始化但这不起作用并给我空白值:

空白值

注意:Kevin Brown给出的解决方案也不起作用,并给出了完全相同的结果.

另一个解决方案是在AJAX中使用类似" &init=1"(或类似的东西)的参数来询问URL,这样服务器就会发送带有添加参数的结果,例如checked: true每个值,我将调用select2这些值.

文档中没有任何关于如何预填的内容select2.我应该怎么做?

这是我的其他功能:

function item_or_text(obj) {
    if (typeof(obj.item)!='undefined') {
        return (obj.item.length ? obj.item : obj.text);
    }
    return obj.text;
}

function formatRepo(data) {
    if (data.loading) return data.text;
    var markup = item_or_text(data);
    console.log('formatRepo', data, markup);
    return markup;
}

function formatRepoSelection(item) {
    var retour = item_or_text(item);
    console.log('formatRepoSelection', item, item.item, retour);
    return retour;
}
Run Code Online (Sandbox Code Playgroud)

Fra*_*bie 3

我使用Select2 示例页面上提供的示例代码在 jsfiddle 上创建了一个工作示例。我只需更改他们的示例选择标签即可接受多个像您这样的标签:

<select multiple="multiple" ...
Run Code Online (Sandbox Code Playgroud)

我还添加了第二个默认选择的选项:

<option value="3620194" selected="selected">select2/select2</option>
<option value="317757" selected="selected">Modernizr/Modernizr</option>
Run Code Online (Sandbox Code Playgroud)

如果您看一下小提琴,您会发现它按照您希望的方式工作。

您没有包含您的方法的代码templateSelection: formatRepoSelection。我的猜测是该方法是导致您的问题的原因。示例页面上使用的代码是:

function formatRepoSelection(repo) {
  return repo.full_name || repo.text;
}
Run Code Online (Sandbox Code Playgroud)

虽然我不知道你的formatRepoSelection代码是什么,但我认为如果你将其更改为如下所示,你的问题就会得到解决:

function formatRepoSelection(repo) {
  return repo.item;
}
Run Code Online (Sandbox Code Playgroud)