vegas2033 vegas2033 - 1 month ago 15
jQuery Question

Typeahead/Bloodhound - number of results

I have been using typeahead and bloodhound for a project and I find it really unintuitive as well as frustrating. Something that should take a second to do ends up in an hour of research.

Anyway i'm trying to alert the number of results into my bloodhound.

Here's my code so far

var pisList = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
url: "../Helper/LookUpPIs?list=" + $list,
cache: false
}
});

alert(pisList.length);

//Typeahead on project numbers
$('.pis').typeahead({
hint: true,
higlight: true,
minLength: 1
},
{
name: 'pis',
source: pisList
});

//Display project details distribution panel
$('input.pis').on('typeahead:selected', function (event, selection) {
var result = selection.match(/\((.*)\)/);
getPiInformation(result[1]);
});
return false;
}


Right now my alert return me undefined. I tried multiple variants but none of them were successful.

Let me know if you have a hint and thanks in advance.

Answer

It's a dumb way but it works. I had to create a hidden field, loop through each result in datumtokenizer and increment the value.

var pisList = new Bloodhound({
                //datumTokenizer: Bloodhound.tokenizers.whitespace,
                datumTokenizer: function (d) {
                    //count number of PIS found and put their ID in the field
                    $('#numberofPIs').val(Number($('#numberofPIs').val()) + Number(1));
                    return tokens;
                },
                queryTokenizer: Bloodhound.tokenizers.whitespace,
                prefetch: {
                    url: "../Helper/LookUpPIs?list=" + $list,
                    cache: false
                }
            });

Probably not the best and cleanest way but it works

Comments