select2 createSearchChoice id from post

select2 createSearchChoice id from post

本文关键字:post from id createSearchChoice select2      更新时间:2023-09-26

我使用select2作为标签输入,但是当涉及到处理新标签的创建并将新标签id返回到select2

时,我被难住了

这个问题是密切相关的Select2.js:为什么id是相同的文本更改为删除?

,您可以从他的jsfiddle http://jsfiddle.net/7e8Pa/中看到,当在createSearchChoice中没有找到文本时,将创建新选项,id:-1, text:term,然后在on change事件中,将id更改为5

我需要能够提交一个$.post到服务器,并得到一个id回来,而不是使用一个静态的5

问题是,如果我在createsearchoption中提交一个帖子,在标签表中没有找到的每个按键都会创建一个新标签,并尝试在更改事件中发布该帖子,我假设更改事件在ajax返回

之前完成
.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);                       
                   });
};

我最后做的是使用createsearchchoice函数,在返回期间,我指定id为连接-1和术语,文本(和一个不必要的额外变量)

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
            };
},

然后,在我的onchange函数中,如果是add,我计算id;如果它存在,它将有一个数字ID,如果它不存在,这意味着它是使用createsearchchoice连接创建的

我发送一个帖子到我的服务器,通过从id中提取新标签来创建该标签(prob最好是regex,但我离题了),并且该帖子返回一个标签id,然后我可以在单独的帖子中使用该标签id来标记项目

.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
                });
              };
            };
这里是add函数
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){
                });
}