gpt4 book ai didi

javascript - 从 json(而不是 ajax)填充数据表

转载 作者:行者123 更新时间:2023-12-03 00:06:14 26 4
gpt4 key购买 nike

我正在尝试点击时填充数据表。最初我有这样的配置:

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();

但出现错误:请求第 0 行第 0 列的未知参数“ShippingMethodId”

最佳答案

方法rows.add()需要对象数组,而不是对象。所以,尝试一下

shippingMethodsTable.clear();
shippingMethodsTable.rows.add(json);
shippingMethodsTable.draw();

相反。

关于javascript - 从 json(而不是 ajax)填充数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54956390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com