从另一个动作设置视图参数

时间:2013-08-02 16:51:12

标签: php zend-framework view

所以我的zend框架应用程序和我试图将视图sucess var设置为true以显示成功的messega。 所以在我的choosetags.phtml中我得到了这个

<script language = "Javascript">
    $(document).ready(function() {
    $("#button").click(function () {
       var param1 = 'first';  //or get value from some DOM element
       var param2 = 'second'; //or get value from some DOM element

       $.ajax({
       url: '/rondas/saveronda',
       type: 'POST',
       data: {param1: param1, param2:param2 },
       datatype: "json"

});

    }); 
});

</script>
<?php if($this->success): ?>
    <div class="albox succesbox" style="z-index: 690;">
        <b>Sucesso :</b> A Palavra-passe foi alterada com sucesso. 
    </div>
<?php endif; ?>

<?php if($this->hasError): ?>
    <div class="albox errorbox" style="z-index: 690;">
        <b>Erro :</b> <?php echo $this->errorMessage; ?> 
    </div>
<?php endif; ?>
<div class="simplebox grid740" style="z-index: 500;">
    <div class="titleh" style="z-index: 400;">
        <h3>Criar Ronda</h3>
    </div>
    <div class="body" style="z-index: 380;">



    <script type="text/javascript">
    var t=0;
     $(function(){

             $("#toolbar").jqGrid({ 
                caption:"Tags",
                colNames:['ID','Nome','Cliente','Descrição','Modo','Estado'],
                colModel:[

                    {name:'id_tag',index:'id_tag',hidden:true},
                    {name:'tag_nome',index:'tag_nome'},
                    {name:'nome',index:'nome'},
                    {name:'descricao',index:'descricao'},
                    {name:'modo',index:'modo'},
                    {name:'estado',index:'estado'}


                ],
                datatype:"json",
                height:421, 
                rownumWidth:40,
                pager:'#ptoolbar',
                rowList:[10,20,30],
                rowNum:10,
                sortname:'id_tag',
                sortorder:'desc',

                viewrecords:true,
                width:700
            });
            $("#toolbar").jqGrid('navGrid','#ptoolbar',{del:false,add:false,edit:false,search:false});
            $("#toolbar2").jqGrid({ 
                caption:"Tags da Ronda",
                colNames:['ID','Nome','Cliente','Descrição','Modo','Estado'],
                colModel:[
                     {name:'id_tag',index:'id_tag',hidden:true},
                    {name:'tag_nome',index:'tag_nome'},
                    {name:'nome',index:'nome'},
                    {name:'descricao',index:'descricao'},
                    {name:'modo',index:'modo'},
                    {name:'estado',index:'estado'}


                ],
                datatype:"json",
                height:421, 
                rownumWidth:40,
                pager:'#ptoolbar2',
                rowList:[10,20,30],
                rowNum:10,
                sortname:'id_tag',
                sortorder:'desc',

                viewrecords:true,
                width:700
            });
            $("#toolbar2").jqGrid('navGrid','#ptoolbar2',{del:false,add:false,edit:false,search:false});
            $("#toolbar").jqGrid('gridDnD',{connectWith:'#toolbar2',dragcopy:true,beforedrop: function (ev, ui, getdata, $source, $target) {
        var names = $target.jqGrid('getCol', 'id_tag');

        if ($.inArray(getdata.id_tag, names) >= 0) {
            // prevent data for dropping
            ui.helper.dropped = false;
            alert("A tag ja existe na ronda");

        }
     else
        {

          document.getElementById("tagini").innerHTML= document.getElementById("tagini").innerHTML +"<option value="+getdata.id_tag+">"+getdata.tag_nome+','+getdata.nome+','+getdata.descricao+','+getdata.modo+"</option>";
document.getElementById("tagfim").innerHTML= document.getElementById("tagfim").innerHTML +"<option value="+getdata.id_tag+">"+getdata.tag_nome+','+getdata.nome+','+getdata.descricao+','+getdata.modo+"</option>";        }}});

    });

    $(function(){
        // Attach the dynatree widget to an existing <div id="tree"> element
        // and pass the tree options as an argument to the dynatree() function:
        $("#tree").dynatree({
            initAjax: {
        url: "/locais/tree"
        },
        onActivate: function(node) {
      t=node.data.key;
      if(t!=0)
      { 

      $("#echoActive").text(node.data.title);
      $("#tables").show();
       $("#toolbar").jqGrid('setGridParam',{url:"/rondas/choosetags/id/"+t}).trigger("reloadGrid");
       reloadGrid();


      }
      else
      {          
      $("#echoActive").text("-");    
       $("#tables").hide();

      } 

        }
        });


    });


    </script>
   <div class="st-form-line" style="z-index: 680;">
<span class="st-labeltext">Escolher a localização:</span>
<div id="tree" > </div>


<div class="clear" style="z-index: 670;"></div>
</div>
 <!-- Add a <div> element where the tree should appear: -->
<div class="st-form-line" style="z-index: 680;">
<span class="st-labeltext">Localização:<span id="echoActive">-</span></span>
<div id="tables" style="display:none" >


    <table id="toolbar"></table>
    <div id="ptoolbar"></div>
    </div>

<div class="clear" style="z-index: 670;"></div>
</div>

<div class="st-form-line" style="z-index: 680;">
<span class="st-labeltext">Tags da ronda:</span>



    <table id="toolbar2"></table>
    <div id="ptoolbar2"></div>


<div class="clear" style="z-index: 670;"></div>
</div>  

<div class="st-form-line" style="z-index: 680;">
<span class="st-labeltext">Nome da ronda:</span>

   <input type="text" name="nomeronda" id="nomeronda">




<div class="clear" style="z-index: 670;"></div>
</div>     
 <div class="st-form-line" style="z-index: 680;">
<span class="st-labeltext">Tag Inicial:</span>


<select id="tagini"  name="tagini">  
</select> 
<div class="clear" style="z-index: 670;"></div>
</div>
 <div class="st-form-line" style="z-index: 680;">
<span class="st-labeltext">Tag Final:</span>


<select id="tagfim"  name="tagfim">  
</select> 
<div class="clear" style="z-index: 670;"></div>
</div> 
 <div class="st-form-line" style="z-index: 680;">
<span class="st-labeltext">Ordem:</span>


<select id="ordem"  name="ordem">
    <option value="Sim">Sim</option>
    <option value="Não">Não</option>
</select> 
<div class="clear" style="z-index: 670;"></div>
</div> 
<div class="button-box" style="z-index: 460;">
<input id="button" class="st-button" type="submit" value="Submit" name="button">
<input id="button2" class="st-clear" type="reset" value="Cancel" name="button">
</div>

</div>
    </div>

因此用户选择当用户按下提交数据时它会对/ rondas / saveronda进行ajax调用,在此操作中我将数据保存在数据库中,一旦成功,我想重定向到/ rondas / chooserondas并显示一个成功的消息,我怎么能实现这一点.. 所以这里是/ rondas / saveronda的代码

   public function saverondaAction()
    {
        // action body

           $this->_helper->layout()->disableLayout();
            $this->_helper->viewRenderer->setNoRender(true);
             if($this->_request->isXmlHttpRequest())
        {


        $param1 = $this->_request->getParam('param1');
$param2 = $this->_request->getParam('param2');
//save data on the db and return to the /rondas/choosetags and display sucess    message.          
             }

    }

这是/ rondas / choosetags

的代码
 public function choosetagsAction()
    {



        // action body
         if($this->_request->isXmlHttpRequest())
        {
           $request=  $this->getRequest();
           $poll_id = $request->getParam('id');
           $this->_helper->layout()->disableLayout();
           $this->_helper->viewRenderer->setNoRender(true);

            Zend_Paginator::setDefaultScrollingStyle('Sliding');
            $page = $this->_getParam('page', 1);
            $limit = $this->_getParam('rows', 0);
            $sidx = $this->_getParam('sidx', 1);
            $sord = $this->_getParam('sord', 0);
            $totalrows = $this->_getParam('totalrows', false);
            if($totalrows)
                $limit = $totalrows;
            $tableModel = new Application_Model_Tags();
            $select = $tableModel->select();
            $select->setIntegrityCheck(false);
            $select->from('tags', array('id_tag','tag_nome', 'id_software', 'id_hardware','id_localizacao','descricao','modo','estado'))->joinInner('clientes', 'tags.id_cliente = clientes.id_cliente', array('nome'))->where('id_localizacao='.$poll_id);
            $filters = !empty($_REQUEST['filters']) ? (array) json_decode($_REQUEST['filters']) : array();

            $adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
            $paginator = new Zend_Paginator($adapter);
            $paginator->setCurrentPageNumber($page)->setItemCountPerPage($limit);
            $count = $paginator->getTotalItemCount();
            $total_pages = $count > 0 ? ceil($count / $limit) : 1;
             $responce = new stdClass();
            $responce->page = $page > $total_pages ? $total_pages : $page;

            $responce->total = $total_pages;
            $responce->records = $count;
            $i = 0;
            foreach($paginator as $item)
            {
                $responce->rows[$i]['id'] = $item['id_tag'];

            $responce->rows[$i]['cell'] = array($item['id_tag'],$item['tag_nome'],$item['nome'],$item['descricao'],$item['modo'],$item['estado']);
                $i++;
            }
            echo json_encode($responce);

        } else
        {
            $this->_helper->layout()->pageTitle = 'Rondas';
            $this->_helper->layout()->pageDescription = 'Criar as rondas';
        }

    }

我可以显示消息,如果我在同一个动作和视图上,我的意思是,在/ rondas / choosetags如果我把这个$ this-&gt; view-&gt; sucess = true,成功消息上传,但是我想在action / rondas / saveronda中将success var设置为true,然后重定向到action / rondas / choose标签并显示成功消息。

1 个答案:

答案 0 :(得分:0)

你不能直接。您可以做的是保存您想要在临时会话中设置的参数(例如,1跳= 1个成功请求到期):

$hopSession = new Zend_Session_Namespace('tempViewParams');
$hopSession->my_view_param1 = 'string with data';
$hopSession->setExpirationHops(1);

您可以在下一个操作中获取临时会话中的值。 另一种方法是在成功保存的重定向的URL中添加要设置为params的视图值(特别是在这种情况下,您想要的只是一个真正的参数)。所以一个网址如:

/rondas/choosetags/sucess/1

所以你可以在choosetagsAction()中添加一些内容,如:

if(1 == $request->getParam('success'))( {
    $this->view->success = true;
}

当然,如果您想要通过多个参数,SESSION解决方案将是最美丽的。