2

I want to filter the Magento 2 UI component based on

$custom value in Magento 2

how I can do the dynamic filters for Magento UI components?

anyone have any idea

listing_component.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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">quick_approve_listing2.quick_quotes_listing_data_source4</item>
            <item name="deps" xsi:type="string">quick_approve_listing2.quick_quotes_listing_data_source4</item>
        </item>
        <item name="spinner" xsi:type="string">quick_quotes_columns</item>
        <!--<item name="buttons" xsi:type="array">
            <item name="add" xsi:type="array">
                <item name="name" xsi:type="string">add</item>
                <item name="label" xsi:type="string" translate="true">Add New Quote</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="string">*/*/new</item>
            </item>
        </item>-->
    </argument>
    <dataSource name="quick_quotes_listing_data_source4">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">QuoteGridDataProvider2</argument>
            <argument name="name" xsi:type="string">quick_quotes_listing_data_source4</argument>
            <argument name="primaryFieldName" xsi:type="string">quick_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                </item>
            </argument>
        </argument>
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
            </item>
        </argument>
    </dataSource>
    <container name="listing_top">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="template" xsi:type="string">ui/grid/toolbar</item>
                <item name="stickyTmpl" xsi:type="string">ui/grid/sticky/toolbar</item>
            </item>
        </argument>
        <bookmark name="bookmarks">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="storageConfig" xsi:type="array">
                        <item name="namespace" xsi:type="string">quick_quotes_listing</item>
                    </item>
                </item>
            </argument>
        </bookmark>
        <component name="columns_controls">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="columnsData" xsi:type="array">
                        <item name="provider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.quick_quotes_columns</item>
                    </item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/controls/columns</item>
                    <item name="displayArea" xsi:type="string">dataGridActions</item>
                </item>
            </argument>
        </component>
        <filterSearch name="fulltext">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="provider" xsi:type="string">quick_approve_listing2.quick_quotes_listing_data_source4</item>
                    <item name="chipsProvider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.listing_top.listing_filters_chips</item>
                    <item name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.listing_top.bookmarks</item>
                        <item name="namespace" xsi:type="string">current.search</item>
                    </item>
                </item>
            </argument>
        </filterSearch>
        <filters name="listing_filters">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="columnsProvider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.quick_quotes_columns</item>
                    <item name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.listing_top.bookmarks</item>
                        <item name="namespace" xsi:type="string">current.filters</item>
                    </item>
                    <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 name="childDefaults" xsi:type="array">
                        <item name="provider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.listing_top.listing_filters</item>
                        <item name="imports" xsi:type="array">
                            <item name="visible" xsi:type="string">quick_approve_listing2.quick_approve_listing2.quick_quotes_columns.${ $.index }:visible</item>
                        </item>
                    </item>
                </item>
                <item name="observers" xsi:type="array">
                    <item name="column" xsi:type="string">column</item>
                </item>
            </argument>
            <filterSelect name="store_id">
                <argument name="optionsProvider" xsi:type="configurableObject">
                    <argument name="class" xsi:type="string">Magento\Cms\Ui\Component\Listing\Column\Cms\Options</argument>
                </argument>
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="dataScope" xsi:type="string">store_id</item>
                        <item name="label" xsi:type="string" translate="true">Store View</item>
                        <item name="captionValue" xsi:type="string">0</item>
                    </item>
                </argument>
            </filterSelect>
        </filters>
        <massaction name="listing_massaction">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.quick_quotes_columns.ids</item>
                    <item name="indexField" xsi:type="string">quick_id</item>
                </item>
            </argument>

            <!--<action name="done">-->
            <!--    <argument name="data" xsi:type="array">-->
            <!--        <item name="config" xsi:type="array">-->
            <!--            <item name="type" xsi:type="string">done</item>-->
            <!--            <item name="label" xsi:type="string" translate="true">Done</item>-->
            <!--            <item name="url" xsi:type="url" path="quick/index/massStatus/status/Done"/>-->
            <!--        </item>-->
            <!--    </argument>-->
            <!--</action>-->
            <!--<action name="new">-->
            <!--    <argument name="data" xsi:type="array">-->
            <!--        <item name="config" xsi:type="array">-->
            <!--            <item name="type" xsi:type="string">new</item>-->
            <!--            <item name="label" xsi:type="string" translate="true">New</item>-->
            <!--            <item name="url" xsi:type="url" path="quick/index/massStatus/status/New"/>-->
            <!--        </item>-->
            <!--    </argument>-->
            <!--</action>-->
            <!--<action name="pending">-->
            <!--    <argument name="data" xsi:type="array">-->
            <!--        <item name="config" xsi:type="array">-->
            <!--            <item name="type" xsi:type="string">pending</item>-->
            <!--            <item name="label" xsi:type="string" translate="true">Pending</item>-->
            <!--            <item name="url" xsi:type="url" path="quick/index/massStatus/status/Pending"/>-->
            <!--        </item>-->
            <!--    </argument>-->
            <!--</action>-->
            <!--<action name="process">-->
            <!--    <argument name="data" xsi:type="array">-->
            <!--        <item name="config" xsi:type="array">-->
            <!--            <item name="type" xsi:type="string">process</item>-->
            <!--            <item name="label" xsi:type="string" translate="true">Under Process</item>-->
            <!--            <item name="url" xsi:type="url" path="quick/index/massStatus/status/Under Process"/>-->
            <!--        </item>-->
            <!--    </argument>-->
            <!--</action>-->
            <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">quick_approve_listing2.quick_approve_listing2.quick_quotes_columns_editor</item>
                            <item name="target" xsi:type="string">editSelected</item>
                        </item>
                    </item>
                </argument>
            </action>
        </massaction>
        <paging name="listing_paging">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.listing_top.bookmarks</item>
                        <item name="namespace" xsi:type="string">current.paging</item>
                    </item>
                    <item name="selectProvider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.quick_quotes_columns.ids</item>
                </item>
            </argument>
        </paging>
    </container>
    <columns name="quick_quotes_columns">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="storageConfig" xsi:type="array">
                    <item name="provider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.listing_top.bookmarks</item>
                    <item name="namespace" xsi:type="string">current</item>
                </item>
                <item name="editorConfig" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.quick_quotes_columns.ids</item>
                    <item name="enabled" xsi:type="boolean">true</item>
                    <item name="indexField" xsi:type="string">quick_id</item>
                    <item name="clientConfig" xsi:type="array">
                        <item name="saveUrl" xsi:type="url" path="tender/approve/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">quick_approve_listing2.quick_approve_listing2.quick_quotes_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 name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.listing_top.bookmarks</item>
                        <item name="root" xsi:type="string">columns.${ $.index }</item>
                        <item name="namespace" xsi:type="string">current.${ $.storageConfig.root}</item>
                    </item>
                </item>
            </item>
        </argument>
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">55</item>
                    <item name="indexField" xsi:type="string">quick_id</item>
                </item>
            </argument>
        </selectionsColumn>
        <column name="quick_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 name="sortOrder" xsi:type="number">0</item>
                </item>
            </argument>
        </column>
        <column name="contact_name">
            <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">Contact Name</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </column>
        <column name="company">
            <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">Title</item>
                    <item name="sortOrder" xsi:type="number">20</item>
                </item>
            </argument>
        </column>
        <column name="email">
            <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="validate-email" xsi:type="boolean">true</item>
                        </item>
                    </item>
                    <item name="label" xsi:type="string" translate="true">Email</item>
                    <item name="sortOrder" xsi:type="number">30</item>
                </item>
            </argument>
        </column>       

        <column name="status">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">FM\Tender\Model\Quick\Source\Status</item>
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">select</item>

                        <item name="default_filter" xsi:type="string">Old</item>

                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
                    <item name="editor" xsi:type="string">select</item>
                    <item name="dataType" xsi:type="string">select</item>
                    <item name="label" xsi:type="string" translate="true">Status</item>
                </item>
            </argument>

        </column>
        <column name="create_date" class="Magento\Ui\Component\Listing\Columns\Date">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">dateRange</item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
                    <item name="dataType" xsi:type="string">date</item>
                    <item name="label" xsi:type="string" translate="true">Created</item>
                </item>
            </argument>
        </column>               
        <actionsColumn name="actions" class="FM\Tender\Ui\Component\Listing\Column\Actions2">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">107</item>
                    <item name="indexField" xsi:type="string">quick_id</item>
                </item>
            </argument>
        </actionsColumn>

    </columns>
    <container name="sticky">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/sticky/sticky</item>
                <item name="toolbarProvider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.listing_top</item>
                <item name="listingProvider" xsi:type="string">quick_approve_listing2.quick_approve_listing2.quick_quotes_columns</item>
            </item>
        </argument>
    </container>
</listing>

Action2.php

    const URL_PATH_EDIT = 'tender/approve/edit';
    const URL_PATH_DELETE = 'tender/index/delete';
    /** @var UrlBuilder */
    protected $actionUrlBuilder;
    /** @var UrlInterface */
    protected $urlBuilder;
    protected $authSession;
    /**
     * @var string
     */
    private $editUrl;
    protected   $customersession;
    /**
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param UrlBuilder $actionUrlBuilder
     * @param UrlInterface $urlBuilder
     * @param array $components
     * @param array $data
     * @param string $editUrl
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        \Magento\Customer\Model\Session $customersession,
        \Magento\Backend\Model\Auth\Session $authSession,
        UrlBuilder $actionUrlBuilder,
        UrlInterface $urlBuilder,
        array $components = [],
        array $data = [],
        $editUrl = self::URL_PATH_EDIT
    ) {
        $this->urlBuilder = $urlBuilder;
        $this->actionUrlBuilder = $actionUrlBuilder;
        $this->customersession=$customersession;
        $this->authSession = $authSession;
        $this->editUrl = $editUrl;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }
    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        $custom2=$this->authSession;
        $user=$custom2->getCurrentUser();
        //$userid=$user->getUserId();
        $custom=$this->customersession;
        $val=$custom->getMyName();
        if (isset($dataSource['data']['items'])) {
              $data1=$dataSource['data']['items'];
              //$data2=$data1;
              foreach ($dataSource['data']['items'] as & $item) {
                $name = $this->getData('name');
                $new=$this->getData();
//                $nn=$new->addFieldToFilter('location',$custom);
                if (isset($item['quick_id'])) {
                    $item[$name]['edit'] = [
                        'href' => $this->urlBuilder->getUrl($this->editUrl, ['quick_id' => $item['quick_id']]),
                        'label' => __('Edit')
                    ];
                    // $item[$name]['delete'] = [
                    //     'href' => $this->urlBuilder->getUrl(self::URL_PATH_DELETE, ['quick_id' => $item['quick_id']]),
                    //     'label' => __('Delete'),
                    //     'confirm' => [
                    //         'title' => __('Delete ${ $.$data.contact_name }'),
                    //         'message' => __('Are you sure you wan\'t to delete a ${ $.$data.contact_name } record?')
                    //     ]
                    // ];
                }
            }
        }
        return $dataSource;
    }
}

1 Answer 1

1

override customer_listing.xml in your theme

<listingToolbar name="listing_top">
        <filters name="listing_filters">
            <filterSelect name="column_name" provider="${ $.parentName }" component="Magento_Ui/js/form/element/ui-select" template="ui/grid/filters/elements/ui-select">
                <settings>
                    <options class="Vendor\Module\Model\Config\Source\Options"/>  //Option Path
                    <caption translate="true">...Select...</caption>
                    <label translate="true">Label</label>
                    <dataScope>salesrep_name</dataScope>
                    <imports>
                        <link name="visible">componentType = column, index = ${ $.index }:visible</link>
                    </imports>
                </settings>
            </filterSelect>
            <columns name="customer_columns">
                    <column name="column_name">
                        <argument name="data" xsi:type="array">
                            <item name="config" xsi:type="array">
                                <item name="visible" xsi:type="boolean">true</item>
                                <item name="sortOrder" xsi:type="number">20</item>
                                <item name="label" xsi:type="string" translate="true">Label</item>
                            </item>
                        </argument>
                    </column>
            </columns>
    </listingToolbar>

Create Option.php file

<?php

namespace Vendor\Module\Model\Config\Source;

use Magento\Framework\Data\OptionSourceInterface;

class Options implements OptionSourceInterface
{

    public $_options;

    public function toOptionArray() {
        return array(
            array('value' => 'helllo', 'label'=>'helllo'),
            array('value' => 'helllo1', 'label'=>'helllo1'),
        );
    }

}
8
  • that field not visible for admin grid ...... Commented Jun 28, 2019 at 6:21
  • public function prepareDataSource(array $dataSource) { } can i use here addFieldToFilter() for $datasource Commented Jun 28, 2019 at 6:22
  • please attach code for understanding Commented Jun 28, 2019 at 6:24
  • i want the filter but that filed not showing in grid Commented Jun 28, 2019 at 6:34
  • add emil column in customer grid and add dynamic filter for that field ??? Commented Jun 28, 2019 at 6:37

Your Answer

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

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