yii2中的Kartik网格视图提供了在网格视图中显示复选框的选项.如何通过选中复选框来删除批量数据?任何帮助都会很棒.这是我的代码:
<?php use kartik\grid\GridView;?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'pjax'=>true,
'pjaxSettings'=>[
'neverTimeout'=>true,
],
'columns' => [
['class' => '\kartik\grid\CheckboxColumn'],
['class' => 'yii\grid\SerialColumn'],
'hotel_id',
'name',
'address',
'phone_no',
'contact_person',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
Run Code Online (Sandbox Code Playgroud)
1.将自定义类或ID添加到pjax容器
使用GridView将类或id添加到pjax容器中,因此您不依赖于自动生成的类和ID(或者如果您在一个页面中有多个GridView小部件).
kartik\grid\CheckboxColumn只是yii\grid\CheckboxColumn的扩展版本.
kartik\grid\View有containerOptions,你可以class在这里指定,它似乎id是自动生成的,不能使用此属性更改.
'containerOptions' => ['class' => 'hotel-pjax-container'],
Run Code Online (Sandbox Code Playgroud)
生成输出的示例:
<div class="hotel-pjax-container table-responsive" id="w0-container">...</div>
Run Code Online (Sandbox Code Playgroud)
yii\grid\View\有options,你可以id在这里指定.结果容器ID将以传递的值为前缀,例如:
'options' => ['id' => 'hotel-pjax'],
Run Code Online (Sandbox Code Playgroud)
生成的输出:
<div class="table-responsive" id="hotel-pjax-container">...</div>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,类被忽略.
我建议指定id.
2.在控制器中创建或修改删除操作
默认情况下,delete自动生成的动作gii具有重定向功能,因此我们可以为多个删除创建另一个动作(或者您可以在一个中处理它,这取决于您).
public function actionDeleteMultiple()
{
$pk = Yii::$app->request->post('pk'); // Array or selected records primary keys
// Preventing extra unnecessary query
if (!$pk) {
return;
}
return Hotel::deleteAll(['hotel_id' => $pk]);
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果您未指定任何条件deleteAll(),则表中的所有记录都将被删除!准确的.
您还可以VerbFilter像这样指定操作:
use yii\filters\VerbFilter;
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
'delete-multiple' => ['post'],
],
],
];
}
Run Code Online (Sandbox Code Playgroud)
3.写一些javascript来统一起来
您可以获取所选行的主键,如下所示:
$('#hotel-pjax-container').yiiGridView('getSelectedRows');
Run Code Online (Sandbox Code Playgroud)
添加此javascript(例如按钮点击):
$.post(
"delete-multiple",
{
pk : $('#hotel-pjax-container').yiiGridView('getSelectedRows')
},
function () {
$.pjax.reload({container:'#hotel-pjax-container'});
}
);
Run Code Online (Sandbox Code Playgroud)
您可以在本期中找到有关使用pjax更新GridView的更多信息.也许试试这个:$('#hotel-pjax-container').yiiGridView('applyFilter');作为另类; 使用资产或仅包含js registerJs();
试试这个,我希望这会对你有所帮助,而不是kartik\grid\CheckboxColumn用这个yii\grid\CheckboxColumn
第1步:在index.php中创建一个用于多次删除的按钮
<input type="button" class="btn btn-info" value="Multiple Delete" id="MyButton" >
Run Code Online (Sandbox Code Playgroud)
第2步:在你的index.php(同一页面)中使用这个javascript
<?php
$this->registerJs('
$(document).ready(function(){
$(\'#MyButton\').click(function(){
var HotId = $(\'#w4\').yiiGridView(\'getSelectedRows\');
$.ajax({
type: \'POST\',
url : \'index.php?r=hotel/multiple-delete\',
data : {row_id: HotId},
success : function() {
$(this).closest(\'tr\').remove(); //or whatever html you use for displaying rows
}
});
});
});', \yii\web\View::POS_READY);
?>
Run Code Online (Sandbox Code Playgroud)
此jquery用于获取所选行的值(id)
第3步:在酒店控制器(HotelController.php)中创建一个多次删除的动作
public function actionMultipleDelete()
{
$pk = Yii::$app->request->post('row_id');
foreach ($pk as $key => $value)
{
$sql = "DELETE FROM hotel WHERE hotel_id = $value";
$query = Yii::$app->db->createCommand($sql)->execute();
}
return $this->redirect(['index']);
}
Run Code Online (Sandbox Code Playgroud)
试试这肯定会帮助你......