samedi 16 janvier 2016

Sqlite wait for return value using call back using javascript

I am trying to display the data in sqlite db in tabular format. One of the columns in the table are comma separated ids of another table.I have used call back to wait for the comma separated final names to come from the db. But the columns are entered in the end after all the values have been displayed in the table. Please help, this is my code

myDB.transaction(function(tx){
tx.executeSql("SELECT A.survey_crop_master_id,A.crop_area_acres,A.crop_area_cent,A.survey_time_sowing,A.survey_time_harvest FROM survey_crop_cultivation_details A LEFT JOIN item_states B ON A.survey_crop_master_id=B.item_states_id WHERE A.survey_master_id=? ",[1],
function(tx, results)
{
      for(i=0;i<results.rows.length;i++)
      {
        //for each(var obj in results)
        //{

           tr= document.createElement('TR');
         //var item_names;
            var row=results.rows.item(i);
            var cropss = row['survey_crop_master_id'];
            var final_crop_name=[];
            var p=0;
            //alert(cropss);                

            for(j in results.rows[i])
            {
                td = document.createElement('TD');
                if(j=='survey_crop_master_id')
                {
                    var final="";
                    var ayyo=row['survey_crop_master_id'];
                    alert(ayyo);

                    get_item_name(ayyo, function(item_names) {
                    console.log("in main "+item_names);     


                    td.appendChild(document.createTextNode(item_names)); 



                    });
                    console.log(final_crop_name);

                }
                else if((j=='crop_area_acres')||(j=='crop_area_cent'))
                {
                    alert("in else of crop");
                    if(j=='crop_area_acres')
                    {

                    var area=results.rows[i][j]+" Acres "+results.rows[i]['crop_area_cent']+" Cents";
                    td.appendChild(document.createTextNode(area)); 
                    }
                }
                else
                {
                 //td = document.createElement('TD');

                  td.appendChild(document.createTextNode(results.rows[i][j]));  
                }
                 tr.appendChild(td);




            }
             table.appendChild(tr);
              }


});
});
/////////////////////////////////////
function get_item_name(ayyo,callBack)
{
    alert("in ayyo");var final="";
                    myDB.transaction(function(tx){
                      tx.executeSql("SELECT * FROM item_states WHERE item_states_id IN ("+ayyo+")",[],
                      function(tx, results)
                      {
                          alert(results.rows.length);
                          for(k=0;k<results.rows.length;k++)
                          {
                              var row1=results.rows.item(k);
                              final+=row1['item_states_name']+",";
                          }
                          console.log(final);
                          callBack(final);
                         // td.appendChild(document.createTextNode(final));  
                      });
                      });


}

Aucun commentaire:

Enregistrer un commentaire