Follow the working demo below on JsFiddle
We have 2 fields, “modulo-1” and “modulo-2”
HTML:
Módulo 1:<br>
<input id="modulo-1" class="typeahead" type="text">
<br>
<br>
Módulo 2:<br>
<input id="modulo-2" class="typeahead" type="text">
<script type="text/javascript" src="//code.jquery.com/jquery-2.1.0.js"></script>
<script type="text/javascript" src="https://twitter.github.io/typeahead.js/releases/latest/typeahead.bundle.js"></script>
html {
overflow-y: scroll;
*overflow-x: hidden;
}
.container {
max-width: 750px;
margin: 0 auto;
text-align: center;
}
.tt-menu,
.gist {
text-align: left;
}
/* base styles */
/* ----------- */
html {
font: normal normal normal 18px/1.2 "Helvetica Neue", Roboto, "Segoe UI", Calibri, sans-serif;
color: #292f33;
}
a {
color: #03739c;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
.table-of-contents li {
display: inline-block;
*display: inline;
zoom: 1;
}
.table-of-contents li a {
font-size: 16px;
color: #999;
}
p + p {
margin: 30px 0 0 0;
}
/* site theme */
/* ---------- */
.title {
margin: 20px 0 0 0;
font-size: 64px;
}
.example {
padding: 30px 0;
}
.example-name {
margin: 20px 0;
font-size: 32px;
}
.demo {
position: relative;
*z-index: 1;
margin: 50px 0;
}
.typeahead,
.tt-query,
.tt-hint {
width: 396px;
height: 30px;
padding: 8px 12px;
font-size: 24px;
line-height: 30px;
border: 2px solid #ccc;
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
outline: none;
}
.typeahead {
background-color: #fff;
}
.typeahead:focus {
border: 2px solid #0097cf;
}
.tt-query {
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
-moz-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075);
}
.tt-hint {
color: #999
}
.tt-menu {
width: 422px;
margin: 12px 0;
padding: 8px 0;
background-color: #fff;
border: 1px solid #ccc;
border: 1px solid rgba(0, 0, 0, 0.2);
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
-webkit-box-shadow: 0 5px 10px rgba(0,0,0,.2);
-moz-box-shadow: 0 5px 10px rgba(0,0,0,.2);
box-shadow: 0 5px 10px rgba(0,0,0,.2);
}
.tt-suggestion {
padding: 3px 20px;
font-size: 18px;
line-height: 24px;
}
.tt-suggestion:hover {
cursor: pointer;
color: #fff;
background-color: #0097cf;
}
.tt-suggestion.tt-cursor {
color: #fff;
background-color: #0097cf;
}
.tt-suggestion p {
margin: 0;
}
.gist {
font-size: 14px;
}
/* example specific styles */
/* ----------------------- */
#custom-templates .empty-message {
padding: 5px 10px;
text-align: center;
}
#multiple-datasets .league-name {
margin: 0 20px 5px 20px;
padding: 3px 0;
border-bottom: 1px solid #ccc;
}
#scrollable-dropdown-menu .tt-menu {
max-height: 150px;
overflow-y: auto;
}
#rtl-support .tt-menu {
text-align: right;
}
JavaScript:
// modulo-1
// ----------
var charMap = {
"à": "a",
"á": "a",
"â": "a",
"é": "e",
"è": "e",
"ê": "e",
"ë": "e",
"É": "e",
"ï": "i",
"î": "i",
"ô": "o",
"ö": "o",
"û": "u",
"ù": "u",
"ü": "u",
"ñ": "n"
};
var names = ["série:Aladdin", "personagem:Naruto","paródia:Dragon Ball Z"];
var normalize = function (input) {
$.each(charMap, function (unnormalizedChar, normalizedChar) {
var regex = new RegExp(unnormalizedChar, 'gi');
input = input.replace(regex, normalizedChar);
});
return input;
};
var queryTokenizer = function (q) {
var normalized = normalize(q);
return Bloodhound.tokenizers.whitespace(normalized);
};
var nombres = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: queryTokenizer,
local: $.map(names, function (name) {
// Normalize the name - use this for searching
var normalized = normalize(name);
return {
value: normalized,
// Include the original name - use this for display purposes
displayValue: name
};
})
});
nombres.initialize();
$('#modulo-1').typeahead({
minLength: 1,
hint: false,
highlight: true
}, {
displayKey: 'displayValue',
source: nombres.ttAdapter()
});
// modulo-2
// ----------
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring q
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring q, add it to the matches array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
var states = ["série:Aladdin", "personagem:Naruto","paródia:Dragon Ball Z"];
$('#modulo-2').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
source: substringMatcher(states)
});
https://jsfiddle.net/avcurdgy/2/
In module-1, it’s almost the way I want it, in it I can search for “série” or “serie” and it gives me the value I need. But when I search for “naruto” it returns nothing.
Now in “modulo-2” when I search for “naruto” it can find the result, but it doesn’t ignore the accents, if I search for “serie” it doesn’t return anything.
I need that in module-1 when I search for “naruto” it finds the result as in module-2, I tried a lot here for several days, but without success.
Could someone help me?
typeahead.js documentation:
https://twitter.github.io/typeahead.js/examples/