jsonlaraveljquery-ui-autocompletebootstrap-tokenfield

Bootstrap Tokenfield show empty tags list. Laravel App


I have a problem with autocomplete.

Firslty I get an array with tags:

  var tagsList = @json(\App\Helpers\Clients::getTags());

And then:

    $('#tags').tokenfield({
         beautify:false,
         autocomplete: {
              source: [tagsList],
              delay: 100
         },
        showAutocompleteOnFocus: true
     });

This code works correctly. No errors in console. But show the list of tags empty!

If I change tagList by static list, work correctly:

   $('#tokenfield').tokenfield({
       autocomplete: {
       source: ['red','blue','green','yellow','violet'],
       delay: 100
       },
      showAutocompleteOnFocus: true
      });

enter image description here

Console debug show the list correctly:

enter image description here

But in app only show this (repeat, no errors console):

enter image description here

Looks like css doesnt work but every css is linked correctly.

Any idea what is happenning?¿

console.log(tagsList) throw:

enter image description here

Best regards.


Solution

  • tokenfields source attribute needs an array, but you are passing an object to it.

    The problem is that you do not have a sequential array so @json cannot convert it to an array but instead converts it to an object.

    Solution 1

    Convert the output from \App\Helpers\Clients::getTags() to a sequential array.

    Solution 2

    get the object values in JS and pass it to source

    $('#tags').tokenfield({
        beautify:false,
        autocomplete: {
            source: Object.values(tagsList),
            delay: 100
        },
        showAutocompleteOnFocus: true
    });