I am new in Zend Framework. I am trying to display data from database using JSON. And I encoded the data and passed it to JQuery. But cannot retrieve value from database. Data displayed as "undefined". My controller function is as follows:
public function displayAction()
{
$data1 = array();
$request = $this->getRequest();
$response = $this->getResponse();
if ($request->isPost()) {
$response->setContent(\Zend\Json\Json::encode(array('data' => $this-> getStickyNotesTable() -> fetchAll())));
}
return $response;
}
My FetchAll() is:
public function fetchAll() {
$resultSet = $this->select(function (Select $select) {
$select->order('created ASC');
});
$entities = array();
foreach ($resultSet as $row) {
$entity = new Entity\StickyNote();
$entity->setId($row->id)
->setNote($row->note)
->setCreated($row->created);
$entities[] = $entity;
}
return $entities;
}
JQuery function :
function getUserList(element) {
$('#indicator').show();
$.post('stickynotes/display',
function(data, textStatus) {
renderUserList(data);
$('#indicator').hide();
},
"json"
);
}
function renderUserList(jsonData) {
var table = '<table width="600" cellpadding="5" class="table table-hover table-bordered"><thead><tr><th scope="col">Note</th></tr></thead><tbody>';
$.each(jsonData, function(index, data){
table += '<tr>';
table += '<td class="edit" field="note" user_id="'+data.id+'">'+data.note+'</td>';
table += '<td><a href="javascript:void(0);" user_id="'+data.id+'" class="delete_confirm btn btn-danger"><i class="icon-remove icon-white"></i></a></td>';
table += '</tr>';
});
table += '</tbody></table>';
$('div#content').html(table);
}
I tested it using Firebug. It shows
{"data":[{},{},{},{},{},{},{},{},{},{},{},{},{}]}
as Response. Anyone please help me. Thanks.
You'll need to configure your module.config.php
and add a strategy within your template_map add.
'strategies' => array(
'ViewJsonStrategy',
),
to return a jsonModel.
If you want to work with a jsonModel within your controller you'll need to call it like so:
$json = new JsonModel(array(
'param' => 'foobar',
'success' => true,
));
return $json;
The issue is with your fetchAll method. Try with this updated version:
public function fetchAll() {
$resultSet = $this->select(function (Select $select) {
$select->order('created ASC');
});
$entities = array();
foreach ($resultSet as $row) {
$entity = array(
"id" => $row->id,
"note" => $row->note,
"created" => $row->created
);
$entities[] = $entity;
}
return $entities;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.