我在stackoverflow上提到了很多关于Yii 2 : how to bulk delete data in kartik grid view?的问题。 但不知道为什么它不起作用。
这里是代码: index.php
<input type="button" class="btn btn-info" value="Multiple Delete" id="MyButton" >
....
echo GridView::widget([
'tableOptions' => ['id' => 'companies_grid_table',
'class' => 'table table-striped table-bordered table-hover'],
'id'=> 'companies_grid_display',
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => $gridColumns,
]);
.....
<script type="text/javascript">
<?php
$this->registerJs('
$(document).ready(function(){
$(\'#MyButton\').click(function(){
var CompId = $(\'#companies_grid_display\').yiiGridView(\'getSelectedRows\');
$.ajax({
type: \'POST\',
url : \'index.php?r=companies/multiple-delete\',
data : {row_id: CompId},
success : function() {
$(this).closest(\'tr\').remove(); //or whatever html you use for displaying rows
}
});
});
});', \yii\web\View::POS_READY);
?>
</script>
控制器
public function actionMultipleDelete()
{
//echo "actionMultidel"; exit;
$pk = Yii::$app->request->post('row_id');
foreach ($pk as $key => $value)
{
$sql = "DELETE FROM companies WHERE id = $value";
$query = Yii::$app->db->createCommand($sql)->execute();
}
return $this->redirect(['index']);
}
如何从gridview中删除多行?