0

I want to add a custom button to the action column of my rows in the admin grid.

i realise it must be added via extending Columns:

namespace Custom\Example\Ui\Component\Listing\Columns;
class EventActions extends Column
{
public function prepareDataSource(array $dataSource)
 {
 if (isset($dataSource['data']['items'])) {
 $storeId = $this->context->getFilterParam('store_id');
 foreach ($dataSource['data']['items'] as &$item) {
 $item[$this->getData('name')]['edit'] = [
 'href' => $this->urlBuilder->getUrl(
 'custom/example/edit',
 ['id' => $item['id'], 'store' => $storeId]
 ),
 'label' => __('Add Available Date'),
 'hidden' => false,
 ];
 }
 }
 return $dataSource;
 }
}

I am however not sure how to add HTML to the above method.

i tried this from this source but it did not work:

public function prepareDataSource(array $dataSource)
 {
 if (isset($dataSource['data']['items'])) {
 $fieldName = $this->getData('name');
 foreach ($dataSource['data']['items'] as & $item) { 
 $item[$fieldName . '_html'] = "<button class='button'><span>Send Mail</span></button>";
 $item[$fieldName . '_title'] = __('Please enter a message that you want to send to customer');
 $item[$fieldName . '_submitlabel'] = __('Send');
 $item[$fieldName . '_cancellabel'] = __('Reset');
 $item[$fieldName . '_customerid'] = $item['calender_id'];
 $item[$fieldName . '_formaction'] = $this->urlBuilder->getUrl('custom/example/edit');
 }
 }
 return $dataSource;
 }

UPDATE -- THE SOLUTION.

thanks to @mohit Rane for this elegant solution.

 public function prepareDataSource(array $dataSource) {
 if (isset($dataSource['data']['items'])) {
 $storeId = $this->context->getFilterParam('store_id');
 foreach ($dataSource['data']['items'] as &$item) {
 $name = $this->getData('name');
 $http = $this->urlBuilder->getUrl(
 'custom/example/edit', ['id' => $item['id'], 'store' => $storeId]
 );
 $item[$name] = html_entity_decode("<a href='{$http}'><button>Edit</button></a>");
 }
 }
 return $dataSource;
 }
asked Aug 1, 2019 at 9:26
2
  • what do you mean "did not work" did you get any error? Commented Aug 1, 2019 at 9:41
  • @MohitRane no errors. It came back blank. but i dont want any functionality in the button. i just want a plane button . nothing more Commented Aug 1, 2019 at 10:36

2 Answers 2

0

You can use the following code to add a simple button,

namespace Custom\Example\Ui\Component\Listing\Columns;
class EventActions extends Column
{
 public function prepareDataSource(array $dataSource)
 {
 if (isset($dataSource['data']['items'])) {
 $storeId = $this->context->getFilterParam('store_id');
 foreach ($dataSource['data']['items'] as &$item) {
 $name = $this->getData('name');
 $item[$name] = 
 html_entity_decode('<button>Test</button>');
 }
 }
 return $dataSource;
 }
}

Hope it helps.

answered Aug 2, 2019 at 4:40
9
  • Hi @Mohit Rane. i am sorry but it does not work. its blank. have you tried it before? Commented Aug 2, 2019 at 8:18
  • yes it's working in my module Commented Aug 2, 2019 at 9:48
  • please check now, I've updated an answer. Commented Aug 2, 2019 at 9:53
  • no. it does not work for me. what version of magento 2 are you using? Commented Aug 2, 2019 at 10:07
  • I'm using 2.2.6 if you could share a whole file code to the question, it would be much better to resolve the issue Commented Aug 2, 2019 at 10:09
0

Try with this code

const URL_PATH_MAIL = 'custom/example/edit';
public function prepareDataSource(array $dataSource)
{
 if (isset($dataSource['data']['items'])) {
 $storeId = $this->context->getFilterParam('store_id');
 foreach ($dataSource['data']['items'] as &$item) {
 $item[$name]['delete'] = [
 'href' => $this->urlBuilder->getUrl(self::URL_PATH_MAIL , ['customer_id' => $item['customer_id']]),
 'label' => __('Send Mail'),
 'confirm' => [
 'title' => __('Send Mail to %1', $item['customer_id']),
 'message' => __('Are you sure you want to Send Mail %1 record?', $item['customer_id']),
 '__disableTmpl' => true,
 ],
 'post' => true,
 ];
 }
 }
 return $dataSource;
}
answered Aug 1, 2019 at 9:48
1
  • Hi. i am trying to add a button to the row. you have not added a html button. so, i am not clear what you have tried to do Commented Aug 1, 2019 at 10:35

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.