2

Magento 2 Filter is not working. In the below code column created through layout xml are getting filter, but on column created via column class file filter is not working. I have found that one solution on stack to implement this code in collection class, but its not working.

protected function _initSelect()
    {
        $this->addFilterToMap('warehouse_id', $mainTable.'.warehouse_id');
        parent::_initSelect();
    }

1) Admin Ui layout file.

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © 2016 Amasty. All rights reserved.
 */
-->
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">amasty_multi_inventory_stock_listing.amasty_multi_inventory_stock_listing_data_source</item>
            <item name="deps" xsi:type="string">amasty_multi_inventory_stock_listing.amasty_multi_inventory_stock_listing_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">amasty_multi_inventory_stock_columns</item>
    </argument>
    <dataSource name="amasty_multi_inventory_stock_listing_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Amasty\MultiInventory\Ui\DataProvider\StockDataProvider</argument>
            <argument name="name" xsi:type="string">amasty_multi_inventory_stock_listing_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">entity_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">entity_id</item>
                    </item>
                </item>
            </argument>
        </argument>
    </dataSource>
    <listingToolbar name="listing_top">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sticky" xsi:type="boolean">false</item>
            </item>
        </argument>
        <bookmark name="bookmarks"/>
        <columnsControls name="columns_controls"/>
        <exportButton class="Amasty\MultiInventory\Ui\Component\ExportButton">
            <argument name="warehouses" xsi:type="object">Amasty\MultiInventory\Ui\Component\Listing\Column\Stock\Source\Warehouse</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="component" xsi:type="string">Amasty_MultiInventory/js/grid/export</item>
                    <item name="displayArea" xsi:type="string">dataGridActions</item>
                    <item name="options" xsi:type="array">
                        <item name="cvs" xsi:type="array">
                            <item name="value" xsi:type="string">csv</item>
                            <item name="label" xsi:type="string" translate="true">CSV</item>
                            <item name="url" xsi:type="string">amasty_multi_inventory/stock/gridToCsv</item>
                        </item>
                        <item name="xml" xsi:type="array">
                            <item name="value" xsi:type="string">xml</item>
                            <item name="label" xsi:type="string" translate="true">Excel XML</item>
                            <item name="url" xsi:type="string">amasty_multi_inventory/stock/gridToXml</item>
                        </item>
                    </item>
                </item>
            </argument>
        </exportButton>
        <filters name="listing_filters">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="templates" xsi:type="array">
                        <item name="filters" xsi:type="array">
                            <item name="select" xsi:type="array">
                                <item name="component" xsi:type="string">Magento_Ui/js/form/element/ui-select</item>
                                <item name="template" xsi:type="string">ui/grid/filters/elements/ui-select</item>
                            </item>
                        </item>
                    </item>
                </item>
            </argument>
            <filterSelect name="low_stock">
                <argument name="optionsProvider" xsi:type="configurableObject">
                    <argument name="class" xsi:type="string">Amasty\MultiInventory\Ui\Component\Listing\Column\Stock\Source\Warehouse</argument>
                </argument>
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="provider" xsi:type="string">${ $.parentName }</item>
                        <item name="dataScope" xsi:type="string">low_stock</item>
                        <item name="caption" xsi:type="string" translate="true">...</item>
                        <item name="label" xsi:type="string" translate="true">Low Stock</item>
                    </item>
                </argument>
            </filterSelect>
        </filters>
        <massaction name="listing_massaction">
            <action name="edit">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="type" xsi:type="string">edit</item>
                        <item name="label" xsi:type="string" translate="true">Edit</item>
                        <item name="callback" xsi:type="array">
                            <item name="provider" xsi:type="string">amasty_multi_inventory_stock_listing.amasty_multi_inventory_stock_listing.amasty_multi_inventory_stock_columns_editor</item>
                            <item name="target" xsi:type="string">editSelected</item>
                        </item>
                    </item>
                </argument>
            </action>
        </massaction>
        <paging name="listing_paging"/>
    </listingToolbar>
    <columns name="amasty_multi_inventory_stock_columns" class="Amasty\MultiInventory\Ui\Component\Listing\Columns">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="editorConfig" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">amasty_multi_inventory_stock_listing.amasty_multi_inventory_stock_listing.amasty_multi_inventory_stock_columns.ids</item>
                    <item name="enabled" xsi:type="boolean">true</item>
                    <item name="component" xsi:type="string">Amasty_MultiInventory/js/grid/editing/editor</item>
                    <item name="indexField" xsi:type="string">entity_id</item>
                    <item name="clientConfig" xsi:type="array">
                        <item name="saveUrl" xsi:type="url" path="amasty_multi_inventory/stock/inlineEdit"/>
                        <item name="validateBeforeSave" xsi:type="boolean">false</item>
                    </item>
                </item>
                <item name="childDefaults" xsi:type="array">
                    <item name="fieldAction" xsi:type="array">
                        <item name="provider" xsi:type="string">amasty_multi_inventory_stock_listing.amasty_multi_inventory_stock_listing.amasty_multi_inventory_stock_columns_editor</item>
                        <item name="target" xsi:type="string">startEdit</item>
                        <item name="params" xsi:type="array">
                            <item name="0" xsi:type="string">${ $.$data.rowIndex }</item>
                            <item name="1" xsi:type="boolean">true</item>
                        </item>
                    </item>
                </item>
            </item>
        </argument>
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="indexField" xsi:type="string">entity_id</item>
                </item>
            </argument>
        </selectionsColumn>
        <column name="entity_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="label" xsi:type="string" translate="true">ID</item>
                </item>
            </argument>
        </column>
        <column name="name">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="add_field" xsi:type="boolean">true</item>
                    <item name="label" xsi:type="string" translate="true">Name</item>
                    <item name="sortOrder" xsi:type="number">30</item>
                </item>
            </argument>
        </column>
        <column name="sku">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">SKU</item>
                    <item name="sortOrder" xsi:type="number">60</item>
                </item>
            </argument>
        </column>
        <column name="status">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Magento\Catalog\Model\Product\Attribute\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="add_field" xsi:type="boolean">true</item>
                    <item name="dataType" xsi:type="string">select</item>
                    <item name="label" xsi:type="string" translate="true">Status</item>
                    <item name="sortOrder" xsi:type="number">90</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Column File

<?php

namespace Amasty\MultiInventory\Ui\Component\Listing;

class Columns extends \Magento\Ui\Component\Listing\Columns
{
    const COLUMN_WAREHOUSE = 'Amasty\MultiInventory\Ui\Component\Listing\Column\Stock\Warehouse';
    const COLUMN_WAREHOUSE_SHIP = 'Amasty\MultiInventory\Ui\Component\Listing\Column\Stock\WarehouseShip';
    const COLUMN_WAREHOUSE_AVAILABLE = 'Amasty\MultiInventory\Ui\Component\Listing\Column\Stock\WarehouseAvailable';
    const COLUMN_WAREHOUSE_SHELF = 'Amasty\MultiInventory\Ui\Component\Listing\Column\Stock\WarehouseShelf';

    /**
     * Default columns max order
     */
    const DEFAULT_COLUMNS_MAX_ORDER = 100;

    /**
     * @var array
     */
    protected $filterMap = [
        'default' => 'text',
        'select' => 'select',
        'boolean' => 'select',
        'multiselect' => 'select',
        'date' => 'dateRange',
    ];

    /**
     * @var \Amasty\MultiInventory\Model\ResourceModel\Warehouse\CollectionFactory
     */
    private $collectionFactory;

    /**
     * @var \Amasty\MultiInventory\Ui\Component\ColumnFactory
     */
    private $columnFactory;

    /**
     * Columns constructor.
     * @param \Magento\Framework\View\Element\UiComponent\ContextInterface $context
     * @param \Amasty\MultiInventory\Ui\Component\ColumnFactory $columnFactory
     * @param \Amasty\MultiInventory\Model\ResourceModel\Warehouse\CollectionFactory $collectionFactory
     * @param array $components
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Amasty\MultiInventory\Ui\Component\ColumnFactory $columnFactory,
        \Amasty\MultiInventory\Model\ResourceModel\Warehouse\CollectionFactory $collectionFactory,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $components, $data);
        $this->columnFactory = $columnFactory;
        $this->collectionFactory = $collectionFactory;
    }

    /**
     *  Prepare fields
     */
    public function prepare()
    {
        $classes = [
            [
                'code' => '',
                'class' => self::COLUMN_WAREHOUSE,
                'title' => 'Qty',
                'filter' => 'textRange',
                'editor' => 1,
                'validation' => 1,
            ]
        ];
        $columnSortOrder = self::DEFAULT_COLUMNS_MAX_ORDER;
        $collection = $this->collectionFactory->create()->addFieldToFilter('manage', 1)->setOrder('warehouse_id');
        foreach ($collection as $warehouse) {
            $config = [];
            if (!isset($this->components[$warehouse->getCode()])) {
                foreach ($classes as $class) {
                    if ($warehouse->getIsGeneral() && $class['code'] == 'shelf') {
                        continue;
                    }
                    if ($class['code']) {
                        $class['code'] = $warehouse->getCode() . "_" . $class['code'];
                    } else {
                        $class['code'] = $warehouse->getCode();
                    }
                    $class['title'] = $warehouse->getTitle();
                    $class['is_general'] = $warehouse->getIsGeneral();
                    $config['sortOrder'] = $warehouse->getId() * $columnSortOrder;
                    $column = $this->columnFactory->create($class, $this->getContext(), $config);
                    $column->prepare();
                    $this->addComponent($class['code'], $column);
                    $columnSortOrder++;
                }
            }
        }
        parent::prepare();
    }
}

3) Collection class file.

<?php

namespace Amasty\MultiInventory\Model\ResourceModel\Warehouse\Grid;

use Magento\Framework\Api\Search\SearchResultInterface;
use Magento\Framework\Search\AggregationInterface;
use Amasty\MultiInventory\Model\ResourceModel\Warehouse\Collection as WarehouseCollection;

class Collection extends WarehouseCollection implements SearchResultInterface
{
    /**
     * @var AggregationInterface
     */
    protected $aggregations;

    /**
     * Collection constructor.
     * @param \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory
     * @param \Psr\Log\LoggerInterface $logger
     * @param \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy
     * @param \Magento\Framework\Event\ManagerInterface $eventManager
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\EntityManager\MetadataPool $metadataPool
     * @param $mainTable
     * @param $eventPrefix
     * @param $eventObject
     * @param $resourceModel
     * @param string $model
     * @param \Magento\Framework\DB\Adapter\AdapterInterface|null $connection
     * @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb|null $resource
     */
    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,
        \Magento\Framework\EntityManager\MetadataPool $metadataPool,
        $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
    ) {
        parent::__construct(
            $entityFactory,
            $logger,
            $fetchStrategy,
            $eventManager,
            $connection,
            $resource
        );
        $this->_eventPrefix = $eventPrefix;
        $this->_eventObject = $eventObject;
        $this->_init($model, $resourceModel);
        $this->setMainTable($mainTable);
        $this->_initSelect();
    }


    protected function _initSelect()
    {
        $this->addFilterToMap('warehouse_id', $mainTable.'.warehouse_id');
        parent::_initSelect();
    }

    /**
     * @return AggregationInterface
     */
    public function getAggregations()
    {
        return $this->aggregations;
    }

    /**
     * @param AggregationInterface $aggregations
     * @return $this
     */
    public function setAggregations($aggregations)
    {
        $this->aggregations = $aggregations;

        return $this;
    }

    /**
     * Get search criteria.
     *
     * @return \Magento\Framework\Api\SearchCriteriaInterface|null
     */
    public function getSearchCriteria()
    {
        return null;
    }

    /**
     * Set search criteria.
     *
     * @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
     * @return $this
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
    {
        return $this;
    }

    /**
     * Get total count.
     *
     * @return int
     */
    public function getTotalCount()
    {
        return $this->getSize();
    }

    /**
     * Set total count.
     *
     * @param int $totalCount
     * @return $this
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function setTotalCount($totalCount)
    {
        return $this;
    }

    /**
     * Set items list.
     *
     * @param \Magento\Framework\Api\ExtensibleDataInterface[] $items
     * @return $this
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
     */
    public function setItems(array $items = null)
    {
        return $this;
    }
}
  • Any news on this? Did you manage to make it work? – Vulovic Vukasin Mar 5 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.