i am using a jquery ui plugin for tagging :
im having a few issues :
when a user selects from the list, i want to save the id and the value for that tag.
only create tags from the list brought back by the AJAX call
$(function() { $('#tags').tagit({tagSource:function( request, response ) { $.ajax({ type:"GET", url: "http://some_link", dataType: "jsonp", data:{ term:request.term, }, success: function( data ) { response( $.map( data, function( item ) { return { label: item.label, value: item.value, id:item.id }; })); } }); } , triggerKeys: ['enter', 'ma', 'tab']}); });
i am using a jquery ui plugin for tagging :
https://github./aehlke/tag-it
im having a few issues :
when a user selects from the list, i want to save the id and the value for that tag.
only create tags from the list brought back by the AJAX call
$(function() { $('#tags').tagit({tagSource:function( request, response ) { $.ajax({ type:"GET", url: "http://some_link", dataType: "jsonp", data:{ term:request.term, }, success: function( data ) { response( $.map( data, function( item ) { return { label: item.label, value: item.value, id:item.id }; })); } }); } , triggerKeys: ['enter', 'ma', 'tab']}); });
2 Answers
Reset to default 5In answer to your second question Chris Leishman's fork of the Tag-It repository contains a new property requireAutoplete
which allows only items in the autoplete list to be used as tags.
You can find his pull request here: https://github./aehlke/tag-it/pull/37
Download this version of the JS file from: https://github./chrisleishman/tag-it
and use it like an ordinary property:
$(selector).tagit({
requireAutoplete: true,
tagSource: [...]
});
As for your first question, I'm working on this myself so I'll update my answer when I've found a solution.
I've made an amendment to my own local TagIt.js at line 271 changing:
var tag = that.createTag(ui.item.value);
to
var tag = that.createTag(ui.item.label);
which fixed the issue whereby the Id of the items shows instead of the label after choosing an option from the autoplete list.
Update
Here's some information on how to save the ID's of each tag.
The first thing I did was override the createTag
method to include a labelName parameter (you can modify the origional if you want, I just prefered to override it).
$.ui.tagit.prototype.createTag = function (labelName, value, additionalClass) {
// The origional code from createTag here
}
Trim the labelName in the same way that the current value param is trimmed:
value = $.trim(value);
labelName = $.trim(labelName)
Change the label variable to use the new labelName:
var label = $(this.options.onTagClicked ?
'<a class="tagit-label"></a>' :
'<span class="tagit-label"></span>').text(labelName);
In the autoplete section of the origional source I change the call to createTag to include the new label:
var tag = that.createTag(ui.item.label, ui.item.value);
this solution is for handling multiple values and the answer to the question :
when a user selects from the list, i want to save the id and the value for that tag.
Its inspired by the solution above (so you would need to read that :- ) ):
var tag = that.createTag(ui.item); // since ui.item will have everything, label, value, id and other things
then in the createTag function
var item = value;
value = item.value;
...
var tag = $('<li></li>')
.data('tag_item_data',item)// add this line
.addClass('tagit-choice ui-widget-content ui-state-default ui-corner-all')
.addClass(additionalClass)
.append(label);
now to retrieve the tags, just create a function
assignedTagsData : function(){
// Only to be used when singleField option is not seletced
var tags = [];
this.tagList.children('.tagit-choice').each(function() {
tags.push($(this).data('tag_item_data') );
});
return tags;
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745362409a4624420.html
评论列表(0条)