在选择二,如何formatSelection和formatResult工作? [英] In Select2, how do formatSelection and formatResult work?

查看:1083
本文介绍了在选择二,如何formatSelection和formatResult工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是选择二( http://ivaynberg.github.io/select2/ )有一个表单的输入域(假设它的id是主题)将在标记模式,与现有的标签列表(允许用户选择其中的一些标签,或创建新的)由远程数据的阵列提供。

I'm using Select2 (http://ivaynberg.github.io/select2/) to have an input field of a form (let's say its id is topics) to be in tagging mode, with the list of existing tags (allowing the user to choose some of these tags, or to create new ones) being provided by an array of remote data.

数组( list.json )正确地从我的服务器了。它有 ID 文本字段,因为选择二似乎需要这些字段。因此,它看起来像这样:

The array (list.json) is correctly got from my server. It has id and text fields, since Select2 seems to need these fields. It thus looks like this:

[ { id: 'tag1', text: 'tag1' }, { id: 'tag2', text: 'tag2' }, { id: 'tag3', text: 'tag3' } ]

在HTML文件中的脚本是这样的:

The script in the HTML file looks like this:

$("#topics").select2({
    ajax: { 
        url: "/mypath/list.json",
        dataType: 'json',
        results: function (data, page) { 
        return {results: data};
        },   
    }
});

但输入栏显示搜索,这意味着它不能使用数组用于标记的支持。

But the input field is showing "searching", which means it's not able to use the array for tagging support.

在与选择二剧本,我知道我必须定义 formatSelection formatInput ,但我不得到他们应该怎么在我的情况下工作,尽管我已阅读选择二文档...谢谢你的帮助!

In the script with Select2, I know I have to define formatSelection and formatInput, but I'm not getting how they should work in my case, although I have read the Select2 documentation... Thank you for your help!

推荐答案

您需要添加功能类似解释的这里。在您的例子:

You need to add the function like explained here. In your example:

function format(state) {

    return state.text;
}

这篇关于在选择二,如何formatSelection和formatResult工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆