0

I have created a table with columns like patient_first_name, patient_last_name etc, I am able to get them in an Admin Grid Column.

<column name="patient_first_name" class="Vendor\Module\Ui\Component\Listing\Column\PatientName">
            <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">Patient Name</item>
                </item>
            </argument>
        </column>

Inside the class, I concat first name and last name to display them in a single column in the admin grid using.

public function prepareDataSource(array $dataSource)
    {
        if(isset($dataSource['data']['items'])) {
            foreach($dataSource['data']['items'] as &$items) {
                $items['patient_first_name'] = $items['patient_first_name']." ".$items['patient_last_name'];
            }
        }
        return $dataSource;
    }

So this does its job to display the first name and last name together in the grid column, but when I use any filter to say, John Doe it searches only the patient_first_name column, which is correct since it is mapped that particular column but since the values would be stored as John in patient_first_name column and Doe in patient_last_name column, it will not return any result.

I used this solution to join both first and last names and show them together. How can I merge columns in UI grid in Magento 2 The issue comes that when you add a filter Magento tries to look for a particular column in Db, so I can't use something like patient_name and was getting the same error as here Magento 2 - custom admin grid field - error when sorting or filtering

I have tried _renderFiltersBefore but the issue with select remains.

I want to know a way how I can override the filter and write my own method which gives a select searching both first name and last name.

1 Answer 1

0

In listing xml, inside < filters > tag

<filterRange name=""amount"" class=""Vendor\Module\Ui\Component\Filters\Type\AmountRange"" />

In Vendor\Module\Ui\Component\Filters\Type\AmountRange.php

namespace Vendor\Module\Ui\Component\Filters\Type;
class AmountRange extends \Magento\Ui\Component\Filters\Type\Range
{

/**
 * Apply filter by its type
 *
 * @param string $type
 * @param string $value
 * @return void
 */
  protected function applyFilterByType($type, $value)
  {

        $filter = $this->filterBuilder->setConditionType($type)
            ->setField($this->getName())
            ->setValue($value)
            ->create();

        $this->getContext()->getDataProvider()->addFilter($filter);

  }
}

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.