4

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;
    }
}
5
  • You create the grid via Grid container or UI component? Please give us these code lines of your custom column(Product) Commented May 5, 2016 at 10:34
  • Shared the code. Commented May 6, 2016 at 9:07
  • created the grid via UI component Commented May 6, 2016 at 9:14
  • @saravanavelu are you able to change the sort order! I am facing same problem Commented Jul 6, 2016 at 6:03
  • I have added tht column to custom table and it was working. May I know in which grid you facing this issue? Commented Jul 6, 2016 at 6:05

2 Answers 2

0

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>
-1
  1. Remove the corresponding row in the ui_bookmark table. This fixed the problem for me.
  2. Clean your cache after doing that.

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.