Yii实现复选框批量操作实例代码


Posted in PHP onMarch 15, 2017

整理文档,搜刮出一个Yii实现复选框批量操作实例代码,稍微整理精简一下做下分享。

在视图下

<?php $this->widget('zii.widgets.grid.CGridView', array(
 'id'=>'user-grid',
 'dataProvider'=>$model->search(),//Model里需要有个search()方法,$model是Controller中传递的Model对象
// /'filter'=>$model,
 'columns'=>array(
    array(
      'selectableRows' => 2,
      'class' => 'CCheckBoxColumn',
      'headerHtmlOptions' => array('width'=>'18px','align'=>'center'),
      'checkBoxHtmlOptions' => array('name' => 'ids[]','align'=>'center'),
    ),
'homework_numberID',
'type',
'target',
'state',

'author_id',
    array('name'=>'state','type'=>'raw','value'=>'($data->state =="not_read" ?"未读":"已审核")'),
    // array('name'=>'passRecord','type'=>'date'),
    array('name'=>'submit_time','type'=>'date'),
 array(
   'class'=>'CButtonColumn',
 ),
 ),
)); ?>
<select name="status" id="status">
  <option value="1">通过</option>
  <option value="0">不通过</option>
</select>
<button type="button" onclick="setStatus();" style="width:120px;">设置状态</button>
<script type="text/javascript">
  function getData(){
    var data=new Array();
    $("input:checkbox[name='ids[]']").each(function (){
      if($(this).attr("checked")){
        data.push($(this).val());
      }
    });

    return data;
  }
  function setStatus(){
    var data = getData();
    if(data.length < 1) {
      alert('请至少选择一个项目。');
      return ;
    }
 //这里是传递的url post方法
    $.post("index.php?r=user/status",{'ids[]':data,'status':$('#status').val()}, function (data) {
      if (data=='ok') {
        alert('设置状态成功!');
      }else{
        alert('设置状态失败,请重试!');
      }
      window.open('index.php?r=user/admin','mainFrame');;
    });
  }
</script>

在Model中的search()函数

public function search() 
  { 
    // @todo Please modify the following code to remove attributes that should not be searched. 

    $criteria=new CDbCriteria; 

    //$criteria->with = array('author'); //添加了和 author 的渴求式加载 
    // $this->state = "not_passed";
    $criteria->compare('state',$this->state); 


    //这里添加了一个 compare, username 是 User 表的字段,$this->name 是我们添加的属性,true 为模糊搜索 
    //$criteria->compare('username',$this->name,true); 

    return new CActiveDataProvider($this, array( 
     'criteria'=>$criteria, 
    ));

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
在PHP中使用XML
Oct 09 PHP
PHP开发微信支付的代码分享
May 25 PHP
PHP远程采集图片详细教程
Jul 01 PHP
PHP使用PDO连接ACCESS数据库
Mar 05 PHP
php使用GD库创建图片缩略图的方法
Jun 10 PHP
为你总结一些php信息函数
Oct 21 PHP
php获取本机真实IP地址实例代码
Mar 31 PHP
Yii2中关联查询简单用法示例
Aug 10 PHP
PHP入门教程之数学运算技巧总结
Sep 11 PHP
php 判断页面或图片是否经过gzip压缩的方法
Apr 05 PHP
PHP基于Redis消息队列实现发布微博的方法
May 03 PHP
php设计模式之备忘模式分析【星际争霸游戏案例】
Mar 24 PHP
thinkPHP实现签到功能的方法
Mar 15 #PHP
thinkPHP框架实现图像裁剪、缩放、加水印的方法
Mar 14 #PHP
ThinkPHP框架分布式数据库连接方法详解
Mar 14 #PHP
让ThinkPHP的模板引擎达到最佳效率的方法详解
Mar 14 #PHP
thinkphp3.2实现跨控制器调用其他模块的方法
Mar 14 #PHP
Yii2下点击验证码的切换实例代码
Mar 14 #PHP
php分页查询的简单实现代码
Mar 14 #PHP
You might like
在同一窗体中使用PHP来处理多个提交任务
2006/10/09 PHP
PHP 文件类型判断代码
2009/03/13 PHP
thinkphp中连接oracle时封装方法无法用的解决办法
2013/06/17 PHP
使用CodeIgniter的类库做图片上传
2014/06/12 PHP
php计算2个日期的差值函数分享
2015/02/02 PHP
php判断邮箱地址是否存在的方法
2016/02/13 PHP
yii2实现分页,带搜索的分页功能示例
2017/01/07 PHP
用javascript实现的激活输入框后隐藏初始内容
2007/06/29 Javascript
JQuery 选择和过滤方法代码总结
2010/11/19 Javascript
幻灯片带网页设计中的20个奇妙应用示例小结
2012/05/27 Javascript
js获取元素外链样式的方法
2015/01/27 Javascript
JS实现超精简的链接列表在固定区域内滚动效果代码
2015/11/04 Javascript
基于jquery实现可定制的web在线富文本编辑器附源码下载
2015/11/17 Javascript
详解Angularjs filter过滤器
2016/02/06 Javascript
Bootstrap页面缩小变形的快速解决办法
2017/02/03 Javascript
基于vue-video-player自定义播放器的方法
2018/03/21 Javascript
VUE实现可随意拖动的弹窗组件
2018/09/25 Javascript
详解使用mocha对webpack打包的项目进行&quot;冒烟测试&quot;的大致流程
2020/04/27 Javascript
整理Python最基本的操作字典的方法
2015/04/24 Python
django上传图片并生成缩略图方法示例
2017/12/11 Python
Python中对数组集进行按行打乱shuffle的方法
2018/11/08 Python
如何通过50行Python代码获取公众号全部文章
2019/07/12 Python
python yield关键词案例测试
2019/10/15 Python
tensorflow 变长序列存储实例
2020/01/20 Python
在 Python 中接管键盘中断信号的实现方法
2020/02/04 Python
python pptx复制指定页的ppt教程
2020/02/14 Python
详解css position 5种不同的值的用法
2019/07/30 HTML / CSS
JAVA程序员面试题
2012/10/03 面试题
爱心捐款倡议书范文
2014/05/12 职场文书
培训督导岗位职责
2015/04/10 职场文书
办公室禁烟通知
2015/04/23 职场文书
爱心捐书倡议书
2015/04/27 职场文书
跟班学习心得体会(共6篇)
2016/01/23 职场文书
《比尾巴》教学反思
2016/02/24 职场文书
经典哲理警句:志不真则心不热,心不热则功不贤
2019/11/14 职场文书
一文搞懂Golang 时间和日期相关函数
2021/12/06 Golang