Alright, so the previous lecture was all about displaying table data on the frontend. You may think, is it possible to delete which is saved and displayed on the frontend?
Yes. It is possible and simple too.
By adding delete column, one can delete the data easily. Or you can also add action column to admin UI grid with delete and edit options.
Programmatic Solution to Add Delete Action Column in Magento 2:
1. Change code in Showdata.php file at appcodeMeetanshiExtensionBlock
<?php namespace Meetanshi\Extension\Block; use Magento\Framework\View\Element\Template; use Magento\Backend\Block\Template\Context; use Meetanshi\Extension\Model\ResourceModel\Extension\CollectionFactory; class Showdata extends Template { public $collection; public function __construct(Context $context, CollectionFactory $collectionFactory, array $data = []) { $this->collection = $collectionFactory; parent::__construct($context, $data); } public function getCollection() { return $this->collection->create(); } public function getDeleteAction() { return $this->getUrl('extension/index/delete', ['_secure' => true]); } }
2. Update showdata.phtml at appcodeMeetanshiExtensionviewfrontendtemplates
<?php $collection = $block->getCollection(); if ($collection->count()) { ?> <div class="table-wrapper orders-history"> <table class="data table table-order-items history" id="my-orders-table"> <caption class="table-caption"><?php echo __('Grid Record') ?></caption> <thead> <tr> <th scope="col" class="col id"><?php echo __('ID') ?></th> <th scope="col" class="col name"><?php echo __('Name') ?></th> <th scope="col" class="col email"><?php echo __('Email') ?></th> <th scope="col" class="col telephone"><?php echo __('Telephone') ?></th> <th scope="col" class="col createat"><?php echo __('Created At') ?></th> <th scope="col" class="col action"><?php echo __('Action') ?></th> </tr> </thead> <tbody> <?php foreach ($collection as $item): ?> <tr> <td data-th="<?= $block->escapeHtml(__('ID')) ?>" class="col id"> <?php echo $item->getId() ?> </td> <td data-th="<?= $block->escapeHtml(__('Name')) ?>" class="col name"> <?php echo $item->getName() ?> </td> <td data-th="<?= $block->escapeHtml(__('Email')) ?>" class="col email"> <?php echo $item->getEmail() ?> </td> <td data-th="<?= $block->escapeHtml(__('Telephone')) ?>" class="col telephone"> <?php echo $item->getTelephone() ?> </td> <td data-th="<?= $block->escapeHtml(__('Created At')) ?>" class="col title"><?php echo date('Y-m-d', strtotime($item->getCreatedAt())); ?> </td> <td data-th="<?= $block->escapeHtml(__('Action')) ?>" class="col delete"> <a href="<?php echo $block->getDeleteAction() . 'id/' . $item->getId(); ?>"><?php echo __('Delete') ?></a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <?php } ?>
After making changes in Showdata.php and showdata.phtml files, you will manage to add ‘delete column’ in Magento 2.

That’s it.
Share with post with newbie Magento developers.
Thank you.