我从Select2开始。我想结合"标记支持"和#34;加载远程数据" (见http://ivaynberg.github.io/select2/)。
在HTML文件的正文中,我目前有一个输入表单:
<input id="topics">
使用&#34;标记支持&#34;使用数组给出的标签列表,如下面的HTML文件头部脚本显示:
$("#topics").select2({
placeholder: "Choose topic(s)",
tags:["russia", "europe", "obama"]
});
我还有一台服务器,使用Express在Node.js中制作,连接到数据库。我知道如何在服务器端处理与数据库相关的请求(POST或GET)。
对于我的HTML文件的输入形式,我想要使用而不是数组[&#34; russia&#34;,&#34; europe&#34;,&#34; obama&#34;]来使用服务器提供的一组数据。
我怎么写呢?
谢谢!
答案 0 :(得分:3)
<强>后端强>
您必须创建一条这样的短途路线:
app.get('/foo/bar.json', function(req, res){
//...
res.send(data);
}
<强>前端强>
$("#topics").select2({
placeholder: "Search for a movie",
minimumInputLength: 1,
ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
url: "/foo/bar.json",
dataType: 'json',
results: function (data, page) { // parse the results into the format expected by Select2.
// since we are using custom formatting functions we do not need to alter remote JSON data
return {results: data};
}
},
});
JSON对象可以是/包含数组。见https://ivaynberg.github.io/select2/#data