0

I am able to add a button to Product Listing page. But how do I add onclick to it?

This is how I am adding the button:

$subject->addButton('amazon_sync_button',[
            'label' => __('Sync with Amazon'),
            'id' => 'amazon-sync-button',
            'on_click' => 'setLocation(\'' . $this->sync() . '\')'
        ]);

I have created a function sync(), but this gets executed even before button click.

I saw some answer using getURL(), but I am not sure how to achieve it. Can anyone guide me on how to do this?

1 Answer 1

0
<?php


use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class Sync implements ButtonProviderInterface
{
 /**
  * @var \Magento\Backend\Block\Widget\Container
  */
 public $container;
 /**
  * Url Builder
  *
  * @var \Magento\Framework\UrlInterface
  */
 public $urlBuilder;
 /**
  * Registry
  *
  * @var \Magento\Framework\Registry
  */
 public $registry;
/**
  * Sync constructor.
  * @param \Magento\Backend\Block\Widget\Context $context
  * @param \Magento\Framework\Registry $registry
  * @param \Magento\Backend\Block\Widget\Container $container
  */

 public function __construct(
     \Magento\Backend\Block\Widget\Context $context,
     \Magento\Framework\Registry $registry,
     \Magento\Backend\Block\Widget\Container $container
 ) {
     $this->container = $container;
     $this->urlBuilder = $context->getUrlBuilder();
     $this->registry = $registry;
 }
 /**
  * @return array|bool
  */
 public function getButtonData()
 {
     $id = $this->container->getRequest()->getParam('id');
     if (!empty($id)) {
         $data = [
             'class' => 'action-secondary scalable delete',
             'label' => __('Sync with Amazon'),
             'on_click' => "syncConfirm('Are you sure you want to do this account?','".$this->getSyncUrl($id)."')",
             'sort_order' => 20,
             'data_attribute' => [
                 'url' => $this->getSyncUrl($id)
             ],
         ];
         return $data;
     }
     return false;
 }

 /**
  * @return string
  */
 public function getSyncUrl($id)
 {
     return $this->urlBuilder->getUrl('*/account/sync', ['id' => $id]);
 }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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