从后选择二createSearchChoice IDcreateSearchChoice、ID

2023-09-10 18:06:28 作者:雨落长安恋佳人

我使用的选择2为标记的输入,但我难倒,当涉及到处理创造新的标签,并获取新的标签ID的回选择2

这个问题是密切相关的Select2.js:为什么ID相同的变化文字去掉?

,你可以从他的jsfiddle http://jsfiddle.net/7e8Pa/ ,当文字是见在createSearchChoice没有找到,就创建新的选项,ID:-1,文本:术语,然后在上的变化时,改变该ID以5

我需要能够提交 $。交到服务器上,并得到一个ID后面,而不是使用静态5

现在的问题是,如果我提交后在createsearchoption,每个按键不是在田部创建了一个新的标签的标签发现,并试图在改变事件的帖子里,我承担了ajax返回前更改事件完成

 。对(变,功能(五){
    日志(变+ JSON.stringify({VAL:e.val补充道:e.added,删除:e.removed}));
    如果(e.added){//如果有一个附加OBJ
        如果(ISNUMERIC(e.added.id)){
        //如果没有一个现有的标签,它通过一个id的字符串,而不是
        //所以只是做一个常规的加
            加(e.added.id);
        } 其他 {
        //获取长期的文字,邮寄至服务器,返回新的标签ID
            .post的$('处理器/ tags.ashx',
               {操作:select2createtag
                  文本:$ .trim(e.added.id.substring(3,e.added.id.length))},
                  功能(数据){
                        加(data.id);
                   });
};
 

解决方案

我最终什么做什么是使用createsearchchoice函数,返回的时候,我指定为串联的ID -1和术语,文字(和一个额外的变量多数民众赞成不必要的)

  createSearchChoice:功能(术语,数据){
    如果($(数据).filter(函数(){
            返回this.text.localeCompare(项)=== 0;
        })。长度=== 0){
            //调用$。员额()这个词添加到服务器,接收返回的ID
            //返回{ID:ID,文本:长期}
            //或检测到这种缩骨,做它低于按变化

            返回 {
            ID:-1 +/+名词,
            文本:$ .trim(词)+'(新标签)
            ,isNew:真
            };

},
 

然后,在我的onchange功能,如果一个插件,我评估ID;如果它存在,将具有数字ID,如果没有,则意味着它是使用createsearchchoice级联创建

我发个帖子到我的服务器上创建从ID substringing出来的新标签(概率将是更好的正则表达式,但我离题)的标签,并且该职位返回标签ID,我就可以在使用单独的后标记的项目

 。对(变,功能(五){
    如果(e.added){
    如果(ISNUMERIC(e.added.id)){
        加(e.added.id);
        } 其他 {
            .post的$('处理器/ tags.ashx',{
               操作:select2createtag
               文本:$ .trim(e.added.id.substring(3,e.added.id.length))},
               功能(数据){
               // SQL返回一个新ID为新创建的标签
                e.added.id = data.id;
                加(data.id); //请参阅添加功能

                });
              };
            };
 
注册苹果id账号输入银行卡后的到期日期是什么,还有代码是什么,输了密码也不对

和这里要说的是添加功能

 函数add(标签识别){
 ///需要一个标签ID,并将其插入关系表中的项目与标签之间
        $阿贾克斯({
                    网址:处理器/ tags.ashx
                    数据类型:JSON,
                    数据: {
                        中IDNumber:entity_id,
                        proposalid:proposal_id,
                        标签:标签识别,
                        操作:select2tag
                    }
                })。完成(功能(数据){

                    如果(data.result == FALSE){
                        警报('标签未插入');
                    }
                }),完成(功能(数据){

                });
}
 

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

解决方案

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){

                });
}