I need your help. I'm trying to put an autocomplete on dynamically create text input but I have got some problem. I would like to use the JSON file because the ajax request took 1/2 sec and for my use it's too much.
Here is the code:
$(document).on('keydown.autocomplete',"[id$=_buy_mod]",function() {
$(this).autocomplete({
source: function(request,response) {
$.ajax({
ttpe: "GET",
url: "js/articoli.json",
data: {
term: request.term
},
dataType:'json',
dataFilter: function(data) {return data;},
success: function(data) {
response($.map(data,function(product) {
return {
label: product.label,
value:product.value
};
}))
},
})
},
minLength: 1,
select: function( event, ui ) {
var localid = $(this).attr('id');
var numerolocal = localid.match(/\d+/);
console.log(numerolocal);
$(this).val(ui.item.label);
$("[id$="+numerolocal+"_buy_id_mod]").val(ui.item.value)
return false;
}
});
Actually timing is good but autocomplete is not filtering data and it show me all possible words.
Here is the json file
[{"value": "1","label": "Panasonic Lumix L1"},
{"value": "10","label": "Sigma 17-50mm f 2.8 OS Canon"},
{"value": "11","label": "Canon 1000D"},
{"value": "1100D","label": "Canon 1100D"}
-------
How can I solve?
I am using Bootstrap Typeahead (version: v2.0.0). I have result set through an ajax call and would like to show autocomplete list using value and one more attribute.
I have result set like:
{"res":[{"id":617,"value":"DESC: Admin responder comment","user_id":37,"status":1,"comment":"Hi {firstname},\n\nThis is testing.","firstname":"Tom's"},
{"id":625,"value":"DESC: my comment","user_id":37,"status":1,"comment":"Hi {first_name}, anther testing comment","firstname":"William's"}
]}
Autocomplete matching keyword with value attribute and populating it. But, I would like to match keyword with comment attribute as well.
I have tried matcher of typeahead and some solutions mentioned on SO as well but did not found useful. Tried one here: but getting error of "TypeError: Bloodhound is undefined" with that solution.
Please check code below used for autocomplete using result by ajax.
var typeahead_pre_written_xhr = null;
var search_keyword = function ( tid, txtElmt )
{
$( "#typeahead-input" ).typeahead({
items: 20,
source: function(typeahead, query) {
action_url = _base_url + "search/search_keyword/";
typeahead_pre_written_xhr = $.ajax({
cache: false,
type: "POST",
dataType: "json",
url: action_url + query,
data: { 'type': type },
error: function (request, status, error) { },
success: function(data) {
if( typeof data.res != 'undefined' && data.res != '' ) {
typeahead.process(data.res);
}
}
});
},
onselect: function() {
// Do stuff
}
});
};
Please help me to achieve this.
Following tutorial Select2 with AJAX, I am trying retrieve students list with ajax. my input is :
<select class="form-control" id="student_id" name="student_id"></select>
and the script is :
var url = '';
url += "{{ route('api.student_list') }}";
console.log(url); //outputs http://localhost/project-child/public/api/get-students
$( "#student_id" ).select2({
placeholder: 'Select an item',
ajax: {
url: url,
dataType: 'json',
delay: 250,
processResults: function (data) {
return {
results: data
};
},
cache: true
}
});
The JSON response is similar to
[{"id":1,"text":"EJAJUL HAQUE"},{"id":2,"text":"MUSTAK AHMED"},{"id":3,"text":"AZAD HOQUE"},{"id":4,"text":"ANJARUL HAQUE"},{"id":5,"text":"ARIFUL ISLAM"},{"id":6,"text":"SANJITA KHATUN"},{"id":7,"text":"MARINA SULTANA"},{"id":8,"text":"SULTANA BEGUM"},{"id":9,"text":"SABINA KHATUN"},{"id":10,"text":"JENIFA AHMED"},{"id":11,"text":"REJUON AHMED"},{"id":12,"text":"FARITA KHATUN"},{"id":13,"text":"HABIJUL HOQUE"},{"id":14,"text":"JENIFA MUSKAN"},{"id":15,"text":"JOTUFA WAHID"},{"id":16,"text":"MAUSUMI BEGUM"},{"id":17,"text":"ABJARUL RAHMAN"},{"id":18,"text":"JANNATUR ANJU"},{"id":19,"text":"RIYAD "},{"id":20,"text":"RAJIBUL HOQUE"},{"id":21,"text":"UMME SALMA"},{"id":22,"text":"RAFIKUL ISLAM"},{"id":23,"text":"ABJAL HUSSAIN"},{"id":24,"text":"IMAM AHMED"},{"id":25,"text":"NARGIS SULTANA"},{"id":26,"text":"MAFUDA KHATUN"},{"id":27,"text":"SIDDIKUL NAHAR"},{"id":28,"text":"KHALEDA BEGUM"}]
But If I search , say EJAJ it says No results found. I am using select version 4.0.3
on processResults , count data and if its length == 0 then do smt ...
processResults: function (data) {
if(data.length==0){
getFromGoogle();
}
return {
results: data
};
},
Get rid of the whole processResults stuff and make your data server turn your JSON array a subarray with key results. This way:
{"results":[your-JSON-array-above]}
Content is not displaying is autocomplate list as you can see in screenshot below.
I am getting this response.
[{"emp_number":1,"fname":"Arslan","lname":"Hassan"},{"emp_number":2,"fname":"Muneeb","lname":"Janjua"
},{"emp_number":3,"fname":"hr","lname":"user"},{"emp_number":4,"fname":"test","lname":""} .......... REMOVED TO MAKE IT LOOK BETTER HERE .......]
My JS Code:
$( "#search-emp" ).autocomplete(
{
source: function (request, response)
{
var form_data = {
ajax : '1',
name : $("#search-emp").val(),
actioncall : 'search-emp'
};
$.ajax({
//contentType: "application/json",
type: "POST",
dataType: 'json',
url: "_ajax.php",
data: form_data,
success: function( data )
{
response( data );
}
});
},
minLength:3,
select:function(evt, ui)
{
alert(ui.item.emp_number);
}
});
I want to display fname and lname on selected into input field.
The problem it's because jQuery autocomplete wants to have 2 fields named label and value. The content from label will be displayed in the autocomplete.
Because your server return other name for the keys you have 2 options:
Change the server to return a json like:
[{"emp_number":1,"fname":"Arslan","lname":"Hassan", "label":"Arslan Hassan", "value": "Arslan Hassan"},....]
Or on the success callback from ajax create an array with this fields (label and value) and pass this array to response() callback.
You can find more informations here: http://api.jqueryui.com/autocomplete/#option-source
This code is working:
$('input#city').focus(function() {
$(this).autocomplete({
source: '/autosuggest_cities?region_id=' + $('select#region').val(),
minLength: 1,
select: function(event, ui) {
$('#search-box #city_id').attr('value', ui.item.id);
}
});
});
But I don't like it because it hits the database each time the user key ups on the autosuggest text field. I rather hit the databse once and store all possible suggestions inside a variable and set that as the source for the autosuggest. So I tried this:
$.ajax({
url: '/autosuggest_cities',
type: 'get',
data: 'region_id=' + $('select#region').val(),
success: function(output) {
global.available_cities = output;
}
});
$('input#city').focus(function() {
$(this).autocomplete({
source: global.available_cities,
minLength: 1
});
});
When I console.log global.available_cities I do get a json object that looks right, for example:
[{"id":"19184","value":"Aiea"},{"id":"19516","value":"Anahola"},{"id":"20159","value":"Barbers Point"},{"id":"21999","value":"Camp H M Smith"},{"id":"16219","value":"Captain Cook"},{"id":"25135","value":"Eleele"},{"id":"15192","value":"Ewa Beach"},{"id":"26152","value":"Fort Shafter"},{"id":"27539","value":"Haiku"},{"id":"27546","value":"Hakalau"},{"id":"12603","value":"Haleiwa"},{"id":"27657","value":"Hana"},{"id":"11960","value":"Hanalei"},{"id":"27658","value":"Hanamaulu"},{"id":"11838","value":"Hanapepe"},{"id":"27916","value":"Hauula"},{"id":"27931","value":"Hawaii National Park"},{"id":"27933","value":"Hawi"},{"id":"28166","value":"Hickam AFB"},{"id":"8008","value":"Hilo"},{"id":"13747","value":"Holualoa"},{"id":"28457","value":"Honaunau"},{"id":"28470","value":"Honokaa"},{"id":"1221","value":"Honolulu"},{"id":"28471","value":"Honomu"},{"id":"28482","value":"Hoolehua"},{"id":"29284","value":"Kaaawa"},{"id":"29286","value":"Kahuku"},{"id":"8009","value":"Kahului"},{"id":"8010","value":"Kailua"},{"id":"29288","value":"Kailua Kona"},{"id":"11961","value":"Kailua-Kona"},{"id":"18379","value":"Kalaheo"},{"id":"29293","value":"Kalaupapa"},{"id":"29302","value":"Kamuela"},{"id":"8012","value":"Kaneohe"},{"id":"14283","value":"Kapaa"},{"id":"29322","value":"Kapaau"},{"id":"11792","value":"Kapolei"},{"id":"29342","value":"Kaumakani"},{"id":"8013","value":"Kaunakakai"},{"id":"11840","value":"Keaau"},{"id":"11962","value":"Kealakekua"},{"id":"29349","value":"Kealia"},{"id":"29359","value":"Keauhou"},{"id":"29386","value":"Kekaha"},{"id":"11777","value":"Kihei"},{"id":"29536","value":"Kilauea"},{"id":"18876","value":"Koloa"},{"id":"29739","value":"Kualapuu"},{"id":"14166","value":"Kula"},{"id":"29744","value":"Kunia"},{"id":"16950","value":"Kurtistown"},{"id":"8014","value":"Lahaina"},{"id":"12733","value":"Laie"},{"id":"8015","value":"Lanai City"},{"id":"30116","value":"Laupahoehoe"},{"id":"30148","value":"Lawai"},{"id":"8016","value":"Lihue"},{"id":"31035","value":"M C B H Kaneohe Bay"},{"id":"13539","value":"Makawao"},{"id":"31138","value":"Makaweli"},{"id":"31563","value":"Maunaloa"},{"id":"32160","value":"Mililani"},{"id":"32858","value":"Mountain View"},{"id":"15040","value":"Naalehu"},{"id":"33509","value":"Ninole"},{"id":"33931","value":"Ocean View"},{"id":"34146","value":"Ookala"},{"id":"34374","value":"Paauhau"},{"id":"34375","value":"Paauilo"},{"id":"34399","value":"Pahala"},{"id":"34400","value":"Pahoa"},{"id":"18798","value":"Paia"},{"id":"34493","value":"Papaaloa"},{"id":"34494","value":"Papaikou"},{"id":"8018","value":"Pearl City"},{"id":"34671","value":"Pearl Harbor"},{"id":"34774","value":"Pepeekeo"},{"id":"35531","value":"Princeville"},{"id":"12265","value":"Pukalani"},{"id":"35622","value":"Puunene"},{"id":"37200","value":"Schofield Barracks"},{"id":"39401","value":"Tripler Army Medical Ctr"},{"id":"40018","value":"Volcano"},{"id":"13879","value":"Wahiawa"},{"id":"40071","value":"Waialua"},{"id":"13759","value":"Waianae"},{"id":"15947","value":"Waikiki"},{"id":"18353","value":"Waikoloa"},{"id":"8019","value":"Wailuku"},{"id":"40072","value":"Waimanalo"},{"id":"13309","value":"Waimea"},{"id":"8020","value":"Waipahu"},{"id":"40083","value":"Wake Island"},{"id":"40900","value":"Wheeler Army Airfield"}]
But when I start typing in the text field awaitng suggestions I get this bizzare server error instead in my console:
414 Request-URI Too Large
The requested URL's length exceeds the capacity limit for this server.
What am I doing wrong? How can I use a json source for auto suggest without hitting the php script each time on keyup?
Phew got it working. All I had to do was add
dataType: 'json'
to my ajax call and all works fine. Will mark this is right answer in a few days when the system allows me.
try,
$('input#city').focus(function() {
$(this).autocomplete({
source: function( request, response ) {
response( global.available_cities )
},
minLength: 1
});
});
But the recommended way of doing this is,
$('input#city').focus(function() {
$(this).autocomplete({
source: function( request, response ) {
var region_id = $('select#region').val();
if( global.available_cities[region_id] != null ){
response( global.available_cities[region_id] )
}else{
$.ajax({
url: '/autosuggest_cities',
type: 'get',
data: 'region_id=' + region_id,
success: function(output) {
global.available_cities[region_id] = output;
response( global.available_cities[region_id] )
}
});
}
},
minLength: 1
});
});