5

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

  • Add some code of your uicomponent grid – Priyank Dec 19 '17 at 5:46
  • thanks for reply @priyank ,Iam not aware of file to get the dropdown data please help me how to add. – shivashankar m Dec 19 '17 at 5:57
  • just place your grid data code in here – Priyank Dec 19 '17 at 6:19
5

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>
  • yes iam getting dropdown but i wants to display the data in the dropdown fields – shivashankar m Dec 19 '17 at 7:06
  • Show your option code – Priyank Dec 19 '17 at 8:47
  • Hi @priyank Please find the above code now you can get the idea – shivashankar m Dec 19 '17 at 10:34
  • Hi Priyank finally this is worked for me – shivashankar m Dec 20 '17 at 12:05

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.