I have a custom grid with a column definition as below:
<column name="practitioner_id" class="Amrita\Practitioner\Ui\Component\Listing\Column\Practitioner">
<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">Practitioner</item>
<item name="sortable" xsi:type="string">true</item>
</item>
</argument>
</column>
and from Amrita\Practitioner\Ui\Component\Listing\Column\Practitioner I'm preparing the data source as follows:
/**
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {
$name = $this->getData('name');
try {
$practitioner = $this->practitionerRepository->getById($item[$name]);
$item[$name] = $practitioner->getName();
} catch (NoSuchEntityException $e) {
$item[$name] = __('-');
}
}
}
return $dataSource;
}
So I'm using an external model to retrieve the practitioner name and display it in a grid (based on another model and associated practitioner_id).
The issue is that when I sort the column by clicking the header, it orders the column by the id rather than by the name, it needs to be sorted alphabetically.
How do apply this custom sorting to this column given i'm using another model to populate the data.
I can see there's the method \Magento\Ui\Component\Listing\Columns\Column::applySorting()
But I'm not sure how to manipulate this, can anyone help?
1 Answer 1
For this you have to make custom data provider. You can make custom data provider in following steps :
For the corresponding grid go to
Vendor\Module\etc\di.xmland find below lines :<virtualType name="Vendor\Module\Model\ResourceModel\SomeModel\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">Edit the line and call your custom data provider, some thing like this:
<virtualType name="Vendor\Module\Model\ResourceModel\SomeModel\Collection" type="Vendor\Module\Ui\Component\Listing\CustomDataProvider">
Now create file
CustomDataProvider.phpin directoryVendor\Module\Ui\Component\Listingwith the following content:class CustomDataProvider extends \Magento\Framework\View\Element \UiComponent\DataProvider\SearchResult { protected function _initSelect() { parent::_initSelect(); $this->getSelect()->joinLeft( [ 'secondTable' => $this->getTable('second_table_name_from_where_data_is_coming') ], 'main_table.practitioner_id = secondTable.practitioner_id', [ 'practitioner_id' ] ); return $this; } }Flush cache and test again.
-
hello @Vivek kumar, same issue it didn't work for me. here is my git, github.com/pinjar/CustomReportJafar Pinjar– Jafar Pinjar2019年10月21日 12:09:20 +00:00Commented Oct 21, 2019 at 12:09
<item name="sortable" xsi:type="string">true</item>should be a boolean:<item name="sortable" xsi:type="boolean">true</item>