Batch Delete case using yii2 gridview, yii2gridview
Author: White Wolf Source: Ghost.
I will continue to discuss the problem of the GridView today. Yesterday, a friend left a message saying that you used the gridview to remove the header link for me? I think, it is really not easy to use the gridview. At least I didn't think of it. I can leave a message only when I click it. However, this gridview has a wool Association. It is clearly set to ActiveDataProvider. How do you use the gridview to implement it.
This problem is also very simple. Let's take a look at the specific implementation.
$dataProvider = new ActiveDataProvider([ 'query' => $query, ]); $dataProvider->setSort(false);
It's not a matter of minutes.
Okay. Let's talk about the topic: How to Use yii2 gridview to implement batch deletion?
Let me take a few steps first, so that some friends who directly paste the code may have deleted a sentence and finally failed!
1. Add an id when setting options in the gridview. Here we name the grid.
'Options' => [//... other settings 'id' => 'grid'],
2. The columns option check box is added. Batch deletion is indispensable. Here, the name value is set to id to facilitate data operations.
[ 'class' => 'yii\grid\CheckboxColumn', 'name' => 'id',],
3. We add a batch delete button on the page. Note that a class gridview is added here to facilitate the click effect of the subsequent JavaScript code.
<? = Html: a ('batch delete', "javascript: void (0);", ['class' => 'btn btn-success gridview '])?>
4. In the last step, write the js implementation button operation and open your console. We can easily get the id of the selected row, and then asynchronously operate the data here.
<? Php $ this-> registerJs ('$ (". gridview "). on ("click", function () {// pay attention to $ ("# grid "), to be consistent with the options id set in step 1, var keys = $ ("# grid "). yiiGridView ("getSelectedRows"); console. log (keys) ;}); ') ;?>
The complete code is attached below
GridView::widget([ // ...... 'options' => ['class' => 'grid-view','style'=>'overflow:auto', 'id' => 'grid'], // ...... 'columns' => [ // ...... [ 'class' => 'yii\grid\CheckboxColumn', 'name' => 'id', ], // ...... ],]);$this->registerJs('$(".gridview").on("click", function () { var keys = $("#grid").yiiGridView("getSelectedRows"); console.log(keys);});');