javascript - 从 json 填充数据表(而不是从 ajax)
问题描述
我正在尝试在单击时填充数据表。最初我有这个配置:
var json = [];
var shippingMethodsTable = $("#shipping-methods-table").DataTable({
'data': json,
"columns": [
{ "data": "ShippingMethodId" },
{ "data": "MethodName"},
{ "data": "Code"},
{ "data": "ShippingTypeName" },
{ "data": "MaxWeight" }
]
});
单击按钮后,我有数组的 json 对象:
json = ko.toJSON(data.shippingMethods); // I am using knockout.js to populate it
结果:
"[{"ShippingMethodId":2,"MethodName":"Priority Mail","Code":null,"ShippingTypeName":"Parcel","MaxWeight":"70 lbs"},{"ShippingMethodId":4,"MethodName":"Priority Mail Express","Code":null,"ShippingTypeName":"Parcel","MaxWeight":"70 lbs"},{"ShippingMethodId":5,"MethodName":"First-Class Mail","Code":null,"ShippingTypeName":"Parcel","MaxWeight":"13 oz"},{"ShippingMethodId":6,"MethodName":"USPS Retail Ground","Code":null,"ShippingTypeName":"Parcel","MaxWeight":"70 lbs"},{"ShippingMethodId":8,"MethodName":"Media Mail","Code":null,"ShippingTypeName":"Parcel","MaxWeight":"70 lbs"}]"
然后我正在尝试更新数据表
shippingMethodsTable.clear();
shippingMethodsTable.rows.add('{"data":' + json + '}');
shippingMethodsTable.draw();
但得到一个错误: Requested unknown parameter 'ShippingMethodId' for row 0, column 0
解决方案
方法rows.add()
需要对象数组,而不是对象。所以,试试
shippingMethodsTable.clear();
shippingMethodsTable.rows.add(json);
shippingMethodsTable.draw();
反而。
推荐阅读
- javascript - asp.net mvc 5-如何在 cshtml 文件的 @using 部分应用 css bootstrap ,js
- c++ - C++ - 问题未定义对 PCM::getInstance() 的引用
- mysql - 如何保留每个用户的最新 N 行并删除 mysql 中的其余行?
- solr - solr facet query sum on distinct values
- html - Html 表格宽度不适合不同的列数
- performance-testing - 如何将 loadrunner 分析数据自动导出到 excel 中?
- c - 有没有办法告诉 C 编译器指针没有别名存储?
- javascript - 如何调用外部网址?
- angular - 更新 formArray 上的值
- javascript - 使用 Node.js 中的 FileSystem(fs) 模块将多个 DOM 元素保存和加载为 JSON 的最简洁方法是什么?