I have a custom column in an admin grid, this column is populated with a custom renderer and is not actually saved to the database. Is it possible to still filter by this column?
in my Grid.php file:
$this->addColumn(
'test_column', [
'header' => __('Test Column'),
'index' => 'test_column',
'renderer' => 'Company\Module\Block\StockMovement\Renderer\Test',
'filter_condition_callback' => array($this, '_myTestFilter')
]
);
...
protected function _myTestFilter($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return $this;
}
$collection->getSelect()->where("test_column like ?", "%$value%");
return $this;
}
And then here is my "Test.php" Renderer file:
<?php
namespace Company\Module\Block\StockMovement\Renderer;
use Magento\Framework\DataObject;
class Test extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{
public function render(DataObject $row)
{
$comments = $row->getsm_comments();
if ($comments) {
return 'abc';
} else {
return 'xyz';
}
}
}
So now in my custom column on my grid with either abc
or xyz
text values. These are not actually saved in the DB but just rendered text. How can I filter using this column's text?
getsm_comments
do?yes
orno
which aren't actually saved to the table.getsm_comments
is getting comment value fromsm
"stock movements" table in db (created via a 3rd party module).movements
table and obtain the value 0/1 via sql.