利用Yii怎么对复选框进行批量操作

发布时间:2021-02-13 13:25:00 作者:Leah
来源:亿速云 阅读:157

利用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();" >设置状态</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, 
    ));

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 利用vuex怎么对state进行监听
  2. 怎么对php复选框进行判断

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

yii

上一篇:如何在PHP项目中解决嵌套问题

下一篇:如何在Linux中部署一个.net core Api项目

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》