1

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 !!! :)

asked Jun 29, 2020 at 8:37

1 Answer 1

0

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');
 } 
}
answered Jul 1, 2020 at 15:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.