"无法读取属性'选项'未定义的“;尝试更新数据网格时的EasyUI

"Cannot read property 'options' of undefined" EasyUI when trying to update datagrid

本文关键字:更新 数据网 EasyUI 网格 数据 未定义 quot 读取 选项 属性      更新时间:2023-09-26

在页面加载时,我希望从本地存储加载JSON,以便能够更新交互式"购物车"。当用户退出页面并返回时,我希望项目仍然存在。

在交互式购物车操作结束时,我将数据保存到本地存储:

localStorage.setItem("datastr", JSON.stringify(data));

这很好,因为当我加载页面时,我会检查本地存储中是否有数据,并记录其详细信息。

if (localStorage.getItem("datastr") !== null) {
    console.log("data found");

然后我将项目添加到一个数组中:

data.rows.push({
            name:loadedarray.name,
            quantity:loadedarray.quantity,
            price:loadedarray.price
        });

但当我尝试用项目更新数据网格时,

$('#cartcontent').datagrid('loadData',loadedarray);

它给了我以下错误:

"Cannot read property 'options' of undefined"

我认为这个错误意味着它试图读取不存在的东西,但我不确定哪里出了问题。

如果有帮助的话,下面是完整的JS文件。

这是我用您提供的数据构建的一个工作测试。希望您可以通过将您的代码与我的代码进行比较来找到您的问题。

function setLocalStorage() {
  var data = [];
  data.push({name: "foo", quantity: 10, price: 4.99});
  data.push({name: "bar", quantity: 4, price: 9.99});
  localStorage.setItem("datastr", JSON.stringify(data));
}
function getLocalStorage() {
  if (localStorage.getItem("datastr") !== null) {
    var loadedarray = JSON.parse(localStorage.getItem("datastr"));
    var data = [];
    data.rows = [];
    for (var i = 0; i < loadedarray.length; i++) {
      var curr = loadedarray[i];
      data.rows.push({
        name: curr.name,
        quantity: curr.quantity,
        price: curr.price
      });
    }
    for (var j = 0; j < data.rows.length; j++) {
      var curr = data.rows[j];
      console.log("row " + j + ": " + curr.name + " " + curr.quantity + " $" + curr.price);
    }
  }
}