Question

Actually in below listing.xml file i am trying to sort customer_id column DESC/ASC but not working fine. After using below code it's loading the data in ASC order only if sort customer_id column one by one.

Vendor\Module\view\adminhtml\ui_component\listing.xml

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
   
        <column name="customer_id" class="Vendor\Module\Ui\Component\Listing\Column\Metainfo" >
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="boolean">false</item>
                    <item name="sortable" xsi:type="boolean">true</item>
                    <item name="label" xsi:type="string" translate="true">Customer ID</item>
                </item>
            </argument>
        </column>

</listing>

Vendor\Module\Model\ResourceModel\Module\Collection.php

<?php

namespace Vendor\Module\Model\ResourceModel\Module;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    protected function _construct()
    {
        $this->_init('Vendor\Module\Model\Module', 'Vendor\Module\Model\ResourceModel\Module');
    }
    
    protected function _initSelect()
    {
        parent::_initSelect();
        
         $this->addFilterToMap('entity_id', 'main_table.entity_id');

         $sales_order = $this->getTable('sales_order');
        
         $this->getSelect()->join(
            ['sales_order' => $sales_order],
            'main_table.entity_id = sales_order.customer_id',
            [
                'sales_order.customer_id','main_table.*'
            ]
        )->group('sales_order.customer_id')->order('main_table.entity_id','DESC'); 
        
        return $this;
    } 
}

Please check above code. Any help would be appriciated. Thanks in Advance !!! :)

Was it helpful?

Solution

Vendor\Module\view\adminhtml\ui_component\listing.xml

 <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
       
           <column name="entity_id">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="filter" xsi:type="string">false</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">Customer ID</item>
                        <item name="sorting" xsi:type="string">desc</item>
                    </item>
                </argument>
            </column>
    
    </listing>

Vendor\Module\Model\ResourceModel\Module\Collection.php

<?php

namespace Vendor\Module\Model\ResourceModel\Module;

use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;

class Collection extends AbstractCollection
{
    protected function _construct()
    {
        $this->_init('Vendor\Module\Model\Module', 'Vendor\Module\Model\ResourceModel\Module');
    }
    
    protected function _initSelect()
    {
         parent::_initSelect();

         $sales_order = $this->getTable('sales_order');
        
         $this->getSelect()->join(
            ['sales_order' => $sales_order],
            'main_table.entity_id = sales_order.customer_id',
            [
                'sales_order.customer_id','main_table.*'
            ]
        )->group('sales_order.customer_id'); 
        
        $this->addFilterToMap('entity_id', 'main_table.entity_id');
    } 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top