Home >Backend Development >PHP Tutorial >Yii2 uses GridView to implement buttons for selecting all data and deleting data in batches

Yii2 uses GridView to implement buttons for selecting all data and deleting data in batches

不言
不言Original
2018-06-15 16:45:072500browse

This article mainly introduces the example of using GridView to implement all data selection and batch deletion buttons in yii2. It has certain reference value. Those who are interested can learn more.

After sorting through the documents, I found an example of using GridView to implement all-data selection and batch deletion buttons. I sorted it out a bit and simplified it for sharing.

Let’s first look at the effect of implementation

The key code is as follows:

  <?= GridView::widget([
   &#39;dataProvider&#39; => $dataProvider,
   &#39;showFooter&#39; => true, //设置显示最下面的footer
    &#39;id&#39; => &#39;grid&#39;,
    &#39;columns&#39; => [
     [
      &#39;class&#39;=>CheckboxColumn::className(),
      &#39;name&#39;=>&#39;id&#39;,
      &#39;headerOptions&#39; => [&#39;width&#39;=>&#39;30&#39;],
      &#39;footer&#39; => &#39;<button href="#" rel="external nofollow" class="btn btn-default btn-xs btn-delete" url="&#39;. Url::toRoute(&#39;admin/delete&#39;) .&#39;">删除</button>&#39;,
      &#39;footerOptions&#39; => [&#39;colspan&#39; => 5],
     ],
     [&#39;attribute&#39; => &#39;id&#39;, &#39;footerOptions&#39; => [&#39;class&#39;=>&#39;hide&#39;]], //其他列每个都要增加footerOptions项,设置class为hide,到达隐藏效果;
     [&#39;attribute&#39; => &#39;username&#39;, &#39;footerOptions&#39; => [&#39;class&#39;=>&#39;hide&#39;]],
     [
      &#39;attribute&#39; => &#39;status&#39;,
      &#39;value&#39; => function($model){
       if ($model->status == Admin::STATUS_ACTIVE){
        return &#39;启用&#39;;
       }
       return &#39;禁用&#39;;
      },
      &#39;footerOptions&#39; => [&#39;class&#39;=>&#39;hide&#39;]
     ],
     [&#39;class&#39; => &#39;yii\grid\ActionColumn&#39;, &#39;header&#39; => &#39;管理操作&#39;, &#39;footerOptions&#39; => [&#39;class&#39;=>&#39;hide&#39;]],
    ],
   &#39;layout&#39; => "{items}\n{pager}"
  ]); ?>

Getting the final selected data:

var ids = $("#grid").yiiGridView("getSelectedRows");

After getting the data, you can ajax submit it to the desired controller

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

yii2 implements paging and paging with search functions

About how to write search paging jQuery in the YII framework

The above is the detailed content of Yii2 uses GridView to implement buttons for selecting all data and deleting data in batches. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn