Yii中Ajax的使用,如收藏功能

view中

<?php

$cs=Yii::app()->clientScript;

$cs->registerScriptFile('http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js');

?>







<!-- ▼显示提示信息▼ -->

<?php if(Yii::app()->user->hasFlash('success')){ ?>

<div class="flash-success">

    <?php echo Yii::app()->user->getFlash('success'); ?>

</div>

<?php } ?>



<?php if(Yii::app()->user->hasFlash('failed')){ ?>

<div class="flash-error">

    <?php echo Yii::app()->user->getFlash('failed'); ?>

</div>

<?php } ?>

<!-- ▲显示提示信息▲ -->









<div class="row">

<?php echo $form->textField($model,'cnt',array('size'=>'6', 'maxlength'=>'6'));?>

<?php echo CHtml::ajaxButton("随机生成邮件地址", 

        //CHtml::normalizeUrl(array('mail/makeMailAddress','count'=>'1')),   // GET方式进行请求

        CHtml::normalizeUrl(array('mail/makeMailAddress')),   // GET方式进行请求

        array(

            'data' => array('cnt'=>'js: $("#MailForm_cnt").val()'),  // 以GET方式将页面的值传递给后台

            'beforeSend'=>'function(){}',

            'update'=>'#MailForm_to',      // 更新对象 

            'success'=>'function(data){    

               $("#MailForm_to").val(data);

            }',                            // 将后台的返回值用JS显示在页面上

        )); ?>

</div>

Controller 中

    public function actionMakeMailAddress(){

        if(Yii::app()->request->isAjaxRequest){

            if (isset($_GET["cnt"])){

                $cnt = is_numeric($_GET["cnt"]) ? $_GET["cnt"] : "0";

            } else {

                $cnt = 1000 ;

            }

            

            $data = "";

            for($i = 0 ; $i < $cnt ; $i++) {

                $data .= rand(10000000, 99999999)."@qq.com \n" ;

            }

            echo $data;

        } else {

            Yii::app()->user->setFlash("failed", "Request Error \n");

            $this->refresh();

        }

    }

From: http://shoukii0721.iteye.com/blog/1576381

 

其它参考链接:

http://www.yiichina.com/api/CHtml#ajaxButton-detail

http://www.yiichina.com/api/CHtml#ajax-detail

http://api.jquery.com/jQuery.ajax/#options

 

http://shoukii0721.iteye.com/blog/1576381

http://blog.csdn.net/dreamzml/article/details/8642229

 

以及zii.widgets.grid.CGridView删除按钮JS:

jQuery(document).on('click','#user-grid a.delete',function() {

    if(!confirm('确定要删除这条数据吗?')) return false;

    var th = this,

        afterDelete = function(){};

    jQuery('#user-grid').yiiGridView('update', {

        type: 'POST',

        url: jQuery(this).attr('href'),

        success: function(data) {

            jQuery('#user-grid').yiiGridView('update');

            afterDelete(th, true, data);

        },

        error: function(XHR) {

            return afterDelete(th, false, XHR);

        }

    });

    return false;

});

 其HTML:

<tr class="odd"> 

<td>1</td><td>demo</td><td></td><td></td><td>0</td><td class="button-column"><a title="查看" href="/index.php/user/1"><img src="/assets/2b2efa90/gridview/view.png" alt="查看" /></a> <a title="修改" href="/index.php/user/update/1"><img src="/assets/2b2efa90/gridview/update.png" alt="更新" /></a> <a class="delete" title="删除" href="/index.php/user/delete/1"><img src="/assets/2b2efa90/gridview/delete.png" alt="删除" /></a></td><td class="button-column"><a style="cursor:pointer;" class="add_status" title="展示" href="/index.php/user/status/1?status=1">展示</a> </td></tr> 

 

收藏功能、顶、踩 等可参考开源中国上的相关JS:

http://my.oschina.net/u/1403186/blog/179641

http://www.oschina.net/code/snippet_197014_17646

 

你可能感兴趣的:(Ajax)