【问题标题】:show array data in table using ajax使用ajax在表格中显示数组数据
【发布时间】:2015-07-09 05:16:59
【问题描述】:
我正在使用 Zend Framework 来实现网站。例如,我有一个数组 $shadow 包含一些人的 ID。 $shadow 查询在 Model 中执行,在 Controller 中调用没有问题。现在,由于某些问题,我想使用 Ajax 在 View 中的 HTML 表中显示数据,我无法使用 PHP 来执行此操作。谁能教我怎么做
【问题讨论】:
标签:
php
jquery
mysql
ajax
zend-framework
【解决方案1】:
假设您在视图中有这样的表格:
HTML 视图
<table id="myTable">
<tr>
<td>Name</td>
<td>Phone No</td>
</tr>
</table>
jQuery Ajax 调用
$.ajax({
url : 'url_path_of_controller',
type : 'POST',
datatype : 'JSON',
success : function(data){
// here we populate data returned by controller
// if returned data is a plain array, then parse into javascript obj
var d = $.parseJSON(data);
// d = [{ name : 'john', phone : 123 }]; --> example
// this depend on your returned data from controller
var output;
$.each(d,function(i,e) {
// here you structured the code depend on the table of yours
output += '<tr><td>'+e.name+'</td><td>'+e.phone+'</td></tr>';
});
// after finish creating html structure, append the output
// into the table
$('#myTable').append(output);
}
});
控制器
function bla(){
$shadow = call_from_model
echo json_encode($shadow);
}
DEMO => 没有 ajax 请求