I am trying to add duplicate button to action column, but it is not working. How to handle this. Please help me
1 Answer
In Your UI Compoenent Grid add actionColumn
in columns
tag
<actionsColumn name="actions" class = "TheRealHunzik\ModuleName\Ui\Component\Listing\Sizechart\Column\Action">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="resizeEnabled" xsi:type="boolean">false</item>
<item name="resizeDefaultWidth" xsi:type="string">107</item>
<item name="indexField" xsi:type="string">id</item>
</item>
</argument>
</actionsColumn>
Now in You TheRealHunzik\ModuleName\Ui\Component\Listing\Sizechart\Column\Action.php
file add like this
<?php
namespace TheRealHunzik\ModuleName\Ui\Component\Listing\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\UrlInterface;
class Action extends Column
{
/** Url path */
const ROW_EDIT_URL = 'therealhunzik_modulename/modulename/addrow';
const ROW_DUPLICATE_URL = 'therealhunzik_modulename/modulename/duplicate';
/** @var UrlInterface */
protected $_urlBuilder;
/**
* @var string
*/
private $_editUrl;
/**
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param UrlInterface $urlBuilder
* @param array $components
* @param array $data
* @param string $editUrl
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
UrlInterface $urlBuilder,
array $components = [],
array $data = [],
$editUrl = self::ROW_EDIT_URL,
$duplicateUrl = self::ROW_DUPLICATE_URL
) {
$this->_urlBuilder = $urlBuilder;
$this->_editUrl = $editUrl;
$this->_duplicateUrl = $duplicateUrl;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
/**
* Prepare Data Source.
*
* @param array $dataSource
*
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as &$item) {
$name = $this->getData('name');
if (isset($item['entity_id'])) {
$item[$name]['edit'] = [
'href' => $this->_urlBuilder->getUrl(
$this->_editUrl,
['id' => $item['entity_id']]
),
'label' => __('Edit'),
];
$item[$name]['suplicate'] = [
'href' => $this->_urlBuilder->getUrl(
$this->_duplicateUrl,
['id' => $item['entity_id']]
),
'label' => __('Duplicate'),
];
}
}
}
return $dataSource;
}
}
Now you have to add Controller and Write code to Duplicate specific Item