i want use select2
build user tag
function. can add tags
users. acheives functions below:
- when typing tags, use ajax search word in database , list match results;
- we can choose result add tags;
- we can press enter add new tags not exist in database.
- when submit, submit tags themeselves.
however, use example in author's website, can achive tags
or ajax
search. can't combine them together.
can give me suggestions?
i can't see problem that. i've added multiple="multiple"
attribute select
in github example , have ajax + tags functionality. additionally i've added tags:true
option let user create own tags.
html:
<select class="js-data-example-ajax" multiple="multiple"> </select>
js:
$(".js-data-example-ajax").select2({ tags:true, ajax: { url: "https://api.github.com/search/repositories", datatype: 'json', delay: 250, data: function (params) { return { q: params.term, // search term page: params.page }; }, processresults: function (data, page) { var newdata = []; $.each(data.items, function (index, item) { newdata.push({ id: item.id //id part present in data , text: item.name //string displayed }); }); return { results: newdata }; }, cache: true }, escapemarkup: function (markup) { return markup; }, // let our custom formatter work minimuminputlength: 1 });
please note i've modified processresults
function in order make work in jsfiddle.
here's working example: http://jsfiddle.net/8bg2y0q3/1/
Comments
Post a Comment