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>