bryancresswell bryancresswell - 1 month ago 8
jQuery Question

Unable to display my list entry on my app [jquery mobile]

function scan()
{
cordova.plugins.barcodeScanner.scan(
function (result) {
if(!result.cancelled)
{
if(result.format == "QR_CODE")
{
var value = result.text;
var data = localStorage.getItem("LocalData");
console.log(data);
data = JSON.parse(data);
data[data.length] = [value];
localStorage.setItem("LocalData", JSON.stringify(data));
location.reload();
appendToList();
// location.hash = '#Vote';
}
}
},
function (error) {
alert("Scanning failed: " + error);
}
);
}
var count = 1;
var listCreated = false;
function appendToList() { //Create the listview if not created
if(!listCreated){
$("#content").append("<ul id='list' data-icon = 'true' data-role='listview' data-inset='true' data-split-icon = 'delete' data-split-theme='a'></ul>");
listCreated = true;
$("#content").trigger("create");
}
$("#list").append("<li id=count"+count+"><a href=#>" + data[0] + "</a><a href='#delete' data-rel = 'popup' data-position-to='window' data-transition='pop'>Delete</a></li>");
$("#list").listview("refresh")
count ++;
}


Is something wrong with my code? I'm trying to output the newest data from my array onto my list. I've tried scanning a QR code countless of times, but it's not working. I want to scan a QR code with text, and then use that text as a new entry in listview.

Answer
    var data; //this should be global variable
    function scan() {
      cordova.plugins.barcodeScanner.scan(
        function(result) {
          if (!result.cancelled) {
            if (result.format == "QR_CODE") {
              var value = result.text;
              if(!localStorage.getItem("LocalData"))localStorage.setItem("LocalData","[]")
              data = localStorage.getItem("LocalData");
              //alert(data);
              data = JSON.parse(data);
              data.push(value);
              localStorage.setItem("LocalData", JSON.stringify(data));
              //why you need to refresh page next line
              //location.reload();
              appendToList();
              // location.hash = '#Vote';
            }
          }
        },
        function(error) {
          alert("Scanning failed: " + error);
        }
      );
    }


    function appendToList() { //Create the listview if not created
      if (!$("#content").length) {
        $("#content").append("<ul id='list' data-icon='true' data-role='listview' data-inset='true' data-split-icon='delete' data-split-theme='a'></ul>");
        $("#content").trigger("create");
      }
        var nveiw = 3 + ((+data.length+1)/1000);
        $("#list").append("<li id='count" + data.length + "'><a href='#details="+(+data.length-1)+"'>" + nveiw + "</a><a href='#delete' data-rel='popup' data-position-to='window' data-transition='pop'>Delete</a></li>");

      $("#list").listview("refresh");
    }
Comments