使用 ajax 在 table 中显示数组数据

show array data in table using ajax

我正在使用 Zend Framework 来实现网站。例如,我有一个数组 $shadow 包含一些人的 ID。 $shadow查询在Model中执行,在Controller中调出没有问题。现在我想使用 Ajax 在视图中的 HTML table 中显示数据,因为某些问题,我无法使用 PHP 来执行此操作。谁能告诉我怎么做

假设您有这样的 table 视图:

Html 查看

<table id="myTable">
  <tr>
    <td>Name</td>
    <td>Phone No</td>
  </tr>
</table>

jQueryAjax呼叫

$.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 请求