2

Fetched details from my custom module and displayed it in a table. There is a delete option give to corresponding row entries. The above table codes written in a phtml page.

How to delete the db entry when i click on the delete option?

<tr class="data-row" >
        <td class="admin__control-fields _no-header" >
           <fieldset class="admin__field">
              <legend class="admin__field-label">
                 <span>Image</span>
              </legend>
              <div class="admin__field-control admin__control-fields _no-header" >
                 <div class="admin__field" >
                    <div class="admin__field-control" >
                       <img class="admin__control-thumbnail" src="">
                    </div>
                 </div>
              </div>
           </fieldset>
        </td>
        <td  class="admin__control-fields _no-header">
           <fieldset class="admin__field" data-form-part="product_form" name="col[]">
              <legend class="admin__field-label">
                 <span >Color</span>
              </legend>
              <div class="admin__field-control admin__control-fields _no-header" >
                 <div class="admin__field" >
                    <div class="admin__field-control" >
                       <div class="control-table-text">
                          <span data-form-part="product_form" name="col[]"><?php echo $color_name ?></span>
                       </div>
                    </div>
                 </div>
              </div>
           </fieldset>
        </td>
        <td  class="admin__control-fields _no-header" >
           <fieldset class="admin__field" >
              <legend class="admin__field-label">
                 <span >Series</span>
              </legend>
              <div class="admin__field-control admin__control-fields _no-header" >
                 <div class="admin__field">
                    <div class="admin__field-control" >
                       <div class="control-table-text">
                          <span ><?php echo $series_name ?></span>
                       </div>
                    </div>
                 </div>
              </div>
           </fieldset>
        </td>


        <td  class="data-grid-actions-cell _fit _no-header">
            <div class="action-select-wrap" >
                <button class="action-secondary uploadtable">
                    <span >Remove</span>
                </button>

            </div>
        </td>
     </tr>

This is my html code. The details shown in the table was fetched from custom table. Here, when i click remove button , i need to remove the entry from db.

2
  • You might want to show some code you have so we don't work blindly. – Marius May 24 '17 at 6:40
  • @Marius Pasted the html code – Rita Jose May 24 '17 at 6:48
2

By jquery we can delete this.

var url =  '<?php echo $baseurl ?>admin/helloworld/index/';
 function deleterow(rowid)
    {
        jQuery.ajax({
            url: url,
            data: { 'id' : rowid},
            type: "post",
            cache: false,
            success: function (data) {
            }
        });
    }

In custom controller,

<?php
namespace Maxime\Helloworld\Controller\Adminhtml\Index;

Class Index extends \Magento\Backend\App\Action {

    public function __construct(\Magento\Backend\App\Action\Context $context) {
        parent::__construct($context);
    }

    public function execute() {

        $value = $this->getRequest()->getParam('id');
        $this->_resources = \Magento\Framework\App\ObjectManager::getInstance()
                    ->get('Magento\Framework\App\ResourceConnection');
                    $connection= $this->_resources->getConnection();

                    $themeTable = $this->_resources->getTableName('color_series_tab_tabless');
             $sql = "DELETE FROM $themeTable WHERE id=$value";
       // exit;
       $connection->query($sql);
    }


}
1
  • Is that the best and clean way to do it ? – androniennn Dec 6 '17 at 10:51
0

Some Other Approaches

  1. Using ResourceConnection Method
<?php
namespace Vendor\Package\Controller\Custom;
use Magento\Framework\Controller\ResultFactory;
class DeleteRecord extends \Magento\Framework\App\Action\Action
{
    protected $connection;
    protected $resource;
    protected $resultRedirect;
    protected $messageManager;

    public function __construct(
        \Magento\Framework\App\ResourceConnection $resource,
        \Magento\Framework\App\Action\Context $context,   
        \Magento\Framework\Message\ManagerInterface $messageManager,     
        ResultFactory $resultRedirect
    )
    {
        $this->connection = $resource->getConnection();
        $this->resource = $resource;
        $this->resultRedirect=$resultRedirect;
        $this->messageManager = $messageManager;
        parent::__construct($context);
    }
    public function execute()
    {

            $resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);
            $resultRedirect->setUrl($this->_redirect->getRefererUrl());            
            $delete_id=$this->getRequest()->getParam('delete_id');             
            $table = $this->resource->getTableName('tablename');
            $sql = "DELETE FROM $table WHERE id=$delete_id";
            $this->connection->query($sql);
            $this->messageManager->addSuccess( __('Record deleted Successfully !') );
            return $resultRedirect;

    }


}
?>
  1. Using ModelFactory Method
namespace Vendor\Package\Controller\Custom;

use Vendor\Package\Model\MyModelFactory;
use Magento\Framework\Controller\ResultFactory;

class DeleteRecord extends \Magento\Framework\App\Action\Action
{


    protected $myCustomModel;
    protected $resultRedirect;
    protected $messageManager;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\Message\ManagerInterface $messageManager,     
        ResultFactory $resultRedirect,
        MyModelFactory $myCustomModel
    ) {
        $this->myCustomModel= $myCustomModel;
        $this->resultRedirect=$resultRedirect;
        $this->messageManager = $messageManager;
        parent::__construct($context);
    }

    public function execute() 
    {
        $resultRedirect = $this->resultRedirect->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());        
        $delete_id = $this->getRequest()->getParam('delete_id ');
        try {
            $model = $this->myCustomModel->create();
            $model->load($delete_id);
            $model->delete();
            $this->messageManager->addSuccess( __('Record deleted Successfully !') );
            return $resultRedirect;
        } catch (\Exception $e) {
            $this->messageManager->addError($e->getMessage());
            return $resultRedirect;
        }
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.