2

I have tried to add Category column in Product grid admin area.I have added it successfully by below code.

<column name="category" class="Chilly\Productsgrid\Ui\Component\Listing\Column\Category">
    <argument name="data" xsi:type="array">
        <item name="options" xsi:type="object">Chilly\Productsgrid\Model\Category\Categorylist</item>
        <item name="config" xsi:type="array">
            <item name="filter" xsi:type="string">select</item>
            <item name="dataType" xsi:type="string">text</item>
            <item name="label" xsi:type="string" translate="true">Categories</item>
            <item name="sortOrder" xsi:type="number">50</item>
        </item>
    </argument>
</column>

In Chilly\Productsgrid\Ui\Component\Listing\Column\Category.php, prepareDataSource() method

$fieldName = $this->getData('name');
if (isset($dataSource['data']['items'])) {
    //$categories=array();
    foreach ($dataSource['data']['items'] as & $item) {
        $p_id=$item['entity_id'];
        $product=$this->_productloader->create()->load($p_id);
        $cats = $product->getCategoryIds();
        $objectManager   = \Magento\Framework\App\ObjectManager::getInstance();
        $categories=array();
        if(count($cats) ){
            foreach($cats as $cat){
                $category = $objectManager->create('Magento\Catalog\Model\Category')->load($cat);
                $categories[]=$category->getName();
            }

}
$item[$fieldName]=implode(',',$categories);
    }
}
return $dataSource;

Got Categories list in product grid. I have tried the following code for add filter,

<item name="filter" xsi:type="string">select</item>

and for add options to category filter dropdown,

<item name="options" xsi:type="object">Chilly\Productsgrid\Model\Category\Categorylist</item>

category dropdown came in filter section. But not add categories list to category dropdown. Have any idea, How to add category filter on product grid in magento2?

1

By default Magento2 not include js for dropdown.So override and use product_listing.xml in your module and add your column class for category options.( for format : see status)

Path:

<vendor>/<module>view/adminhtml/ui_component/product/product_listing.xml

and use below code :-

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
  <listingToolbar name="listing_top">     
    <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>
    </filters>
</listingToolbar>
    <columns name="product_columns" class="Magento\Catalog\Ui\Component\Listing\Columns">
        <column name="category_ids" class="<VENDOR><MODULE_NAME>\Ui\Component\Listing\Column\Category">
            <argument name="data" xsi:type="array">
               <item name="options" xsi:type="object"><VENDOR><MODULE_NAME>\Model\Category\Categorylist</item>
                <item name="config" xsi:type="array">
                   <item name="filter" xsi:type="string">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">Categories</item>
                    <item name="sortOrder" xsi:type="number">100</item>
                </item>
            </argument>
        </column>
     </columns>
</listing>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.