私は彼らのJqueryUIオートコンプリートプラグインを使用して、テキストフィールドに名前を入力し始めると、音楽アーティストの自動提案リストを返します。データソースとしてEchoNestAPIを使用しています。
私が現在使用しているコードは次のとおりです:-
jQuery.ajaxSetup({cache:true});
$("#form-field-influence .text" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://developer.echonest.com/api/v4/artist/suggest",
dataType: "jsonp",
data: {
results: 12,
api_key: "my_API_key",
format:"jsonp",
name:request.term
},
success: function( data ) {
response( $.map( data.response.artists, function(item) {
return {
label: item.name,
value: item.name,
id: item.id
}
}));
}
});
},
minLength: 1,
delay: 0,
select: function( event, ui ) {
$("#log").empty();
$("#log").append(ui.item ? ui.item.id + ' ' + ui.item.label : '(nothing)');
},
});
これで完全に機能し、オートコンプリートリストが表示されますが、これを複数選択フィールドに変換して、ユーザーが希望する数のアーティストを基本的に「タグ付け」できるようにする必要があります。
この機能を提供するプラグインをいくつか見つけました。最近、私は「tokenInput」プラグインで遊んでいます。ただし、このプラグインは、データが特定の形式で返されることを期待しています...次のように:-
[
{"id":"856","name":"House"},
{"id":"1035","name":"Desperate Housewives"},
...
]
私はプラグインを初期化するためにこの単純な行を使用しています:-
$("#form-field-influence .text").tokenInput(
"http://developer.echonest.com/api/v4/artist/suggest?api_key=my_API_key"
);
ただし、JSONPデータソースを読み取るようにtokenInputを設定すると、結果は次のように返されます。
{"response": {"status": {"version": "4.2", "code": 0, "message": "Success"}, "artists": [{"name": "Hold A Candle To This (Alternate Version) (Re-mastered for 'Pirate Radio')", "id": "SOFXGAE12A58A787E3"}, {"name": "Radiohead", "id": "ARH6W4X1187B99274F"}, {"name": "Radio Moscow", "id": "AR3O0021187B999BC8"}, {"name": "The Radio Dept.", "id": "AREKO1L1187B997EFE"}, {"name": "TV on the Radio", "id": "AR0PK561187B9B9EF9"}, {"name": "Joshua Radin", "id": "ARPCRYQ1187FB4ECB8"}, {"name": "The One AM Radio", "id": "ARSHZ531187B99B36F"}, {"name": "Radaid", "id": "ARVGZWZ12086C11298"}, {"name": "New Radicals", "id": "ARL4UQB1187B9B74E3"}, {"name": "Go Radio", "id": "ARQCFYC12A10043E5B"}, {"name": "Radio Slave", "id": "ARKVKKG1187FB3B49A"}, {"name": "Red City Radio", "id": "ARYDXLV11EB9C82776"}, {"name": "Radio Radio", "id": "ARJUCSZ11F4C83E301"}, {"name": "Radikal Guru", "id": "ARCHBIP123526A0E1B"}, {"name": "Radium", "id": "AR8ZGW31187B99F0E5"}]}}
ご覧のとおり、必要なデータは「artists」オブジェクト内にネストされています。したがって、プラグイン(および私が試した他の代替手段)はデータを読み取らず、ドロップダウンリストに表示しません。
このデータソースをtokenInputプラグインで動作させる方法はありますか?あるいは、この機能を実現できる他の「オートコンプリートタグ付け」プラグインはありますか?
どうもありがとう!