您可以做的是动态构建您的表,然后将其发送到数据表。
在以下示例中,我假设 Json 在 jQuery Ajax 调用中返回,并且您的响应中有多个项目:
$.ajax({
type: "POST",
url: "Controller Url",
contentType: "application/json; charset=utf-8",
dataType: "json",
...
...
success: function (response) {
// Assuming you need to parse, you may not need to depending on how the data is returned.
var parsedJson = $.parseJSON(response);
// Empty var to hold the table content.
var buildTable = "";
$.each(parsedJson, function(i)) {
buildTable += "<tr>";
buildTable += "<td>" + parsedJson[i].buzz + "</td>";
buildTable += "<td>" + parsedJson[i].attributeValue.name + "</td>";
buildTable += "<td>" + parsedJson[i].id + "</td>";
buildTable += "</tr>";
});
// Now fill your datatable with the table content in the order you want and with only the fields required.
$("#datatableContent").append(buildTable);
// Configure your datatable
$('#datatableContent').dataTable({ });
} //end success
});// end ajax
在您的视图中,您将拥有:
<table id="datatableContent">
<thead>
<tr>
<th>Buzz</th>
<th>Name</th>
<th>ID</th>
</tr>
</thead>
<tbody>
</tbody>
要记住的一件事是,如果我$('#datatableContent').dataTable({ });
在 ajax 调用成功的情况下放置了配置部分,我就会遇到问题。我$("#datatableContent").append(buildTable);
在成功的线路之后放置了地雷,并且每次都能完美运行。