【问题标题】:select2 createSearchChoice id from post从帖子中选择2 createSearchChoice id
【发布时间】:2014-06-06 19:13:00
【问题描述】:

我使用 select2 作为标签输入,但在处理新标签的创建和将新标签 id 重新放入 select2 时,我感到很困惑

这个问题密切相关Select2.js: why is id the same as text on change for removed?

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

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

问题是,如果我在 createsearchoption 中提交帖子,则在标签表中找不到的每个击键都会创建一个新标签,并尝试在 on change 事件中发布帖子,我假设 change 事件在 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);                       
                   });
};

【问题讨论】:

  • 我觉得伙计,我已经浏览了文档和至少十几个表格。同样的问题:stackoverflow.com/questions/25150065/…。找到解决方案了吗?
  • 我确实找到了解决方案,我会在下面发布我的答案并看看你的问题

标签: javascript jquery ajax jquery-select2


【解决方案1】:

我最终做的是使用 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 函数中,如果添加,我评估 id;如果存在,它将有一个数字 ID,如果不存在,则表示它是使用 createsearchchoice 连接创建的

我向我的服务器发送一个帖子以通过从 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

                });
              };
            };

这里是添加功能

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

                });
}

【讨论】:

  • 但是这会说创建一个标签,然后引用从ajax返回的新ID作为标签ID吗? select2 让我很困惑,甚至找不到它存储术语/标签的标签 id 的位置,因为它不像在提交表单时再次查询 ajax 以查看哪个 id 与哪个术语相关。
  • 是的,这是我的目标,并且能够使用上述方法使其工作......例如,新标签“love”将显示 id 为“-1/love " 在输入列表中,但 e.added.id 被替换为 data.id ,因此在标签删除期间它将删除正确的标签...注意我不会使用表单提交,只是获取和发布跨度>
猜你喜欢
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
  • 2021-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多