在select tag(view)中显示json值zend框架2
我的模型:从数据库中获取数据
public function getAllProjectDomain() { $dbadapter = $this->adapter; $sql = new Sql($dbadapter); $select = $sql->select(); $select->columns(array('cmain')); $select->from('projcat'); $selectString = $sql->getSqlStringForSqlObject($select); $result = $dbadapter->query($selectString, $dbadapter::QUERY_MODE_EXECUTE); foreach ($result as $row) { $entity = array( "domain" => $row->cmain ); $entities[] = $entity; } return $entities; }
我的控制器:
public function domainAction() { if ($this->getRequest()->isPost()) { $service = $this->getServiceLocator()->get('UserService'); $result = $service->getAllProjectDomain(); //it will go to service and the method from table $mail = $result; } $results = new JsonModel(array( 'mail' => $mail, )); return $results; }
我的看法:
--Select-- <option value="">
我的jquery:
$(document).ready(function() { $.post("url('checkcourse'); ?>", {}, function(data) { if (data.mail) { document.getElementById('experience').value=data.mail; } else { alert('no data'); } }, "json"); });
在这里,我试图显示下拉选项。 选项列表应来自数据库。 现在我在jquery(视图文件)中获取json值,但我不知道如何使它在select标签中显示。
编辑视图:
> > > > > >
在jquery函数中添加:
$('').appendTo('#experience'); for (var i = 0; i < data.length; i++){ $('').appendTo('#experience'); }
而不是json模型尝试返回一个json数组
$this->getResponse()->setContent(Json::encode($arrayOfData))