Yii 2:如何在Kartik网格视图中批量删除数据? [英] Yii 2 : how to bulk delete data in kartik grid view?
问题描述
Kartik网格视图提供了在网格视图中显示复选框的选项.如何通过选中复选框删除批量数据?任何帮助将是巨大的.这是我的代码:
Kartik grid view in yii2 provides an option to display checkboxes in grid view. How do I delete bulk data by checking the checkboxes? Any help would be greatful. Here is my code:
<?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'],
],
]); ?>
推荐答案
尝试一下,希望对您有所帮助,而不是使用yii\grid\CheckboxColumn
Try this , i hope this will help you, instead of this kartik\grid\CheckboxColumn
use this yii\grid\CheckboxColumn
第1步: 在index.php中创建一个用于多次删除的按钮
Step 1: create a button for multiple delete in index.php
<input type="button" class="btn btn-info" value="Multiple Delete" id="MyButton" >
第2步: 在您的index.php(同一页面)中,使用此javascript
Step 2: In your index.php(same page) use this 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);
?>
此jquery用于获取所选行的值(id)
this jquery is used to get the value(id) of selected row
第3步: 在酒店的控制器中(HotelController.php) 创建多次删除操作
Step 3: in controller of hotel (HotelController.php) create a action for multiple-delete
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']);
}
一定要尝试一下,这将帮助您...
Try this surely this will help you...
这篇关于Yii 2:如何在Kartik网格视图中批量删除数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!