javascript 在 Select2 中,formatSelection 和 formatResult 是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23643409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Select2, how do formatSelection and formatResult work?
提问by GBC
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.
我正在使用 Select2 ( http://ivaynberg.github.io/select2/) 使表单的输入字段(假设它的 id 是topics
)处于标记模式,并带有现有标记列表(允许用户选择其中一些标签,或创建新标签)由远程数据数组提供。
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:
数组 ( list.json
) 是从我的服务器正确获取的。它有id
和text
字段,因为 Select2 似乎需要这些字段。因此它看起来像这样:
[ { id: 'tag1', text: 'tag1' }, { id: 'tag2', text: 'tag2' }, { id: 'tag3', text: 'tag3' } ]
The script in the HTML file looks like this:
HTML 文件中的脚本如下所示:
$("#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.
但是输入字段显示“正在搜索”,这意味着它无法使用数组进行标记支持。
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!
在带有 Select2 的脚本中,我知道我必须定义formatSelection
和formatInput
,但是我不知道它们在我的情况下应该如何工作,尽管我已经阅读了 Select2 文档...谢谢您的帮助!