I am implementing Grid for categories collection using UI component.
I have used the following code
Vendor/Module/view/adminhtml/layout/vendor_index_index.xml
<body>
<referenceContainer name="content">
<uiComponent name="vendor_manage_listing"/>
</referenceContainer>
</body>
Vendor/Module/view/adminhtml/ui_component/vendor_manage_listing.xml
<columns name="seoreport_category_columns">
<selectionsColumn name="ids">
<settings>
<indexField>entity_id</indexField>
</settings>
</selectionsColumn>
<column name="entity_id">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="editor" xsi:type="array">
<item name="editorType" xsi:type="string">text</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
</item>
</item>
<item name="label" xsi:type="string" translate="true">Category Id</item>
</item>
</argument>
</column>
<column name="value">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">false</item>
<item name="label" xsi:type="string" translate="true">Category Name</item>
</item>
</argument>
</column>
<column name="is_active">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\Status</item>
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">select</item>
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
<item name="dataType" xsi:type="string">select</item>
<item name="label" translate="true" xsi:type="string">Category Status</item>
</item>
</argument>
</column>
</columns>
Vendor/Module/Model/ResourceModel/Category.php
<?php
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
class Category extends AbstractDb
{
protected function _construct()
{
/* Main Table Name */
$this->_init('catalog_category_entity','entity_id');
}
}
Vendor/Module/Model/ResourceModel/Category/Collection.php
<?php
class Collection extends AbstractCollection
{
protected function _construct()
{
$this->_init('Vendor\Module\Model\Category',
'Vendor\Module\Model\ResourceModel\Category');
}
protected function _initSelect()
{
parent::_initSelect();
return $this;
}
}
Vendor/Module/Model/ResourceModel/Category/Grid/Collection.php
<?php
use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\Api\Search\AggregationInterface;
use Vendor\Module\Model\ResourceModel\Category\Collection as EntityCollection;
class Collection extends EntityCollection implements SearchResultInterface
{
protected $aggregations;
protected $request;
public function __construct(
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
\Magento\Store\Model\StoreManagerInterface $storeManager,
$mainTable,
$eventPrefix,
$eventObject,
$resourceModel,
$model = 'Magento\Framework\View\Element\UiComponent\DataProvider\Document',
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
\Magento\Framework\App\Request\Http $request
) {
parent::__construct(
$entityFactory,
$logger,
$fetchStrategy,
$eventManager,
$connection,
$resource
$this->request = $request;
);
$this->_eventPrefix = $eventPrefix;
$this->_eventObject = $eventObject;
$this->_init($model, $resourceModel);
$this->setMainTable($mainTable);
}
/**
* @return AggregationInterface
*/
public function getAggregations()
{
return $this->aggregations;
}
public function setItems(array $items = null)
{
return $this;
}
protected function _renderFiltersBefore()
{
$filters = $this->request->getParam('filters');
if (isset($filters['is_active'])) {
$is_active = $filters['is_active'];
if ($is_active == 0) {
$this->getSelect()->orWhere("main_table.is_active = 1");
}
}
parent::_renderFiltersBefore();
}
}
Vendor\Module\Model\Config\Source\Status.php
use Magento\Framework\Option\ArrayInterface;
class Status implements ArrayInterface
{
public function toOptionArray()
{
$result = [];
foreach ($this->getOptions() as $value => $label) {
$result[] = [
'value' => $value,
'label' => $label,
];
}
return $result;
}
public function getOptions()
{
return [
'1' => __('All Categories'),
'0' => __('Visible Categories')
];
}
}
If the first option selected I need to load all categories if the second option is selected I need to show only categories which are enabled.
I am looking for below 2 solutions.
1.Filter should work once applied
2.I have to show Category Status only in Filter not in Column.
How can we implement above functionality. Someone please help me on this. Thanks in Advance!!
Vendor\Module\Model\ResourceModel\Category\Grid\Collection
file?