I have one input box and I have applied easy autocomplete, with the help of a class and I have added input button on clicking on that button it adds one more input box with same and I can add as many as input box but easy autocomplete is not working on all the added input box, they have the same class.
var options = {
url: function (phrase) {
return "all_social_media.php";
},
getValue: function (element) {
return element.name;
conole.log(element.name);
},
ajaxSettings: {
dataType: "json",
method: "POST",
data: {
dataType: "json",
}
},
preparePostData: function (data) {
data.phrase = $(".social_media").val();
return data;
console.log(data);
},
requestDelay: 400,
list: {
onSelectItemEvent: function() {
}
}
};
$(".social_media").easyAutocomplete(options);
found the solution, I have to rewrite the code of easy autocomplete code at the time of dynamically generated text box
//create new socials and expenses fields
$("#add_socials").click(function(){
var html_data = $('<div class="row" style="margin-top:10px;"><div class="col-md-5"><input type="text" class="form-control social_media" placeholder="Enter Social Media" name="social_media"></div><div class="col-md-5"><input type="text" class="form-control social_media_expenses" name="social_media_expenses" placeholder="Enter Expenses"></div><div class="col-md-2"><button type="button" class="btn btn-icon btn-danger mr-1" onclick="delete_socials(this)"><i class="fa fa-times"></i></button></div></div>');
$(".add_social_data").append(html_data);
// easy auto complete is not working on dynamic generate text box, need to do this again
var options = {
url: function (phrase) {
return "all_social_media.php";
},
getValue: function (element) {
return element.name;
conole.log(element.name);
},
ajaxSettings: {
dataType: "json",
method: "POST",
data: {
dataType: "json",
}
},
preparePostData: function (data) {
data.phrase = $(".social_media").val();
return data;
console.log(data);
},
requestDelay: 400,
list: {
onSelectItemEvent: function() {
// get_order_id = $("#order_line_entry").getSelectedItemData().order_id;
// console.log(get_order_id);
}
}
};
$(".social_media").easyAutocomplete(options);
html_data.find('.social_media').easyAutocomplete(options);
});
for me, it is working fine.
Related
How to take multiple value when autocomplete with ajax in codeignator
view
<input type="text" class="form-control" name="" id="search">
<div id="result"></div>
ajax
$(document).ready(function() {
$("#search").autocomplete({
minLength: 1,
source: function(request, response) {
$.ajax({
url: "<?php echo base_url(); ?>index.php/Admin/ajaxPro",
dataType: 'json',
crossOrigin: true,
type: 'POST',
data: {
search: request.term
},
success: function(data) {
response(data);
}
});
},
select: function(event, ui) {
$("#result").append(
"<div>" + ui.item.value + "</div>"
);
},
});
});
controller
public function ajaxPro()
{
$term = $this->input->get('term');
// var_dump($term);
$this->db->like('business_name', $term);
$resultSet = $this->db->get("user_table")->result();
$data = [];
foreach($resultSet as $rs){
$data[] = $rs->business_name;
}
header('Content-Type: application/json');
echo json_encode($data);
}
When I retrieve data using autocomplete, the only business_name is attached to the select: function Only the business name is available in the item .value, how do you get the corresponding value of the business name?
Please check the database
Now if I select the business name, how do I append the Email and rating in the result?
I want to make edit form and attach data from database to select2 option. In insert form this select2 option search data in database when we type word by word. I can't attach the data in this select2 option. I want to make this select2 option is fill with the data from database and also can change the data like in insert form. I already try $('#requestor').val($row['requestor']); but the data doesn't appear. Can anyone help me???
<select class="requestor form-control" name="requestor" id="requestor" style="width:700px" required="required">
<option value=""></option>
</select>
//search data in database word by word
$('.requestor').select2({
placeholder: 'Requestor Name',
ajax:{
url: "<?php echo base_url('Hire_4/select_personnel'); ?>",
dataType: "json",
delay: 250,
processResults: function(data){
var results = [];
$.each(data, function(index, item){
results.push({
id: item.ID,
text: item.FullName,
option_value:item.ID
});
});
return{
results: results,
cache: true,
};
},
}
});
function load() {
alert('ok');
//console.log(<?php echo $row['ID']; ?>);
if (<?php echo $row['ID']; ?> != '') {
$('#requestor').val($row['requestor']);
}
}
window.onload = load;
Thank You :)
Your code is very messy. Be more clear like this:
<div class="col-md-12">
<select style="width:100%;" name="requestor[]" id="requestor" class="select2-multiple" multiple></select>
</div>
Then:
var $ajax = $("#requestor");
$ajax.select2({
ajax: {
url: "<?php echo base_url('Hire_4/select_personnel'); ?>",
type: "post",
dataType: 'json',
delay: 250,
data: function (params) {
return{
search: params.term
};
},
processResults: function (data, params) {
params.page = params.page || 1;
return {
results: data
};
},
cache: true
},
language: "en",
placeholder: "Requestor Name",
allowClear: true,
minimumInputLength: 3,
maximumSelectionLength: 1,
escapeMarkup: function (markup) { return markup; },
templateResult: formatRepo,
templateSelection: formatRepoSelection
});
function formatRepo(repo) {
var markup = repo.text ;
return markup;
}
function formatRepoSelection (repo) {
return repo.Name.replace(/<\/?("[^"]*"|'[^']*'|[^>])*(>|$)/g, "");
}
Your Ajax data must be have this format:
data[0]['ID'] = 1; //You must have "ID" key
data[0]['Name'] = 'Name'; //You must have "Name" key
It's easy that's it!
Good luck.
I wanna create array from ajax response. I took array from ajax and i have to create dynamic data for chips
materializecss autocomplete
response from ajax
var tag = '';
$(document).on('keyup', '.tags', function() {
var _this = $(this).val();
if(_this.length > 1) {
$.ajax({
url: '/story/searchTags',
type: 'POST',
dataType: 'JSON',
data: {
tag: _this
},
success: function(tags) {
tag = tags;
}
});
}
});
$('.chips-placeholder').chips({
placeholder: lang_('Your tag'),
secondaryPlaceholder: '+ tag',
autocompleteOptions: {
// I need data like this from response
data: {
'Google', null
'Googol', null
},
limit: 5,
minLength: 2
}
});
<div class="chips chips-placeholder chips-autocomplete input-field" style="background:#fff">
<input class="input tags" placeholder="tag" autocomplete="off" />
</div>
that's my code
Finally, thanks to #Vel to find right answer.
jsfiddle - work code
You need to do like this.
var tag = '';
$(document).on('keyup', '.tags', function() {
var _this = $(this).val();
if(_this.length > 1) {
/*$.ajax({
url: '/story/searchTags',
type: 'POST',
dataType: 'JSON',
data: {
tag: _this
},
success: function(tags) {
tag = tags;
}
});*/
// ['google', 'googol'] <- this array I take from ajax
tag = ['google', 'googol'];
var obj = {};
$(tag).each(function (index, value) {
obj[value] = null;
});
$('.chips').chips({
secondaryPlaceholder: '+ tag',
autocompleteOptions: {
data:obj,
}
});
}
});
fiddle link
I am using Select2 with remote data. when i click on drop-down. no option will shows until write in search box.
But i want to show some data when drop-down open.
I am trying it with initselection or data but its initialize when data present in selected value.
$('.countrySelection').select2({
placeholder: "Select Country",
allowClear: true,
minimumInputLength: 1,
ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
url: base_url+'ajax_response/getCountry',
dataType: 'json',
quietMillis: 100,
data: function (term, page) {
return {
q: term, // search term
page: page
};
},
results: function (data, page) {
$(".statenoSelect").select2("val", "");
$(".citynoSelect").select2("val", "");
var more = (page * 2) < data.page
return {
results: data.items,
more: more
};
},
cache: true
},
initSelection: function(element, callback) {
var id = $(element).val();
if (id !== "") {
$.ajax(base_url+'ajax_response/getCountry?id=' + id, {
dataType: "json"
}).done(function(data) { callback(data['items'][0]); });
}else{
$.ajax(base_url+'ajax_response/getCountry?default=true', {
dataType: "json"
}).done(function(data) { callback(data['items']); });
}
},
data:function(element, callback) {
callback(JSON.parse($(element).attr('data-selection')));
},
formatResult: repoFormatResult,
formatSelection: repoFormatResult,
dropdownCssClass: "bigdrop",
escapeMarkup: function (m) { return m; }
}).focus(function () { $(this).select2('open'); });
Its working with ajax, but how can I add options without search anything
iam trying to show wait message while data is being fetched from database. iam using ajax to fetch the data. the message is already showing but it will disappear quickly. how can i increase the time gap for wait message ,that is the user can view the messge for atleast 4 seconds. Please helps...
<input onchange="mail()" class="span6 m-wrap" type="text" value="" name="mail_id" type="text" id="mail_id" placeholder="Type your mail" />
<div id="loader" style="display:none;">please wait</div>
function mail()
{
var Email=$('#mail_id').val();
$.ajax({
url: "<?echo base_url()?>mailsetr/mail_fetch",
dataType: "json",
type: "POST",
data: {Email:Email},
success: function (res) {
var status = res.user_status;
var name = res.user_name;
var id = res.user_id;
if(status == 1)
{
document.getElementById("name").value =name ;
document.getElementById("id").value =id ;
}
else
{
document.getElementById("name").value ='';
document.getElementById("id").value ='';
}
$('#loader').hide();
},
beforeSend: function ()
{
$('#loader').show();
}
});
}
In Success function use this
$("#loader").fadeOut("3000");
}, 3000);
remove this
beforeSend: function ()
{
$('#loader').show();
}
and paste $('#loader').show(); after $.ajax({