twitter / typeahead.js

typeahead.js is a fast and fully-featured autocomplete library
http://twitter.github.io/typeahead.js/
MIT License
16.52k stars 3.2k forks source link

Bloodhound, minLength and default suggestions doesnt work #1346

Closed sajadghawami closed 3 years ago

sajadghawami commented 9 years ago

Hello there,

i tried everything, checked every issue related to this, but i dont seem to get it to work.

Thats my code:

var typeahead_fach = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.whitespace,
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        local: typeahead_fach
    });

    $('input.typeahead_fach').typeahead({
            hint: true,
            highlight: true,
            minLength: 0
        },
        {
            name: 'fach',
            limit: fach.length,
            source: fachDefaults
        });

    function fachDefaults(q, sync) {
        if (q === '') {
            sync(typeahead_fach.get('Detroit Lions', 'Green Bay Packers', 'Chicago Bears'));
        }

        else {
            typeahead_fach.search(q, sync);
        }
    }

it shows me the usual stuff in my array "typeahead_fach" but deosnt show any default suggestions.

Whats happening?

imolorhe commented 9 years ago

Why is the local option in the Bloodhound set to the Bloodhound instance?

local: typeahead_fach
sajadghawami commented 9 years ago

thats how the example for bloodhound looked like. And it works too:


// constructs the suggestion engine
var states = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.whitespace,
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  // `states` is an array of state names defined in "The Basics"
  local: states
});

$('#bloodhound .typeahead').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'states',
  source: states
});
imolorhe commented 9 years ago
// `states` is an array of state names defined in "The Basics"

The states was created before as an array from the previous example. So it was something like this:

var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
  'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
  'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
  'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
  'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
  'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
  'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
  'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];

// constructs the suggestion engine
var states = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.whitespace,
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  // `states` is an array of state names defined in "The Basics"
  local: states
});

$('#bloodhound .typeahead').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'states',
  source: states
});
sajadghawami commented 9 years ago

yeah! didnt want to copy the whole code, but thats how i use it.

as you can see, it uses the local option in the Bloodhound as the Bloodhound instance

imolorhe commented 9 years ago

No. It uses the states array as the local option.

-----Original Message----- From: "leakingminds" notifications@github.com Sent: ‎8/‎13/‎2015 5:05 PM To: "twitter/typeahead.js" typeahead.js@noreply.github.com Cc: "Samuel" samuelimolo4real@gmail.com Subject: Re: [typeahead.js] Bloodhound, minLength and default suggestionsdoesnt work (#1346)

yeah! didnt want to copy the whole code, but thats how i use it. as you can see, it uses the local option in the Bloodhound as the Bloodhound instance — Reply to this email directly or view it on GitHub.

sajadghawami commented 9 years ago

But isnt that what you meant in your comment earlier?

Nonetheless, i use it like the example.

"tyepeahead_fach" is my array and bloodhound works without a problem.

MichaelLogutov commented 9 years ago

Got the same error when using remote. I think this comment describes why: http://stackoverflow.com/a/30330790/219900

madhumithavenkat commented 7 years ago

got any solution for the above problem?

neemias-santos commented 6 years ago

I got the same problem. To work I added the same line of the file bootstrap-typeahead.js in my function. And works!! I think is not the better way to do, what do you all think?

this.element.find('input[type=search]').typeahead({
        autoSelect: false,
        items: 20,
        delay: 300,
        minLength: 3,
        highlighter: function (item) {
            var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
            return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
                return '<strong>' + match + '</strong>'
            })
        }
    });
ravindrabajpai commented 6 years ago

I tried even with the highlighter, default suggestions are not coming up with click. Can someone please help me with this - http://jsfiddle.net/RavindraBajpai/gc4d0Lwn/1/

To make it work figured out a hacky way with - $('#my-datasets .typeahead').on( 'click', function() { $(this).typeahead( 'val', 'a' ); $(this).typeahead( 'open'); });

But it doesn't sound a nice solution. Please suggest

iranianpep commented 6 years ago

I had to upgrade from version 0.11.1 to the latest version here to resolve the issue: https://github.com/corejavascript/typeahead.js/releases

jeffz2012 commented 6 years ago

minLength still does not work reliably on 0.11.1

rahul-bluebash commented 5 years ago

minLength still does not work reliably on 0.11.1

@jeffz2012 minLength is working fine. Here my code -

this.typeahead({
    minLength: 3
  },
  { name: 'search',
    displayKey: 'id',
    source: searchedVariants,
    templates: {
      empty: [
        '<div class="empty-message">',
          'No result found',
        '</div>'
      ].join('\n'),
      pending: function (query) {
        return '<div>Loading...</div>';
      },
      suggestion: function(variant){
        window.variantTemplate = Handlebars.compile($("#variants_template").text());
        return formatVariantResult(variant)
      }
    }
  });