当前位置: 移动技术网 > IT编程>开发语言>PHP > Yii实现复选框批量操作实例代码

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

整理文档,搜刮出一个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, 
    )); 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网