Magento – Custom renderered column sorting not working magento2

adminhtmlgridmagento2renderer

I have created the custom grid with one custom column(Products) by renderer method which shows product name. When I tried to apply sorting it looks the custom table in which other columns exists but this field is not available in that table as it rendered, later it shows application went wrong error.

enter image description here

app/code/local/Module/Name/view/adminhtml/ui_component/list.xml

    <column name="products" class="Module\Name\Ui\Component\Listing\Column\Products">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="indexField" xsi:type="string">pdt_id</item>
                        <item name="label" xsi:type="string" translate="true">Products</item>
                        <item name="sortOrder" xsi:type="number">200</item>
                    </item>
                </argument>
     </column>

app/code/local/Module/Name/Ui/Component/Listing/Column/Products.php

namespace Module\Name\Ui\Component\Listing\Column;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;

/**
 * Class BlockActions
 */
class Products extends Column
{
    /**
     * @var Magento\Sales\Model\Order
     */
    protected $_productCollectionFactory;

    /**
     * Constructor
     *
     * @param ContextInterface $context
     * @param UiComponentFactory $uiComponentFactory
     * @param UrlInterface $urlBuilder
     * @param array $components
     * @param array $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        CollectionFactory $product,
        array $components = [],
        array $data = []
    ) {
        $this->_productCollectionFactory = $product;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $pdtNames = array();           
                $productCollection = $this->_productCollectionFactory->create();
                $productCollection->addAttributeToSelect('*');
                $productCollection->addAttributeToFilter('developer',array('eq' =>$item['name']));

                foreach ($productCollection as $collection){
                    $pdtNames[] = $collection->getName();
                }
                $partnerPdts = implode(', ', $pdtNames);
                $item['partners_products'] = $partnerPdts;
            }
        }

        return $dataSource;
    }
}

Best Answer

Try to add the sortOrder attribute. I face the same issue and adding it, fixed my sorting column issue after rendering some specific columns.

<column name="products" class="Module\Name\Ui\Component\Listing\Column\Products" sortOrder="100">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="indexField" xsi:type="string">pdt_id</item>
            <item name="label" xsi:type="string" translate="true">Products</item>
            <item name="sortOrder" xsi:type="number">200</item>
        </item>
    </argument>
</column>