Yii框架zii.widgets.grid自定义按钮,ajax触发事件并提示

11 篇文章 0 订阅

Yii内置了3种按钮:查看,修改和删除,你可以自定义样式、事件。详细配置见类参考:CButtonColumn.


如果需要自定义按钮绑定指定的事件该怎么办呢?

幸运的是Yii提供了自定义按钮的办法.看代码:

在视图文件里面:

$this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'xx-xx-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'pager'=>array(
            'class'=>'CLinkPager',
            'nextPageLabel'=>'下一页',
            'prevPageLabel'=>'上一页',
            'header'=>'',
    ),
    'summaryText'=>'<font color=#0066A4>显示{start}-{end}条.共{count}条记录,当前第{page}页</font>',
    'columns'=>array(
        array(
                'name'=>'id',
                'htmlOptions'=>array('width'=>'25'),
                'sortable'=>false,
        ),
        array(
            'class'=>'CButtonColumn',
            'template'=>'{view} {update}',
            'viewButtonOptions'=>array('title'=>'查看'),
            'updateButtonOptions'=>array('title'=>'修改'),
        ),
        array(
            'class'=>'CButtonColumn',
            'header'=>'首页展示',
            'template'=>'{add} {del}',
            'buttons'=>array(
                    'add' => array(
                            'label'=>'展示',     // text label of the button
                            'url'=>'Yii::app()->controller->createUrl("focus/create",array("id"=>$data->primaryKey,"type"=>1))',       // a PHP expression for generating the URL of the button
                            'imageUrl'=>'http://s.maylou.com/common/images/ysh.jpg',  // image URL of the button. If not set or false, a text link is used
                            'options'=>array('style'=>'cursor:pointer;'), // HTML options for the button tag
                            'click'=>$click,     // a JS function to be invoked when the button is clicked
                            'visible'=>'SiteRecommend::isItemInTypeAndId(1, $data->id)?false:true',
                    ),
                    'del' => array(
                            'label'=>'取消展示',     // text label of the button
                            'url'=>'Yii::app()->controller->createUrl("focus/delete",array("id"=>$data->primaryKey,"type"=>1))',       // a PHP expression for generating the URL of the button
                            'imageUrl'=>'http://s.maylou.com/common/images/yzhu.jpg',  // image URL of the button. If not set or false, a text link is used
                            'options'=>array('style'=>'cursor:pointer;'), // HTML options for the button tag
                            'click'=>$click,     // a JS function to be invoked when the button is clicked
                            'visible'=>'SiteRecommend::isItemInTypeAndId(1, $data->id)?true:false',
                    )
            ),
        ),
    ),
));

buttons选项提供了创建按钮的方法,上面创建了2个按钮:add和del,并注册到template里面。其中最主要的是click选项,决定了你的触发条件。这里用ajax触发。在上面的代码前面加上$click内容:

$csrfTokenName = Yii::app()->request->csrfTokenName;
    $csrfToken = Yii::app()->request->csrfToken;
    $csrf = "\n\t\tdata:{ '$csrfTokenName':'$csrfToken' },";
    $Confirmation= "你确定要这么做?";
    $afterDelete = 'function(link,success,data){ if(success) alert(data); }';
    $click=<<<EOD
    function() {
    if(!confirm("$Confirmation")) return false;;
    var th=this;
    var afterDelete=$afterDelete;
    $.fn.yiiGridView.update('build-oneprice-grid', {
    type:'POST',
    url:$(this).attr('href'),$csrf
    success:function(data) {
    $.fn.yiiGridView.update('build-oneprice-grid');
        afterDelete(th,true,data);
    },
    error:function(XHR) {
        return afterDelete(th,false,XHR);
    }
    });
    return false;
    }
EOD;

srf不用管他,是安全验证,必须要有,否则会400报错.$click是js函数的字符窜,用了文档字符窜形式,注意结束的EOD前面必须没空格,也不能缩进。

 

这是Yii内置的yiiGridView Jquery插件,把请求提交到控制器的动作里面处理,然后返回结果并显示。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`console.php`是Yii框架中的一个命令行脚本,用于执行各种命令和任务,如数据库迁移、生成代码等。在Yii2中,`console.php`被用于运行控制台应用程序,这些应用程序通常用于处理后台任务、定时任务、计划任务等。 在Yii2中,您可以使用`yii`命令来运行`console.php`脚本,如下所示: ``` php /path/to/your/yii-application/yii <command> ``` 其中,`/path/to/your/yii-application/`是您的Yii应用程序的路径,`<command>`是要执行的命令。例如,要运行数据库迁移命令,可以使用以下命令: ``` php /path/to/your/yii-application/yii migrate ``` 除了运行命令,您还可以使用Yii2的Console应用程序来设置定时任务。定时任务是指在指定的时间间隔内自动执行的任务。要创建定时任务,您需要执行以下步骤: 1. 创建一个继承自`yii\console\Controller`类的控制器,该控制器将处理定时任务的逻辑。 2. 在`console/config/main.php`配置文件中配置一个名为`cron`的组件,该组件将设置定时任务的时间表。 3. 在服务器上设置一个计划任务(cron job),以在指定的时间间隔内运行Yii2的console应用程序。 下面是一个简单的示例,演示如何使用Yii2的Console应用程序设置定时任务: 1. 创建一个名为`TestController`的控制器,用于处理定时任务的逻辑: ```php <?php namespace app\commands; use yii\console\Controller; class TestController extends Controller { public function actionIndex() { echo "This is a test command\n"; } } ``` 2. 在`console/config/main.php`配置文件中配置一个名为`cron`的组件,该组件将设置定时任务的时间表: ```php <?php return [ // ... 'components' => [ // ... 'cron' => [ 'class' => 'yii\console\CronController', 'schedule' => [ '* * * * *' => ['test/index'], // 每分钟执行一次TestController的index动作 ], ], ], ]; ``` 在上面的配置中,我们创建了一个名为`cron`的组件,它是`yii\console\CronController`的一个实例。`'schedule'`属性包含一个时间表数组,该数组指定了要运行的控制器操作和它们的执行时间。 在上面的示例中,我们设置了一个时间表,每分钟执行一次`TestController`控制器的`index`操作。 3. 在服务器上设置一个计划任务(cron job),以在指定的时间间隔内运行Yii2的console应用程序。 在Linux环境中,您可以使用`crontab`命令来设置计划任务。要设置一个计划任务,您需要打开终端并输入以下命令: ``` crontab -e ``` 这将打开一个文本编辑器,在其中输入以下内容: ``` * * * * * /usr/bin/php /path/to/your/yii-application/yii cron/run >> /dev/null 2>&1 ``` 其中,`/usr/bin/php`是PHP解释器的路径,`/path/to/your/yii-application/`是您的Yii应用程序的路径。`cron/run`是您要运行的控制器操作。`>> /dev/null 2>&1`将输出重定向到`/dev/null`,以避免将输出写入日志文件。 这将在每分钟运行一次Yii2的Console应用程序,并执行指定的控制器操作。 希望这个示例可以帮助您理解如何使用Yii2的Console应用程序设置定时任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值