gclark18
gclark18

Reputation: 725

Select2: Creating tags with ajax

I am using the select2 library.

My select2 element can search the database for each tag via the ajax and that works fine.

My issue is, I want the user to also be able to create a new tag. Looking at their documentation I should use the createTag option; however, this fires as soon as I click into the element and on each key press.

Can anyone offer any guidance on how I can achieve my goal?

here is my code thus far

I am using ajax top search for tags but I would also like to create new tags to the database. I have tried doing this via createTag but this seems to be firing as soon as I click in the HTML element and on each key press.

Here is my code:

              $('.tags').select2({
              tags: true,
              placeholder: "These tags will apply to all lines",
              tokenSeparators: [','],
              ajax: {
                  url: '/api/tags/find',
                  dataType: 'json',
                  data: function (params) {
                      return {
                          q: $.trim(params.term)
                      };
                  },
                  processResults: function (data) {
                    return {
                        results: data
                    }
                  },
                  cache: true,
              },
              createTag: function(params) {
                  alert('tag created') // This is were I would put my ajax POST. 
              }
          });

Upvotes: 4

Views: 1999

Answers (1)

gclark18
gclark18

Reputation: 725

After reading the documentation again, I can see I should have been using the events https://select2.org/programmatic-control/events

I used the createTag option to assign newTag: true to newly created tags and used the select2:selected event which checked if a new tag had been selected and, if it was, sent an ajax request to the server to create that tag.


          $('.tags').select2({
              tags: true,
              placeholder: "These tags will apply to all lines",
              minimumInputLength: 3,
              tokenSeparators: [','],
              ajax: {
                  url: '/api/tags/find',
                  dataType: 'json',
                  data: function (params) {
                      return {
                          q: $.trim(params.term)
                      };
                  },
                  processResults: function (data) {
                    return {
                        results: data
                    }
                  },
                  // cache: true,
              },
              createTag: function(params) {
                  let term = $.trim(params.term);
                  if (term.length < 3)
                  {
                      return null
                  }

                  return {
                      id: term,
                      text: term,
                      newTag: true,
                  }
              },
          });

          $('.tags').on('select2:select', function (e) {
              let tag = e.params.data;
              if (tag.newTag === true)
              {
                  axios.post('/api/newtag', {
                      name: tag.text,
                      type: 'default',
                  })
              }
          });

Upvotes: 4

Related Questions