Question

Can anyone help me, how to fetch the custom module admin collection for filters drop down section in magento 2 admin.

Please find the screen shots below:

https://prnt.sc/hpesq6

Here is the fields to display in dropdown:

https://prnt.sc/hpeu3x

This is my drop down code: under view/adminhtml/Ui_component

<column name="department" class="Ewall\HelpDesk\Ui\Component\Listing\Column\TicketDepartment">
            <argument name="data" xsi:type="array">
                <item name="options" xsi:type="object">Ewall\HelpDesk\Model\Ticket\Source\DepartmentCollection</item>
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">select</item>
                    <item name="label" translate="true" xsi:type="string">Department123</item>
                    <item name="editor" xsi:type="array">
                        <item name="editorType" xsi:type="string">select</item>
                        <item name="validation" xsi:type="array">
                            <item name="required-entry" xsi:type="boolean">false</item>
                        </item>
                    </item>
                </item>
            </argument>
        </column>

and This is DepartmentCollection.php

<?php


/**
* A Magento 2 module named Ewall/HelpDesk
* Copyright (C) 2017  Ewall Solutions Pvt Ltd
*
*/
namespace Ewall\HelpDesk\Model\Ticket\Source;

class DepartmentCollection implements \Magento\Framework\Data\OptionSourceInterface
{
    /**
     * @var \Ewall\HelpDesk\Api\GroupRepositoryInterface
     */
    private $departmentRepositoryInterface;

    /**
     * @var \Magento\Framework\Api\SearchCriteriaBuilder
     */
    private $searchCriteriaBuilder;

    /**
     * @var \Magento\Framework\Convert\DataObject
     */
    private $objectConverter;

    /**
     * @param \Ewall\HelpDesk\Api\DepartmentRepositoryInterface $departmentRepositoryInterface
     * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
     * @param \Magento\Framework\Convert\DataObject $objectConverter
     * @param \Ewall\HelpDesk\Model\ResourceModel\Department\CollectionFactory $collectionFactory
     */
    public function __construct(
        \Ewall\HelpDesk\Api\DepartmentRepositoryInterface $departmentRepositoryInterface,
        \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
        \Magento\Framework\Convert\DataObject $objectConverter,
        \Ewall\HelpDesk\Model\ResourceModel\Department\CollectionFactory $collectionFactory
   ) {
        $this->departmentRepositoryInterface = $departmentRepositoryInterface;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->objectConverter = $objectConverter;
        $this->_departmentCollectionFactory = $collectionFactory;

   }
   public function toOptionArray($addEmpty = true)
   {
      /** @var \Ewall\HelpDesk\Model\ResourceModel\Department\Collection $collection */
      $instance = \Magento\Framework\App\ObjectManager::getInstance();
      $product_collections = $instance ->get('\Ewall\HelpDesk\Model\ResourceModel\Department\CollectionFactory');
$collections = $product_collections->create();
      $options = [];
      if ($addEmpty) {
         $options[] = ['label' => __('-- Please Select a Category --'), 'value' => ''];
      }
      foreach ($collections as $category) {
         $options[] = ['label' => $category->getTitle(), 'value' => $category->getDepartmentId()];
      }
      return $options;
   }

Thanks

Was it helpful?

Solution

When you define your fields datatype and filter as select. Magento will automatically display your column filter as dropdown.

    <column name="type">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">Namespace\Modulename\Model\Config\Source\Departments</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="dataType" xsi:type="string">select</item>
                <item name="sorting" xsi:type="string">asc</item>
                <item name="sortOrder" xsi:type="number">2</item>
                <item name="label" translate="true" xsi:type="string">Departments</item>
            </item>
        </argument>
    </column>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top