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 ++;
}
我的代码有问题吗?我正在尝试将我的数组中的最新数据输出到我的列表中。我曾尝试无数次扫描QR码,但它无法正常工作。我想用文本扫描QR码,然后将该文本用作列表视图中的新条目。
答案 0 :(得分:0)
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");
}
答案 1 :(得分:0)
我通过实现重置功能来重置本地存储数据来解决它。