Datatables(jquery 插件)未使用 AJAX 填充 table,返回有效 JSON
Datatables(jquery plugin) not populating table with AJAX, valid JSON being returned
我正在尝试使用 DataTable jquery 插件填充此 HTML table,使用 AJAX 调用从 php 脚本,
服务器响应:
{
"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 warning: table id=newsletter_datatable - Requested unknown parameter '0' for row 0. For more information about this error, please see http://datatables.net/tn/4"
我看了其他案例,但乍一看它们是不同的案例.....
编辑:我已经解决了....问题出在服务器上的脚本上,索引需要是数字....现在它正在正确填充table
最终代码:
-现在服务器的响应是(显然插件只接受这种格式,从我在其他人的错误中看到的):
{"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;
}
仅供参考:您只需正确操作即可解决您原来的问题:
columns:[
{ data: 'anio' }, //NOT { 'newsletters': 'anio' }
{ data: 'mes' },
{ data: 'quincena' }
],
使用 data
定义每个新闻通讯项目的哪个属性应该进入该列。
我正在尝试使用 DataTable jquery 插件填充此 HTML table,使用 AJAX 调用从 php 脚本,
服务器响应:
{
"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 warning: table id=newsletter_datatable - Requested unknown parameter '0' for row 0. For more information about this error, please see http://datatables.net/tn/4"
我看了其他案例,但乍一看它们是不同的案例.....
编辑:我已经解决了....问题出在服务器上的脚本上,索引需要是数字....现在它正在正确填充table
最终代码:
-现在服务器的响应是(显然插件只接受这种格式,从我在其他人的错误中看到的):
{"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;
}
仅供参考:您只需正确操作即可解决您原来的问题:
columns:[
{ data: 'anio' }, //NOT { 'newsletters': 'anio' }
{ data: 'mes' },
{ data: 'quincena' }
],
使用 data
定义每个新闻通讯项目的哪个属性应该进入该列。