在选择标签(视图)Zend框架2显示JSON值 [英] display json value in select tag(view) zend framework 2

查看:108
本文介绍了在选择标签(视图)Zend框架2显示JSON值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型:从数据库中读取数据。

my model: to fetch the data from database

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;   

    }

我的控制器:

my controller:

 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;
    }

我的看法:

<div class="form-group"> 
        <label for="experience" class="col-lg-2 control-label">Relevant Experience </label>
        <div class="col-lg-3">
            <select class="form-control" id="experience" name="experience" title="Please select experience">
                 <option value="" selected="selected" >--Select--</option>
         <?php  foreach ($mail as $value) { ?>
  <option value="<?php echo $value ?>"> <?php echo $value ?> </option>
<?php } ?>

            </select>
        </div>
    </div>

我jQuery的:

my jquery:

$(document).ready(function() {

$.post("<?php echo $this->url('checkcourse'); ?>", {},
    function(data) {

        if (data.mail)
        {
            document.getElementById('experience').value=data.mail;
        }
        else
        {
             alert('no data');
    }     
    }, "json");
});

在这里,我想显示的下拉选项。选项​​列表应来自datbase。现在,我收到jQuery中(查看文件)json的价值,但我不知道如何使它在选择标记来显示。

Here i am trying to display the drop down option. The list of option should come from the datbase. Now i am getting the json value in jquery(view file), but i dont know how to make it to display in select tag.

推荐答案

编辑观点:

> <div class="form-group"> 
    >         <label for="experience" class="col-lg-2 control-label">Relevant Experience </label>
    >         <div class="col-lg-3">
    >             <select class="form-control" id="experience" name="experience" title="Please select experience">
    >                  
    > 
    >             </select>
    >         </div>
    >     </div>

在jQuery的功能补充一点:

in the jquery function add this:

$('<option value="">'+'Select Action...'+'</option>').appendTo('#experience');
               for (var i = 0; i < data.length; i++){

                   $('<option value="'+data[i]['mail']+'">'+data[i]['mail']+'</option>').appendTo('#experience');
                }

而不是一个JSON模式

试图返回JSON数组

instead of a json model try to return a json array

$this->getResponse()->setContent(Json::encode($arrayOfData))

这篇关于在选择标签(视图)Zend框架2显示JSON值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆