Skip to main content
Version: Fegg 5.x

Bulk delete

Delete the checked rows of a CMS list at once

A list of a post type can have a checkbox in front of each row and a 「選択した項目を削除」 button above the table. The box in the header checks every row on the page. The button works once a row is checked, and asks first: 「2件を削除してもよろしいですか?」. The list then shows 「2件削除しました」.

Turning it on​

Add the trait to the CMS controller of the post type:

site/src/Controller/Cms/Event.php
class Event extends Controller
{
use \Fegg\Cms\BulkActions; // POST /cms/event/bulkDelete

protected $_cmsSection = 'event';
// ...
}

Then put the form above the table of the list template, and a first column of checkboxes in the table:

site/template/cms/tpl/event/lists.tpl
{{ include '/cms/parts/bulk_delete' }}

<table class="c-table-list u-mb-2">
<thead class="c-table-list__thead">
<tr class="c-table-list__thead__row">
<th class="c-table-list__thead__head is-check"><input type="checkbox" data-cms-bulk-all aria-label="すべて選択"></th>
<!-- ... -->
</tr>
</thead>
<tbody class="c-table-list__tbody">
{{ foreach $page.data as $key => $post }}
<tr class="c-table-list__tbody__row">
<td class="c-table-list__tbody__data is-check"><input type="checkbox" name="ids[]" value="{{ $post[$page.this_id] }}" form="cms-bulk-delete" data-cms-bulk-item aria-label="選択"></td>
<!-- ... -->
</tr>
{{ end foreach }}
</tbody>
</table>

The checkboxes are outside the form and belong to it with form="cms-bulk-delete", so the rows' own 削除 forms stay as they are. is-check makes the column as narrow as the box. The scripts are in the CMS's base.js, which every CMS screen loads: the template has no script of its own.

The skeleton's news (Cms\News) and Lab (Cms\Lab) lists have it on.

What is deleted​

  • The rows are deleted like the list's 削除 button: $this->_model->delete($id) sets valid = 0. All the checked rows are deleted in one transaction.
  • Only a POST with the CSRF token is accepted, like the other CMS actions. IDs that are not whole numbers are ignored; with no row left, the list shows 「項目が選択されていません」.
  • When the post type's delete() does more than the model's delete (removes files or related rows), override _bulkDeleteRow() in the controller to do the same. It returns the number of rows deleted:
site/src/Controller/Cms/Event.php
protected function _bulkDeleteRow($id)
{
$this->_model_gallery->deleteWhere('event_id = ?', $id);

return $this->_model->delete($id);
}

Messages with a count​

The list shows the message of the action it is redirected to (index/DELETED). A key followed by a number, such as DELETED-3, takes the KEY-n message of Fegg\Cms\Helper with the number filled in: 「3件削除しました」.