0

I have created the add button using the below block file. On button click, I have called return FilterControl.addItem() from my template file seo.phtml But it shows errors like below

Uncaught ReferenceError: FilterControl is not defined at HTMLButtonElement.onclick

Please provide me with a solution

Note : I have referred core template Magento/Catalog/view/adminhtml/templates/catalog/product/edit/price/tier.phtml

Block.php <?php

namespace xx\yy\Block\Adminhtml\News\Edit\Tab;

use xx\yy\Model\SeoPageFactory;

class File extends \Magento\Backend\Block\Widget implements
\Magento\Framework\Data\Form\Element\Renderer\RendererInterface
{

    protected $_seopage;
    protected $_attributeFactory;
    protected $_template = 'xx_yy::seo.phtml';

    public function __construct(\Magento\Backend\Block\Template\Context $context, SeoPageFactory $seopage, \Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory $attributeFactory, array $data = array())
    {
        $this->_seopage = $seopage;
        $this->_attributeFactory = $attributeFactory;
        parent::__construct($context, $data);
    }

    protected function _prepareLayout()
    {
        $button = $this->addChild(
                'add_button', \Magento\Backend\Block\Widget\Button::class, ['label' => __('Add Filter'), 'click' => 'return FilterControl.addItem()',
            'class' => 'add']
        );
        $button->setName('add_filter_button');
        $this->setChild('add_button', $button);
        return parent::_prepareLayout();
    }

    public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
    {
        $this->_element = $element;

        $this->setElement($element);
        return $this->toHtml();
    }

    public function getAttributes()
    {
        $collection = $this->_attributeFactory->create()->addVisibleFilter()->addIsFilterableFilter();
        return $collection;
    }

    public function getFilters()
    {
        $pageId = $this->getRequest()->getParam('id');
        if ($pageId) {
            $page = $this->_seopage->create()->load($pageId);
            return $page->getFilters();
        }
    }

    public function getAddButtonHtml()
    {
        return $this->getChildHtml('add_button');
    }

}

seo.phtml

<?php $_htmlId = $this->getElement()->getHtmlId();
?>
<?php $_htmlClass = $this->getElement()->getClass() ?>
<?php $_htmlName = $this->getElement()->getName() ?>
<div class="admin__control-table-wrapper">
    <table>
        <tr id="attribute-options-table">
            <td class="label"><?php echo $this->getElement()->getLabel() ?></td>
            <td colspan="10" class="data-grid">
                <table  class="admin__control-table" cellspacing="0"  id="filters_table">
                    <col width="120" />
                    <col width="120" />
                    <col width="1" />
                    <thead >
                        <tr id="attribute-options-table">
                            <th class = "col-default control-table-actions-th"><?= /* @escapeNotVerified */ __('Attributes') ?></th>
                            <th class = "col-default control-table-actions-th"><?= /* @escapeNotVerified */ __('Options') ?></th>

                        </tr>
                    </thead>
                    <tbody id="<?= /* @escapeNotVerified */ $_htmlId ?>_container"></tbody>
                   <!--<tbody id="container"></tbody>-->
                    <tfoot>
                        <tr>
                            <td></td>
                            <td colspan="4" class="a-right"><?= $block->getAddButtonHtml() ?></td>
                        </tr>
                    </tfoot>
                </table>
            </td>
        </tr>
    </table>

    <script>
        require([
            'mage/template',
            "prototype",
            "mage/adminhtml/form"
        ], function ($, mageTemplate) {

            // var filterRowTemplate = '<p>hi</p>';

            alert("asgsedg");
            var FilterControl = {

                addItem: function () {
                    alert("test");
                }
            };




        });


    </script>
</div>
4
  • FilterControl is not mention in template file. Look at group price or tier price template .
    – Sourav
    Commented Apr 3, 2018 at 9:47
  • File Path: Folder_name/vendor\magento\module-catalog\view\adminhtml\templates\catalog\product\edit\price\tier.phtml
    – Sourav
    Commented Apr 3, 2018 at 9:53
  • use xx\yy\Model\SeoPageFactory; how you extend model class from Block parent class ????
    – Sourav
    Commented Apr 3, 2018 at 9:59
  • Reference : Block Tier namespace Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Price\Tier
    – Sourav
    Commented Apr 3, 2018 at 10:02

1 Answer 1

0

in your Block.php File

please replace code :

protected function _prepareLayout()
    {
        $button = $this->addChild(
                'add_button', \Magento\Backend\Block\Widget\Button::class, ['label' => __('Add Filter'), 'click' => 'return FilterControl.addItem()',
            'class' => 'add']
        );
        $button->setName('add_filter_button');
        $this->setChild('add_button', $button);
        return parent::_prepareLayout();
    }

TO

protected function _prepareLayout()
    {
        $button = $this->addChild(
                'add_button', \Magento\Backend\Block\Widget\Button::class, ['label' => __('Add Filter'), 'onClick' => 'return FilterControl.addItem()',
            'class' => 'add']
        );
        $button->setName('add_filter_button');
        $this->setChild('add_button', $button);
        return parent::_prepareLayout();
    }
1
  • Thanks, for the response but it is not working
    – Jaisa
    Commented Apr 3, 2018 at 6:19

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.