Modify the behavior of jquery tag-it based on autocomplete library to use ajax JSON sources

我怕爱的太早我们不能终老 提交于 2019-12-04 11:16:48

You ask you want to submit, something different. Then lets take that single point of entry:

$('#data').submit(function() {
    // Change the value we are submitting
    var tagids = [];
    $.each($('#singleFieldTags').val().split(','),
           function(idx, taglabel){
               tagids.push(lookup_tagid_for_label(taglabel));
           }
    )
    $('#singleFieldTags').val(tagids.join(','));          
    return true; // Let the event go on.
});

That should change the labels with the ids before submit.

lookup_tagid_for_label, could either do the ajax call again, but it may be cheaper to cache it on the field at first lookup:

$("#singleFieldTags").tagit({
     autocomplete: {
        source: function(search, showChoices) {
                    $.getJSON("/repo/json", {q: search.term},
                        function (data) {
                            var choices = [];
                            $.each(data, function (idx, tag) {
                                choices.push(tag.name);
                                $("#singleFieldTags").data(tag.name, tag.id);
                            });
                            showChoices(choices);
                    });
                }
});

Then you can replace lookup_tagid_for_label(taglabel) with $("#singleFieldTags").data(taglabel).

In contrary to my other answer, that expected tag-it to follow the autocomplete-api, this works now.

Changing the tags when they are added breaks removal and possibly other things as well. You could try the mitigate that by writing a beforeTagRemoved handler and possibly other handlers in the future. (a pain to maintain).

As I commented you should use the autocomplete api. The doc points further to jquery-ui. The data can be in this form:

... An array of objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ]

So I suspect this could be enough:

 $("#singleFieldTags").tagit({
     autocomplete: {
        source: function(search, showChoices) {
                    $.getJSON("/repo/json", {q: search.term},
                              function(data) {
                                  var choices = [];
                                  $.each(data, function(idx, tag){
                                      choices.push({label: tag.name,
                                                    value: tag.id})
                                      });
                                  showChoices(choices);
                              });
                }
         }
     })

Sadly this doesn't work as expected as tag-it, currently drops the label and uses just the id, when creating the tag. I think tag-it should be cleaned up and support the complete autocomplete api.

Update

The maintainer agrees with this. So this answer may become the canonical answer in the future.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!