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

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 – Vishwas Bhatnagar Sep 16 '16 at 13:33
  • Is this code give any error then specify that. – Dhiren Vasoya Sep 16 '16 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 – Vishwas Bhatnagar Sep 16 '16 at 13:37
  • @VishwasBhatnagar Can you please tell us the way to add button in grid . The above code is not working properly. – Vinay Sikarwar Feb 22 '18 at 10:29
  • @VinaySikarwar Have you tried my solution – Vishwas Bhatnagar Feb 26 '18 at 6:13

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.