简体   繁体   中英

how to return a value from function to another in js using phonegap android

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 getdatafromtable function is called it should create fill the data in the array object and pass that arrayobject back to the createlist function 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.

Assuming your getdatafromtable function is properly made to return the array

var data_array = getdatafromtable(autocompleteData);

and in your getdatafromtable function you should have a return statement returning an array

Also dont name your variable the same as an already existing function. autocompleteData in this case is both a local variable and a 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');
        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,  $ul, $input, value, html) {
    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;
                }
            }
            after_data_retrieved(autocompleteData, $ul, $input, value, html);
        });
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM