Home >Backend Development >PHP Tutorial >Using yii2 gridview to implement batch deletion cases, yii2gridview_PHP tutorial

Using yii2 gridview to implement batch deletion cases, yii2gridview_PHP tutorial

WBOY
WBOYOriginal
2016-07-12 08:53:59870browse

Using yii2 gridview to implement batch deletion cases, yii2gridview

Author: Bailang Source: http://www.manks.top/article/yii2_gridview_deleteall The copyright of this article belongs to the author, welcome to reprint , but without the author’s consent, this statement must be retained and a link to the original text must be provided in an obvious position on the article page. Otherwise, we reserve the right to pursue legal liability.

Today we continue to discuss the issue of GridView. Yesterday, a friend left a message saying that you can use gridview to remove the header link for me? I thought and thought, this is really not easy to achieve using gridview, at least I didn't think of it. If you know how, please leave a message below. However, this gridview has a connection. It obviously needs to set up an ActiveDataProvider. How do you want me to use the gridview to implement it?

This problem is also very simple, let’s take a look at the specific implementation

<span>$dataProvider</span> = <span>new</span><span> ActiveDataProvider([ 
</span>'query' => <span>$query</span>,<span> 
]); 
</span><span>$dataProvider</span>->setSort(<span>false</span>);

It’s easy to get it done in minutes.

Okay, let’s talk about the topic: How to use yii2 gridview to implement batch deletion?

Let me talk about a few steps first, so as not to post the code directly. Some friends deleted one sentence and added another and finally failed!

1. Add an id when setting options in gridview. Here we name grid

'options' =><span> [
</span><span>//</span><span> ...其他设置项</span>
'id' => 'grid'<span>
]</span>,

2. Add option checkboxes to columns. Checkbox operations are indispensable for batch deletion. Here our name value is set to id to facilitate data operations

<span>[
    </span>'class' => 'yii\grid\CheckboxColumn',
    'name' => 'id',<span>
]</span>,

3. We add a batch delete button on the page. Note that we have added a class gridview here to facilitate the later js to achieve the click effect

<?= Html::a('批量删除', "javascript:void(0);", ['class' => 'btn btn-success gridview']) ?>

4. The last step is to write js to implement button operation. Open your console and take a look. We can easily get the ID of the selected row, and then we can operate the data asynchronously here.

<?<span>php
</span><span>$this</span>->registerJs('<span>
$(".gridview").on("click", function () {
//注意这里的$("#grid"),要跟我们第一步设定的options id一致
    var keys = $("#grid").yiiGridView("getSelectedRows");
    console.log(keys);
});
</span>'<span>);
</span>?>

Paste the complete code below

GridView::<span>widget([
        </span><span>//</span><span> ......</span>
        'options' => ['class' => 'grid-view','style'=>'overflow:auto', 'id' => 'grid'],
        <span>//</span><span> ......</span>
        'columns' =><span> [
            </span><span>//</span><span> ......</span>
<span>            [
                </span>'class' => 'yii\grid\CheckboxColumn',
                'name' => 'id',<span>
            ]</span>,
            <span>//</span><span> ......</span>
        ],<span>
]);
</span><span>$this</span>->registerJs('<span>
$(".gridview").on("click", function () {
    var keys = $("#grid").yiiGridView("getSelectedRows");
    console.log(keys);
});
</span>');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1122762.htmlTechArticleUsing yii2 gridview to implement batch deletion cases, yii2gridview Author: Bailang Source: http://www.manks.top /article/yii2_gridview_deleteall The copyright of this article belongs to the author, and you are welcome to reprint it, but without authorization...
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