我试图用DataTable jquery填充这个DataTable,使用从php脚本接收这个JSON的AJAX调用,
来自服务器的响应:
{
"newsletters": [{
"anio": "2016",
"mes": "1",
"quincena": "1"
}, {
"anio": "2016",
"mes": "1",
"quincena": "2"
}]
}HTML文件:
<div id="tabla_newsletters" >
<table id="newsletter_datatable">
<thead>
<tr>
<th>anio</th>
<th>mes</th>
<th>quincena</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<script>
$(document).ready(function(){
var table = $('#newsletter_datatable').DataTable( {
ajax: {
url: '/newsletter/getNewsletters',
dataSrc: 'newsletters'
},
columns:[
{ 'newsletters': 'anio' },
{ 'newsletters': 'mes' },
{ 'newsletters': 'quincena' }
],
} );
});
</script>然后,我的php服务(在symfony 1.4中创建),正如我前面所述,是根据JSON在线验证器返回正确的JSON:
public function executeGetNewsletters(sfWebRequest $request){
$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
$qry=$conn->execute("Select anio,mes,quincena from newsletters");
$newsletters = $qry;
$dataNews = array();
$i=0;
foreach ($newsletters as $news)
{
$dataNews[$i] = array(
"anio" => $news['anio'],
"mes" => $news['mes'],
"quincena" => $news['quincena'],
);
++$i;
}
$output = array(
"newsletters" => $dataNews,
);
$json=$this->renderText(json_encode($output));
return $json;
}Datatable引发以下错误:
"DataTables警告:表id=newsletter_datatable -请求行0的未知参数'0‘。有关此错误的更多信息,请参见http://datatables.net/tn/4
我看过其他案件,但乍一看,它们是不同的.
编辑:我已经解决了it....the问题是在服务器上的脚本上,索引需要是numbers....now --它正在正确地填充表
最终代码:
服务器上的-Response现在是(显然插件只接受我在其他人的bug中看到的这种格式):
{"newsletters":[["2016","1","1"],["2016","1","2"]]}-HTML文件代码是:
<div id="tabla_newsletters" >
<table id="newsletter_datatable">
<thead>
<tr>
<th>anio</th>
<th>mes</th>
<th>quincena</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
<script>
$(document).ready(function(){
var table = $('#newsletter_datatable').DataTable( {
ajax: {
url: '/newsletter/getNewsletters',
dataSrc: 'newsletters'
}
} );
});
</script>而php服务代码是(我只将关联数组的索引从字段名更改为数字):
public function executeGetNewsletters(sfWebRequest $request){
$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
$qry=$conn->execute("Select anio,mes,quincena from newsletters");
$newsletters = $qry;
$dataNews = array();
$i=0;
foreach ($newsletters as $news)
{
$dataNews[$i] = array(
"0" => $news['anio'],
"1" => $news['mes'],
"2" => $news['quincena'],
);
++$i;
}
$output = array(
"newsletters" => $dataNews,
);
$json=$this->renderText(json_encode($output));
return $json;
}发布于 2016-05-03 06:06:41
FYI :你本可以通过正确的方法解决原来的问题:
columns:[
{ data: 'anio' }, //NOT { 'newsletters': 'anio' }
{ data: 'mes' },
{ data: 'quincena' }
], 使用data定义应该进入列中的每个新闻稿项中的哪个属性。
https://stackoverflow.com/questions/36989812
复制相似问题