我有一个php文件(ads.php),它返回包含记录集的json对象数组
数组是
[{"hea":{"0":"Kidney Stone Removal"},"id":{"0":"16282238572"},"desc":{"0":"Get treated at top kidney center"},"desc2":{"0":"Take a free advice from our experts"},"url":{"0":"www.ainuindia.com"},"cli":{"0":"1"},"cpc":{"0":"0"},"con":{"0":"0"},"cost":null,"ctr":{"0":"5.26%"},"imp":{"0":"19"},"ap":{"0":"2.2"}}]
我的java Srcript是
$("#example1").dataTable();
$("#groupid").change(function(){
var oTable = $('#example1').dataTable();
var grpvalue=$('#groupid').val();
$.ajax({
type:"post",
dataType : 'json',
url:"pages/ads.php",
data:"adgroup="+grpvalue,
success: function(s) {
oTable.fnClearTable();
for(var i = 0; i < s.length; i++) {
oTable.fnAddData([
s[i]['hea'],
s[i]['id'],
s[i]['desc'],
s[i]['desc2'],
s[i]['url'],
s[i]['cli'],
s[i]['cpc'],
s[i]['con'],
s[i]['cost'],
s[i]['ctr'],
s[i]['imp'],
s[i]['ap']
]);
}
}
});
});
和Html数据表是
<table id="example1" class="table table-bordered table-striped num-right-alignct">
<thead>
<tr>
<th style="text-align: center;">Ad Headline</th>
<th style="text-align: center;">Ad ID</th>
<th style="text-align: center;">Ad Description 1</th>
<th style="text-align: center;">Ad Description 2</th>
<th style="text-align: center;">URL Appeared</th>
<th style="text-align: center;">Clicks</th>
<th style="text-align: center;">CPC</th>
<th style="text-align: center;">Conversions</th>
<th style="text-align: center;">CTR %</th>
<th style="text-align: center;">Impressions</th>
<th style="text-align: center;">Avg Pos</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
当我检索这些值时,整个数据只嵌入到第一列而不是其他列中,如何将这些值放入数据表中并填充所有列和行?
答案 0 :(得分:0)
尝试以下代码。希望这会帮助你。
$("#example1").dataTable();
$("#groupid").change(function(){
var oTable = $('#example1').dataTable();
var grpvalue=$('#groupid').val();
var col = ["hea", "id", "desc", "desc", "desc2", "url", "cli","cpc","con","cost","ctr","imp","ap"];
$.ajax({
type:"post",
dataType : 'json',
url:"pages/ads.php",
data:"adgroup="+grpvalue,
success: function(s) {
oTable.fnClearTable();
for(var i = 0; i < s.length; i++) {
$("#example1 tr:first td:nth-child("+i+")" ).html(s[i][col[i]]);
}
}
});
});