I am creating an app in which i am using jquery mobile autocomplete. To create the listview i am calling a function in js file. When a user enter the character in the input field it will call the js file and then i want to call another function which is return to access the data from data base and it will create and array object and this created array i want to pass back to the function called and then it will create an li base on that array. Here is the code inside the demoautocomplete.js
function createlist(autocomplete_name){
var objdata=['user_name','user_id'];
$( "#"+autocomplete_name).on("listviewbeforefilter", function ( e, data ) {
var autocompleteData=new Array();
var $ul = $( this ),
$input = $( data.input ),
value = $input.val(),
html = "";
$ul.html( "" );
getdatafromtable(autocompleteData,value); var dataarray=getdatafromtable(autocompleteData);
if ( value && value.length > 1 )
{
$.mobile.loading('hide');
for(var j=0;j<dataarray.length;j++)
{
html +="<li id='"+dataarray[j].id+"'>"+dataarray[j].user_name+"</li>";
}
$ul.html( html );
$ul.listview( "refresh" );
$ul.trigger( "updatelayout");
$.mobile.activePage.find('input[placeholder="Find a name..."]').attr('id','autocomplete');
}
$("ul>li").click(function()
{
var textval=$(this).text();
var id=$(this).attr('id');
$('#autocomplete').val(textval);
$.mobile.activePage.find("[data-role=listview]").children().addClass('ui-screen-hidden');
storeselectedid(id,autocompleteData);
});
});
}
function getdatafromtable(autocompleteData,value){
db.transaction(function(tx){
$.mobile.loading('show');
var selectQuery='select first_name||" "||last_name user_name,user_id from users where first_name like "%'+value+'%" or last_name like "%'+value+'%" limit 10';
var selectQuery1='select account_name user_name,account_id from crm_account where account_name like "%'+value+'%" limit 10';
tx.executeSql(selectQuery,[],function(tx,results){
var dataset=results.rows;
if(dataset.length>0){
for(var i=0;i<dataset.length;i++)
{
var item=dataset.item(i);
var element = new Object();
element['id']=autocomplete_name+"_"+i;
for(var j=0;j<objdata.length;j++)
{
element[objdata[j]]=item[objdata[j]];
}
autocompleteData[i]=element;
}
return autocompleteData;
}
});
}); }
here the script code in html from where js will be called:
$(function(){
<ul id="autocomplete" data-role="listview" data-inset="true" data-filter="true" data-filter-placeholder="Find a name..."
data-filter-theme="d" >
</ul>
var autocomplete=$(document.createElement('ul')).attr('id',autocomplete_name);
autocomplete.attr('data-role','listview');
autocomplete.attr('data-inset',true);
autocomplete.attr('data-filter',true);
autocomplete.attr('data-filter-placeholder','Find a name');
autocomplete.appendTo("#contentDemo");
createlist(autocomplete_name); });
when getdatafromtablefunction is called it should create fill the data in the array object and pass that arrayobject back to the createlistfunction and then if loop should be executed.
here is the flow of code how it should work :
1. the page is loaded then when user enters a character in the input field.
2.it will go to thejs file then input value is assign to value variable. which i want to pass to the function getdatafromtable so base o that input it fetch the data from the database.
3.the retrive is stored in the array object than its pass back to the function from where it was called.
4.After retriving the array data it should create the li listview.
Any help is appreciated.
Thanks in advance.