2

I have created a custom grid on customer view in admin

 protected function _construct()
    {
        parent::_construct();

        $this->setDefaultSort('model','desc');
        $this->setSortable(true);
        $this->setPagerVisibility(true);
        $this->setFilterVisibility(true);
    }

are used to get sorter , pager and filters

How can i add new custom button here above the grid?

enter image description here

2 Answers 2

4

In your grid.php (where grid columns and sorter etc)

 public function getMainButtonsHtml()
    {
        $html = parent::getMainButtonsHtml();//get the parent class buttons
        $addButton = $this->getLayout()->createBlock('Magento\Backend\Block\Widget\Button')
            ->setData(array(
            'label'     => 'Add',
            'onclick'   => "setLocation('#')",
            'class'   => 'task'
        ))->toHtml();
        return $addButton.$html;
    }

SOURCE

1

You need to check magento 2 default functionlity to add button in any grid.

$this->buttonList->add(
                'nameofbutton',
                [
                    'label' => __('YOUR TEXT'),
                    'onclick' => 'setLocation(\'' . YOUR ACTION URL . '\')',
                    'class' => 'add'
                ],
                0
);
6
  • Already tried not working Sep 16, 2016 at 13:33
  • Is this code give any error then specify that. Sep 16, 2016 at 13:34
  • that is not any add method in 'magento\module-backend\Block\Widget\Grid\Extended.php' , anyways found the solution thanks for answering Sep 16, 2016 at 13:37
  • @VishwasBhatnagar Can you please tell us the way to add button in grid . The above code is not working properly. Feb 22, 2018 at 10:29
  • @VinaySikarwar Have you tried my solution Feb 26, 2018 at 6:13

Your Answer

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

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