How can I use join table to display second table data in admin grid? Admin grid is created using layout & block in proper manner.
I used this link (how to create a grid without using ui component in magento 2) as reference.
Using below code I display data
<block class="Magento\Backend\Block\Widget\Grid\Column" name="recipient_email">
<arguments>
<argument name="header" xsi:type="string" translate="true">Recipient Email</argument>
<argument name="index" xsi:type="string">recipient_email</argument>
<argument name="renderer" xsi:type="string">Comapny\Module\Block\Adminhtml\Widget\Grid\Column\Renderer\Recipientemail</argument>
</block>
But issue while searching & sorting
I am using this for overriding the default module of enterprise (gift card account)
Code of
Company/Module/Block/Adminhtml/Widget/Grid/Column/Renderer/Recipientemail.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Company\Module\Block\Adminhtml\Widget\Grid\Column\Renderer;
class Recipientemail extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{
protected $_storeManager;
public function __construct(
\Magento\Backend\Block\Context $context,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Sales\Model\Order\Item $orderItem,
array $data = []
) {
parent::__construct($context, $data);
$this->_storeManager = $storeManager;
$this->orderitem = $orderItem;
}
public function render(\Magento\Framework\DataObject $row)
{
$code = $row->getCode();
$result = array();
$salesorderitem = $this->orderitem->getCollection()
->addFieldToFilter('product_options', array('like' => '%'.$code.'%'));
if(!empty($salesorderitem))
{
foreach($salesorderitem as $orderdata){
$productOptions = $orderdata->getProductOptions();
$recipientemail = $productOptions['giftcard_recipient_email'];
if(!empty($recipientemail)){
return $recipientemail;
}else{
return '-';
}
}
}else{
return '-';
}
}
public function getContents($str, $startDelimiter, $endDelimiter) {
$contents = array();
$startDelimiterLength = strlen($startDelimiter);
$endDelimiterLength = strlen($endDelimiter);
$startFrom = $contentStart = $contentEnd = 0;
while (false !== ($contentStart = strpos($str, $startDelimiter, $startFrom))) {
$contentStart += $startDelimiterLength;
$contentEnd = strpos($str, $endDelimiter, $contentStart);
if (false === $contentEnd) {
break;
}
$contents[] = substr($str, $contentStart, $contentEnd - $contentStart);
$startFrom = $contentEnd + $endDelimiterLength;
}
return $contents;
}
}
Code for Adminhtml\Grid\Filter\Recipientemail.php
<?php
namespace Comapny\Module\Block\Adminhtml\Grid\Filter;
class Recipientemail extends \Magento\Backend\Block\Widget\Grid\Column\Filter\Text {
public function getCondition()
{
if ($this->getValue() === null) {
return null;
}
return null;
}
}