如何使用zf2做ajax + json?

时间:2014-07-06 18:13:21

标签: javascript jquery json zend-framework2 zfcuser

我正在使用zf2。我想通过使用ajax调用加载我的第二个下拉列表。我试过以下代码。我可以获得硬编码值。但我不知道如何将数据库值添加到数组并使用ajax将值加载到下拉列表。

phtml中的Ajax:

<script type="text/javascript">
$(document).ready(function () {

$("#projectname").change(function (event) {

    var projectname = $(this).val();
    var projectkey = projectname.split(" - ");
    var projectname = {textData:projectkey[1]};


    //The post using ajax
    $.ajax({
            type:"POST",
            // URL : / name of the controller for the site / name of the action to be                         
            //                                                 executed
            url:'<?php echo $this->url('userstory', array('action'=>'answer')); ?>',
            data:projectname,
            success: function(data){ 

                //code to load data to the dropdown

            },
            error:function(){alert("Failure!!");}


           });

   });
  });

</script>

控制器操作:

  public function answerAction() {

    // ead the data sent from the site
    $key = $_POST ['textData'];

    // o something with the data
    $data= $this->getProjectTable ()->getkeyproject( $key );
    $projectid = $data->id;

    $projectusers[] = $this->getRoleTable()->fetchRoles($projectid);
    // eturn a Json object containing the data

    $result = new JsonModel ( array (
            'projectusers' => $projectusers
    ) );
    return $result;
}

数据库查询:

   public function fetchRoles($id) {
    $resultSet = $this->tableGateway->select ( array (
            'projectid' => $id 
    ) );

    return $resultSet;

}

2 个答案:

答案 0 :(得分:0)

你的json对象new JsonModel ( array ( 'projectusers' => $projectusers ) json对象变成这种格式Click here for Demo

   var projectkey = [];

       projectkey =  projectname.split(" - ");
       var projectname = { "textData" : "+projectkey[1]+" };

       $.ajax({
        type:"POST",
        url     :  "url.action",
        data    :   projectname,
        success :  function(data){ 

            $.each(data.projectusers,function(key,value){
                 $('#divid').append("<option value="+key+">"+value+"</option>");
            });
           });
        });



     <select id="divid"></select>

答案 1 :(得分:0)

这就是我在控制器中所做的。完成了编码。

public function answerAction() {

    // ead the data sent from the site
    $key = $_POST ['textData'];

    // o something with the data
    $data= $this->getProjectTable ()->getkeyproject( $key );
    $projectid = $data->id;
    $i=0;
    $text[0] = $data->id. "successfully processed";
    $projectusers = $this->getRoleTable()->fetchRoles($projectid);

    foreach ($projectusers as $projectusers) :
    $users[$i][0] = $projectusers->username;
    $users[$i][1] = $projectusers->id;
    $i++;
    // eturn a Json object containing the data
    endforeach;
    $result = new JsonModel ( array (
            'users' => $users,'count'=>$i
    ) );
    return $result;
}

和ajax是这样的

<script type="text/javascript">
 $(document).ready(function () {

$("#projectname").change(function (event) {

    var projectname = $(this).val();
    var projectkey = projectname.split(" - ");
    var projectname = {textData:projectkey[1]};


    //The post using ajax
    $.ajax({
            type:"POST",
            // URL : / name of the controller for the site / name of the action to be                         
            //                                                 executed
            url:'<?php echo $this->url('userstory', array('action'=>'answer')); ?>',
            data:projectname,
            success: function(data){ 
                // alert(data.users[0][0]+" - " + data.users[0][1] );

                 var count= data.count;
                 alert(count);
                 $('#myDropDown').empty();
                 for(var i=0;i<count;i++){

                     $('#myDropDown').append($('<option></option>').attr('value', data.users[i][1]).text(data.users[i][0]));

                 }

            },
            error:function(){alert("Failure!!");}


           });

});
  });

 </script>

使用相同的zf2查询来访问数据库。感谢大家的帮助:)。