auto-complete に基づいて jQuery プラグインtag-itにいくつかの機能を追加しようとしています:
a) JSON データをフィルタリングして、タグの名前のみを表示しようとしています。
によって返される JSON サンプル/repo/json
:
[{id:1, name:"0.8-alpha-1", category:"version"}, {id:2, name:"0.8-alpha-2", category:"version"}, {id:3, name:"0.8-alpha-3", category:"version"}, {id:4, name:"0.8-alpha-4", category:"version"}, {id:5, name:"0.8-alpha-1", category:"version"}, {id:6, name:"0.8-alpha-2", category:"version"}, {id:7, name:"0.8-alpha-3", category:"version"}, {id:8, name:"0.8-alpha-4", category:"version"}]
b) ユーザーがデータを送信するときに、名前ではなくタグの ID を送信したい。
c) tag-it 入力フィールドにいくつかの制約を追加しようとしています: ユーザーは my によって返された JSON にないタグを検証できません/repo/json call
。
tag-it リポジトリをフォークしたくありません。ユーザー配列と検索の交差をbeforeTagAdded
オプションでテストすることは可能のようです。
交差点を実現するためのタグのリストがどこにあるかわからないため、現時点では成功していません。
私のjsコード:
$(function(){
$("#singleFieldTags").tagit({
tagSource: function(search, showChoices) {
$.ajax({
url: "/repo/json",
dataType: "json",
data: {q: search.term},
success: function(choices) {
showChoices(choices);
}
})},
beforeTagAdded: function(event, ui) {
//if ($.inArray(ui.tagLabel, search) == -1) {
// $("#singleFieldTags").tagit("removeTagByLabel", ui.tagLabel);
// }
console.log(ui.tag);
}});
});
html フォーム:
<form name="data" action="/repo/uploadMole" method="POST" enctype="multipart/form-data">
<input name="tags" id="singleFieldTags" ><br/>
<input type="Submit">
</form>