0

I want to be able to have the option persist after I added a tag. A similar question was solved here: Dynamically add item to jQuery Select2 control that uses AJAX But a commenter is right, it is only solved with an additional input box somewhere not in the original select box.

[![Question kinda solved][1]][1]

I tried a number of things and they all do not work: returning an object with createOption or createSearchChoice Appending a new Option

This is because the widget already secretly adds a new options as is necessary to accommodate the new tag, but if it wasn't a tag from the original data, it will delete it if a new option is selected. I thought maybe on the select2:select event, I could see if the new value is in the data somewhere. Alas, that data is hard to find. [1]: https://i.stack.imgur.com/CyCC4.png

Here are some failed attempts with commentary

    var quote = {};
          google.script.run.withSuccessHandler(function(e){
             quote = $('#quote').select2({
              width:'100%',
              data: e,  //[{id:'quote', text:'quote'},{id:'quote2', text:'quote2'}]
              tags: true,
              /*Alas, this doesn't work either becuse the tag is still in the option when we check fo rit
                createTag: function(){
                if (quote.find("option[value='" + params.term + "']").length) quote.append(Option(params.term,params,term,true,true)).trigger('change')
                return {id: params.term, text:params.term})
                */
                //insertTag: function(d,t){d.push(t)}  Didn't work either
              allowClear:true,
              placeholder:'No Quote'
            }).on('select2:select',function(e){
              /*
              // Set the value, creating a new option if necessary
                This doesn't work because the new item is in the option thingy temporarily while we're trying to see if its there
              if (quote.find("option[value='" + e.params.data.id + "']").length) {
                    quote.val(e.params.data.id).trigger('change');
                } else { 
                    // Create a DOM Option and pre-select by default
                    var newOption = new Option(e.params.data.id, e.params.data.id, true, true);
                    // Append it to the select
                    quote.append(newOption).trigger('change');
} 
              */
              if (e.params.data.id&&(quotes.indexOf(e.params.data.id)!=-1)){
                loadQuote(e.params.data.id)
              } else {
                //new quote so now we have to make preparations...
                quotes.push(e.params.data.id)
                console.log(e.params.data)
                console.log(quotes)
                var newOption = new Option(e.params.data.id,e.params.data.id,true,true)
                quote.append(newOption).trigger('change')
                //don't send anything to the server, because there is no need
              }
            })
            quote.val(null).trigger('change')

0 Answers0