我创建了一个json对象,想要读取内容并将值放入dataTable.But我无法读取Json对象的内容。
以下是我的javascript函数:
finalGrid : function(data){
console.log("final Grid");
var strJson = JSON.stringify(data);
var dataJson = JSON.parse(strJson);
console.log(dataJson);
var table = $('#' + Dcn.tableId).dataTable();
var tableObj = table.api();
var rows = [];
var field = Dcn.tableField;
var count=0;
$.each(dataJson, function(index, obj) {
var row = [];
row[field.dcnRefNo.index] = obj[field.dcnRefNo.name];
row[field.name.index] = obj[field.name.name]";
row[field.package.index] = obj[field.package.name];
row[field.priority.index] = obj[field.priority.name];
row.push("<button class='btn btn-labeled btn-success project-edit' " +
"onclick=\"Dcn.List.bindDcnStatusUpdate('"+obj["_id"]+"')\"> " +
"<span class='btn-label'><i class='fa fa-edit'></i></span>View Dcn</button>");
rows.push(row);
count = count+1;
console.log(count);
});
tableObj.rows.add(rows).draw();
$('#' + Dcn.tableId).show();
}
怎么办?
答案 0 :(得分:0)
如果你的json看起来像这样:
{"package": "package-a", "priority": "1", "dcnRefNo": "DCN/Duraline/001", "name": "abc"}
您可以使用此代码示例:
var data = [{"package": "package-a", "priority": "1", "dcnRefNo": "DCN/Duraline/001", "name": "abc"}];
function printJson(data){
var strJson = JSON.stringify(data);
var dataJson = JSON.parse(strJson);
$.each(dataJson, function(index, obj) {
console.log(obj.dcnRefNo);
console.log(obj.name);
console.log(obj.package);
console.log(obj.priority);
});
}