这是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]);
}