javascript - how to return a value from function to another in js using phonegap android -
i creating app in using jquery mobile autocomplete. create listview calling function in js file. when user enter character in input field call js file , want call function return access data data base , create , array object , created array want pass function called , create li base on array. here code inside 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 = $( ), $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 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 users first_name "%'+value+'%" or last_name "%'+value+'%" limit 10'; var selectquery1='select account_name user_name,account_id crm_account account_name "%'+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 script code in html js called:
$(function(){ <ul id="autocomplete" data-role="listview" data-inset="true" data-filter="true" data-filter-placeholder="find 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 name'); autocomplete.appendto("#contentdemo"); createlist(autocomplete_name); });
when getdatafromtable
function called should create fill data in array object , pass arrayobject createlist
function , if loop should executed. here flow of code how should work : 1. page loaded when user enters character in input field. 2.it go thejs file input value assign value variable. want pass function getdatafromtable base o input fetch data database. 3.the retrive stored in array object pass function called. 4.after retriving array data should create li listview. appreciated. in advance.
assuming getdatafromtable
function made return array
var data_array = getdatafromtable(autocompletedata);
and in getdatafromtable
function should have return statement returning array
also dont name variable same existing function. autocompletedata
in case both local variable , function name.
edit:
$("#" + 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, $ul, $input, value, html); }); function after_data_retrieved(autocompletedata, $ul, $input, value, html) { if (value && value.length > 1) { $.mobile.loading('hide'); (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 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, $ul, $input, value, html) { db.transaction(function (tx) { $.mobile.loading('show'); var selectquery = 'select first_name||" "||last_name user_name,user_id users first_name "%' + value + '%" or last_name "%' + value + '%" limit 10'; var selectquery1 = 'select account_name user_name,account_id crm_account account_name "%' + value + '%" limit 10'; tx.executesql(selectquery, [], function (tx, results) { var dataset = results.rows; if (dataset.length > 0) { (var = 0; < dataset.length; i++) { var item = dataset.item(i); var element = new object(); element['id'] = autocomplete_name + "_" + i; (var j = 0; j < objdata.length; j++) { element[objdata[j]] = item[objdata[j]]; } autocompletedata[i] = element; } } after_data_retrieved(autocompletedata, $ul, $input, value, html); }); }); }
Comments
Post a Comment