select2 createSearchChoice id from post

ぐ巨炮叔叔 提交于 2019-12-08 08:03:52

问题


I am using select2 as a tagging input , but i am stumped when it comes to handling the creation of new tags and getting the new tag id's back into the select2

this question is closely related Select2.js: why is id the same as text on change for removed?

and you can see from his jsfiddle http://jsfiddle.net/7e8Pa/ , when text is not found in the createSearchChoice, new option is created, id:-1, text:term, then in the on change event, altering that id to 5

I need to be able to submit a $.post to the server, and get an id back instead of using a static 5

The problem is, if i submit a post in the createsearchoption, every keystroke not found in the tags tabe creates a new tag, and attempting the post in the on change event , i assume the change event finishes before the ajax returns

.on("change", function(e) { 
    log("change "+JSON.stringify({val:e.val, added:e.added, removed:e.removed}));   
    if (e.added) { // if its got an add obj
        if (isNumeric(e.added.id)){ 
        //if its not an existing tag , it passes a string instead of an id
        // so just do a regular add
            add(e.added.id);
        } else {    
        //get the term text, post to server, return the new tag id          
            $.post('handlers/tags.ashx', 
               { operation:"select2createtag", 
                  text: $.trim(e.added.id.substring(3, e.added.id.length))} , 
                  function(data){                   
                        add(data.id);                       
                   });
};

回答1:


what i ended up doing was use the createsearchchoice function, and during the return i specified the id as concatenated -1 and the term, the text (and an additional variable thats unnecessary)

createSearchChoice: function (term, data) {
    if ($(data).filter(function () {
            return this.text.localeCompare(term) === 0;
        }).length === 0) {
            // call $.post() to add this term to the server, receive back id
            // return {id:id, text:term}
            // or detect this shiftiness and do it below in the on-change

            return {
            id: -1+'/'+term,
            text: $.trim(term) + ' (new tag)'
            , isNew: true
            };

},

Then, during my onchange function, if an add, i evaluate the id; If it exists it will have a numeric ID, if it does not, it means it was created using the createsearchchoice concatenation

I send a post to my server to create that tag by substringing out the new tag from the id (prob would be better to regex, but i digress), and that post returns a tag id which i can then use in a separate post to tag the item

.on("change", function(e) {
    if (e.added) {
    if (isNumeric(e.added.id)){
        add(e.added.id);
        } else {
            $.post('handlers/tags.ashx', { 
               operation:"select2createtag", 
               text: $.trim(e.added.id.substring(3, e.added.id.length))} ,   
               function(data){
               //sql returns a new id for the newly created tag
                e.added.id = data.id;
                add(data.id); // see add function

                });
              };
            };

and here is that add function

function add(tagid){
 ///takes a tag id and inserts it in the relational table between item and tag
        $.ajax({
                    url: "handlers/tags.ashx",
                    dataType: "json",
                    data: {
                        idnumber: entity_id,
                        proposalid: proposal_id,
                        tag: tagid,
                        operation:"select2tag"
                    }
                }).done(function(data){

                    if(data.result == false){
                        alert('tag was not inserted');
                    }
                }).complete(function(data){

                });
}


来源:https://stackoverflow.com/questions/24089070/select2-createsearchchoice-id-from-post

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