1

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.

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

  • I am using UI component for this. So where I need to define the _addColumnFilterToCollection ? – manish_khot May 11 '17 at 7:01
  • It's a different function. U can define after/before _prepareCollection(). Can print query for log – Ankit Shah May 11 '17 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? – manish_khot May 11 '17 at 7:21
  • U r using .xml file for listing? – Ankit Shah May 11 '17 at 7:23
  • Yes as mentioned in question I am using ui-component in .xml. – manish_khot May 11 '17 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>
  • Doesn't work if you already have name column in the main table (or, for example, join yet another table that has name column). – Jānis Elmeris Nov 28 '17 at 10:35
0

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;
}

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.