YIi2中checkboxOptions前選框的用法


這是checkboxOptions前選框的用法

use yii\grid\GridView;

$this->registerJs("

    $('#selection_all').click(function(){

        $('input[name=selection_all]').click();

    });

 

    $('#delete_select').click(function(){

        var selection = $('#w0').yiiGridView('getSelectedRows');

        if(selection.length==0){

            alert('未選中');

            return false;

        }else{

            if(confirm('您確定要刪除嗎?')==true){

               $.post('".Url::to(['product-base/delete-select'])."', {selection:selection});

               location=location;

            }else{

               location=location;

            }        

        }

});

 

    $('#up_select').click(function(){

        var selection = $('#w0').yiiGridView('getSelectedRows');

        if(selection.length==0){

            alert('未選中');

            return false;

        }else{

            $.post('".Url::to(['product-base/status-select'])."', {selection:selection,status:1});

            location=location;

        }

    });

 

    $('#down_select').click(function(){

        var selection = $('#w0').yiiGridView('getSelectedRows');

        if(selection.length==0){

            alert('未選中');

            return false;

        }else{

            $.post('".Url::to(['product-base/status-select'])."', {selection:selection,status:0});

            location=location;

        }

    });

", \yii\web\View::POS_END);

 

<?= GridView::widget([

          'layout' => "{items}<div class='row'>

               <div class='btn-group col-md-3'>

                 <button class='btn btn-warning' id='selection_all'>全選</button>

                 <button class='btn btn-default' id='up_select'>上架</button>

                 <button class='btn btn-default' id='down_select'>下架</button>

                 <button class='btn btn-default' id='delete_select'>刪除</button>

               </div>

               <div class='col-md-7' style='margin-top:-20px'>

               {pager}</div>

                            </div>

               ",

   

        'dataProvider' => $dataProvider,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],

            ['class' => 'yii\grid\CheckboxColumn',

                'checkboxOptions' => function($model, $key, $index, $column) {

                    return ['value' => $model->id];

                }],

            ['class' => 'yii\grid\ActionColumn'],

            [

                'label' => '更多操作',

                'format'=>'raw',

                'value' => function($model){

                    return Html::a('店鋪商品列表', ['product/index','id' => $model->id], ['title' => '店鋪商品列表']);

                }

            ],

        ],

]); ?>

 

 

Controller中的寫法:

   public function actionDeleteSelect(){

        //刪除selection

        $selection = Yii::$app->request->post('selection');

        ProductBase::deleteAll(['id'=>$selection]);

    }

 

    //改變狀態

    public function actionStatusSelect(){

        //找到選中的

        $selection = Yii::$app->request->post('selection');

        //找到post過來的

        $status = Yii::$app->request->post('status');

        ProductBase::updateAll(['status'=>$status],['id'=>$selection]);

    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM