2

I have created a custom button in UI component magento2 I want to have post data ( id ) on button click in controller

Button in UI Component

<item name="combinations" xsi:type="string">Vendor\Module\Block\Adminhtml\Form\Buttons\CombinationButton</item>

Block

class CombinationButton extends GenericButton implements ButtonProviderInterface
{
 
    /**
     * @return array
     */
    public function getButtonData()
    {
        return [
            'label' => __('Generate Combinations'),
            'on_click' => sprintf("location.href = '%s';", $this->getUrl()),
            'class' => 'action-secondary',
            'data_attribute' => [
                'mage-init' => [
                    'buttonAdapter' => [
                        'actions' => [
                            [
                                'targetName' => 'hairforadmin_formula_form.hairforadmin_formula_form',
                                'actionName' => 'generatecombinations',
                                'params' => [
                                    false
                                ]
                            ]
                        ]
                    ]
                ]
            ],
            'sort_order' => 100
        ];
    }
 
    /**
     * Get URL for back (reset) button
     *
     * @return string
     */
    public function getUrl()
    {
        return $this->getUrl('*/*/generatecombinations');
    }
}

Controller

class GenerateCombinations extends Action

{

private $modelFactory;

public function __construct(Context $context, FormulaFactory $modelFactory)
{
    $this->modelFactory = $modelFactory;
    parent::__construct($context);
}

public function execute()
{
    $id = $this->getRequest()->getParam('formula_id');

    echo "<pre>";
    print_r($id);
    exit;
}
}

0

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.

Browse other questions tagged or ask your own question.