Analysis on batch deletion of Yii2gridview

Source: Internet
Author: User
Some children's shoes in the circle of friends talked to me about the GridView problem. Some friends said that you used the gridview to remove the header link for me? I thought for a long time that using the gridview is really not easy to implement. If you have different opinions, please leave a message. However, this gridview has a wool Association. it is clearly set up Yii2

Some children's shoes in the circle of friends talked to me about the GridView problem. Some friends said that you used the gridview to remove the header link for me? I thought for a long time that using the gridview is really not easy to implement. If you have different opinions, please leave a message. 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);});');

Recommended reading:

Common operations of GridView in Yii2

Tips for loading css and js at the bottom of the yii2 page

Analysis on how to format and implement date searchable in Yii2 GridView

Analysis of Yii2 GridView implementation drop-down search tutorial

The above content is a batch deletion tutorial on Yii2 gridview, which is introduced by Xiaobian. I hope it will be helpful to you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.