3

I have a customer column in my custom module admin grid (created using UI component) where I show the customer name. But in database I save customer Id. I need to use the filter on customer name as input box in my grid filters. When I filter the grid by customer ID it works fine.

My question is how I can filter it by using customer name? Same as we do in Magento 1 using filter_condition_callback.

1

3 Answers 3

1

Yes. If one wants to join eav_attribute_option_value table to filtering option value, one can use below code.

$this->addColumn(
        'product_manufacturer',
        [
            'header' => __('Manufacturer'),
            'index' => 'manufacturer',
            'filter_condition_callback' => [$this, 'filterCustomManufacturer'],
            'header_css_class' => 'col-manufacturer',
            'column_css_class' => 'col-manufacturer'
        ]
    );

Below add filter condition callback function including joins like,

protected function filterCustomManufacturer($collection, $column)
{
    if (!$value = $column->getFilter()->getValue()) {
        return $this;
    }

    $collection->getSelect()->joinLeft(array('c_p_e_i'=>'catalog_product_entity_int'),
    'e.row_id = c_p_e_i.row_id',array('value'));

    $collection->getSelect()->joinLeft(array('attribute_option'=>'eav_attribute_option_value'),
    'c_p_e_i.value = attribute_option.option_id and attribute_option.store_id = 0',
     array('eaov_val'=>'value' , 'eaov_option'=>'option_id'));

     $collection->getSelect()->where("`attribute_option`.`value` like ?", "%$value%");

    return $this;
}
0

Please check

Magento 2 : How can I load an admin grid with pre-defined filter values?

U have to apply

<item name="filter_url_params" xsi:type="array">
   <item name="customer_id" xsi:type="string">*</item>
</item>

& follow for code. Check in URL it's passing or not.

https://github.com/thejinxters/magento2-testing/blob/master/lib/internal/Magento/Framework/View/Element/UiComponent/DataProvider/DataProvider.php

U need to add prepareUpdateUrl() function

8
  • I am using UI component for this. So where I need to define the _addColumnFilterToCollection ? May 11, 2017 at 7:01
  • It's a different function. U can define after/before _prepareCollection(). Can print query for log
    – Jackson
    May 11, 2017 at 7:04
  • I didn't used file CompanyName\ModuleName\Block\Adminhtml\Custom\Edit\Tab\Grid.php for fetching collection. Rather I have used CompanyName\ModuleName\Model\ResourceModel\Custom\Grid\Collection.php which extends ...\ResourceModel\Custom\Collection.php and fetching the collection from there. Could you tell me the path where should I write those functions? May 11, 2017 at 7:21
  • U r using .xml file for listing?
    – Jackson
    May 11, 2017 at 7:23
  • Yes as mentioned in question I am using ui-component in .xml. May 11, 2017 at 7:24
0

I have solved the problem by joining my custom module collection with 'customer_grid_flat' table.

In Vendor\ModuleName\Model\ResourceModel\ModuleName\Collection.php

    protected function _renderFiltersBefore()
{
    $joinTable = $this->getTable('customer_grid_flat');
    $this->getSelect()->joinleft($joinTable.' as customer','main_table.customer_id = customer.entity_id', array('name'=> 'name'));
    parent::_renderFiltersBefore();
}

And in Uicomponent xml Vendor/ModuleName/view/adminhtml/ui_component/custom_module_listing.xml added column for field 'name'

    <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="label" xsi:type="string" translate="true">Customer Name</item>
                <item name="dataType" xsi:type="string">text</item>
            </item>
        </argument>
    </column>
1
  • Doesn't work if you already have name column in the main table (or, for example, join yet another table that has name column). Nov 28, 2017 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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