我创建了mvc格式的数据表。但是当加载scriopt时,它与表数据不匹配,并且不会绑定在我的表行中。我的代码如下。
$.ajax({
// my passing value
success: function (data) { // i get the data properly
$('#Table').dataTable().fnClearTable();
$.each(data, function (k, v) {
var ID= v.ID;
var name= v.name;
var batch= v.batch;
$('#Table').dataTable().fnAddData([
ID,
name,
batch
]);
});
}
});
我的桌子:
<table id="Table" width="100%">
<thead>
<tr>
<th>EMP_ID</th>
<th>EMP_name</th>
<th>EMP_batch</th>
</tr>
</thead>
<tbody></tbody>
</table>
DataTables:http://www.datatables.net/
答案 0 :(得分:0)
问题的关键是您的AJAX请求的响应。确保它是由对象数组组成的正确JSON对象。像这样:
[
{
"ID": 123,
"name": "slim shady",
"batch": "smack my batch up"
},
{
"ID": 321,
"name": "eddie vedder",
"batch": "yellow ledbetter"
}
]
您的JavaScript工作正常:http://jsfiddle.net/upSvJ/