I am completely new to using the typeahead plugin, and unfortunately, my JavaScript skills (not jQuery) are not up to par. Below you can find a snippet of my JSON:
{"Product":[
{"@ProductCode":"C1010","@CategoryName":"Clips"},
{"@ProductCode":"C1012","@CategoryName":"Clips"},
{"@ProductCode":"C1013","@CategoryName":"Clips"},
{"@ProductCode":"C1014","@CategoryName":"Clips"},
{"@ProductCode":"C1015","@CategoryName":"Clips", "EAN":"0987654321"}
]}
Currently, I am trying to work with the typeahead bundle version 0.10.5 in conjunction with this JavaScript code:
$(document).ready(function () {
var products = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
limit: 100,
remote: {
url: 'TypeAhead.ashx?q=%QUERY&cat=prod',
filter: function (data) {
return data.Products;
}
}
});
products.initialize();
$("#tbSSearch").typeahead({
highlight: true,
minLength: 2
}, {
source: products.ttAdapter(),
displayKey: function (products) {
return products.product.code;
},
templates: {
header:"<h3>Products</h3>"
}
});
});
Upon checking the Chrome console, I encountered an error message stating:
Uncaught TypeError: Cannot read property 'length' of undefined
However, it seems like the issue lies within my minified jquery 2.1 library rather than the aforementioned JavaScript source. There is no popup displayed below the #tbSearch
input field in the browser.
Following @Mike's suggestion, I attempted to test the code on jsfiddle http://jsfiddle.net/gw0sfptd/1/. Unfortunately, I had to tweak some components to align with local JSON, resulting in no successful outcomes.
Edit: Following David's advice, I have cleaned up my JSON structure as shown below:
[{"Code":"C1010","Gtin13":0,"CategoryName":"Clips"},
{"Code":"C1012","Gtin13":0,"CategoryName":"Clips"},
{"Code":"C1013","Gtin13":0,"CategoryName":"Clips"}]
Additionally, the revised JavaScript now looks as follows:
remote: {
url: 'TypeAhead.ashx?q=%QUERY&cat=prod',
filter: function (products) {
return $.map(products.results, function (product) {
return {
value: product.Code
};
});
}
}
datumTokenizer: function (datum) {
return Bloodhound.tokenizers.whitespace(datum.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
Despite these adjustments, the typeahead feature still fails to function properly, and there are no noticeable errors popping up in the Firefox console. My desired output would include a list of product codes, their corresponding categories, and the GTIN13 values (if applicable) due to SQL search requirements. Would crafting a JavaScript 'class' for products on the client side and parsing the JSON accordingly be a viable solution? Understanding the mechanics behind Bloodhound remains perplexing to me even after studying the samples and documentation provided by both Typeahead and Bloodhound. Ideally, when selecting an item from the typeahead suggestions, I envision that the product data source would link to productdetail.aspx. Alternatively, choosing an item from the category data source (not included in this post) should redirect the page to categorydetail.aspx.