Jquery tokeninput, first search doesn't work

905 views Asked by At

I have strange problem with tokeninput...

    $("[name='business_list']").tokenInput( {{businessList|raw}}, {
        hintText: "{{ 'business.findBusiness'|trans }}",
        noResultsText: "{{'no_results'|trans({},'app')|capitalize}}",
        searchingText: "{{'searching'|trans({},'app')|capitalize}}",
        prePopulate: {{businesses|raw}},
        onReady: function(){
            $("[name='business_list']").siblings(".token-input-list").find("#token-input-").focus();
        },
        onResult: function(items){
            items.push({
                value: "aaa",
                label: "<span>aaa</span>"
            })

            console.log(items);
            //return items;
        }
    });

And it works like this: For example when I type 'a' for the first time it shows 'No results' (but in the console log I can see that he found 7 objects). When I delete 'a' and type it again it works fine. But... when I will type for example 'z' it will be again the same. It shows 'no results' but I can see that it founds 4 objects and after deleting and typing again it shows them... Any ideas ?

It works fine without 'onResult' function but I want to add one object to any result but with this it doesn't work fine...

1

There are 1 answers

0
Michal Olszowski On BEST ANSWER

PROBLEM SOLVED, code:

$("[name='business_list']").tokenInput( {{businessList|raw}}, {
            hintText: "{{ 'business.findBusiness'|trans }}",
            noResultsText: "{{'no_results'|trans({},'app')|capitalize}}",
            searchingText: "{{'searching'|trans({},'app')|capitalize}}",
            prePopulate: {{businesses|raw}},
            onReady: function(){
                $("[name='business_list']").siblings(".token-input-list").find("#token-input-").focus();
            },
            onResult: function(items){
                var value = $('#token-input-').val();
                items.push({
                    label: "<span class='new-business'><img src='{{ asset('bundles/cloudcrm/images/business.png') }}' style='margin-right: 5px;'>{{ 'deals.newDeal.addBusiness'|trans({},'crm')|capitalize }}<strong>" + " " + value + "</strong></span>",
                    id: "new",
                    value: value
                });
                return items;
            },
            onAdd: function(item){
                if( $(item.label).hasClass("new-business") == true ){
                    $("[name='business_list']").tokenInput("remove", item);
                    $(".modal-business-add").modal('show');
                }
            }
        });