0

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.

1 Answer 1

1

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);
        });
    });
}
Sign up to request clarification or add additional context in comments.

13 Comments

i have update the code as you said but nothing change still it not returning the value from getdatafromtable to createlist function..
I am getting an error in logcat 09-04 10:34:07.788: D/CordovaLog(5466): Uncaught TypeError: Cannot read property 'length' of undefined 09-04 10:34:07.788: E/Web Console(5466): Uncaught TypeError: Cannot read property 'length' of undefined at file:///android_asset/www/js/demoautocomplete.js:21
you put you return statement in the wrong area of the function. Move is so that it is called after everything else in the function
can you tell me were exactly should i put the return method and did you check the edited code do i calling the function properly and storing the return in variable correctly.
not sure why you added value as a parameter now you will have to call getdatafromtable(somevar, someothervar) and it doesnt seem to actually do anything
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.