最新消息: 新版网站上线了!!!

Yii CGridView 复选框、选择所有、批量删除、批量处理

第一步:  设置视图:admin.php

 

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'mz-pro-try-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        array(
            'htmlOptions'=>array('width'=>"30px"),
            'class' => 'CCheckBoxColumn',
            'name'=>'id',
            'value'=>'$data->id',
            'id'=>'
ids',
            'headerTemplate'=>'{item}',
            'selectableRows'=>2,
        ),

        'id',
        'name', 
         array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

 

第二步:  设置视图:admin.php   JS,通过ajax完成操作
  

<!-- 对复选框的操作  JS -->
<div class="row buttons">
    <script type="text/javascript">
        var data = new Object();  //对象
        data.YII_CSRF_TOKEN='<?php echo Yii::app()->getRequest()->getCsrfToken() ?>';
        function submitAjax(state){
            var state = state;
            data.state = state;   //为对象添加state属性,属性值为state  等同于:data['state'] = state
            data.checkedValue=$('#mz-pro-try-apply-grid').yiiGridView('getChecked', 'ids');
//            data.state= state;
            if (data.checkedValue.length==0){
                alert("至少选择一项");
                return;
            }
            if(state == 6){
                url = '/XXXXXXX/delall';
            }else {
                url = '/XXXXXXX/state';
            }
//            $.each(data,function(key,val){
//                alert('data数组中,索引:'+key+'对应的值为:'+val);
//            });
            $.ajax({
                url: url,
                type:'get',//必须使用,不知道为什么
                dataType:'json',
                data:data,
                success:function(data){
//                    alert(data);
                    jQuery('#mz-pro-try-apply-grid').yiiGridView('update');
                }
            })
        }

    </script>
    <?php echo CHtml::button("已审核",array('onClick'=>'submitAjax(1);')); ?>
    <?php echo CHtml::button("撤销审核",array('onClick'=>'submitAjax(2);')); ?>
    <?php echo CHtml::button("处理3",array('onClick'=>'submitAjax(3);')); ?>
    <?php echo CHtml::button("处理4",array('onClick'=>'submitAjax(4);')); ?>
    <?php echo ' &nbsp&nbsp&nbsp┃&nbsp&nbsp&nbsp ';?>
    <?php echo CHtml::button("处理5",array('onClick'=>'submitAjax(5);')); ?>
    <?php echo ' &nbsp&nbsp&nbsp┃&nbsp&nbsp&nbsp ';?>
    <?php echo CHtml::button("删除",array('onClick'=>'submitAjax(6);')); ?>
</div>

 

第三部:   在控制器写处理函数

    //复选框:批量删除
    public function actionDelall(){
        foreach($_GET['checkedValue'] as $value){
            $this->loadModel($value)->delete();
//            echo $value;
        }
    }


    //复选框:批量修改状态
    public function actionState(){
        $connection = Yii::app()->db;
        //$state 等于1(已审核),等于2(未审核),等于3(处理3),等于4(处理4),等于5(处理5),
        $state = $_GET['state'];
        foreach($_GET['checkedValue'] as $v){
            $sql = "UPDATE 数据表名 SET state = $state WHERE id = $v ";
            $command = $connection->createcommand($sql);
            $command->query();
        }
    }
 

结束!!!

转载请注明:谷谷点程序 » Yii CGridView 复选框、选择所有、批量删除、批量处理