1

I am creating 3 custom columns in sales order grid column:

  1. Country
  2. Area
  3. City

Filters are working but when I try to sort the column it shows Attention: Something went wrong

Iksula/InternationalShipping/view/adminhtml/ui_component/sales_order_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
 <columns name="sales_order_columns">
 <column name="country_id" class="Iksula\InternationalShipping\Ui\Component\Listing\Column\Country">
 <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">Country</item>
 </item>
 </argument>
 </column>
 <column name="region" class="Iksula\InternationalShipping\Ui\Component\Listing\Column\Area">
 <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">Area</item>
 </item>
 </argument>
 </column>
 <column name="city" class="Iksula\InternationalShipping\Ui\Component\Listing\Column\City">
 <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">City</item>
 </item>
 </argument>
 </column>
 </columns>
</listing>

Iksula/InternationalShipping/Ui/Component/Listing/Column/Country.php

<?php
namespace Iksula\InternationalShipping\Ui\Component\Listing\Column;
use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;
class Country extends Column
{
 protected $_orderRepository;
 protected $_searchCriteria;
 public function __construct(ContextInterface $context, 
 UiComponentFactory $uiComponentFactory, 
 OrderRepositoryInterface $orderRepository, 
 SearchCriteriaBuilder $criteria, 
 \Magento\Directory\Model\CountryFactory $countryFactory,
 array $components = [], 
 array $data = [])
 {
 $this->_orderRepository = $orderRepository;
 $this->_searchCriteria = $criteria;
 $this->_countryFactory = $countryFactory;
 parent::__construct($context, $uiComponentFactory, $components, $data);
 }
 public function prepareDataSource(array $dataSource)
 {
 if (isset($dataSource['data']['items'])) {
 foreach ($dataSource['data']['items'] as & $item) {
 $order = $this->_orderRepository->get($item["entity_id"]);
 $country_code = $order->getShippingAddress()->getData('country_id');
 $country = $this->_countryFactory->create()->loadByCode($country_code);
 $item[$this->getData('name')] = $country->getName();
 }
 }
 return $dataSource;
 }
}

Iksula/InternationalShipping/Ui/Component/Listing/Column/Area.php

<?php
namespace Iksula\InternationalShipping\Ui\Component\Listing\Column;
use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;
class Area extends Column
{
 protected $_orderRepository;
 protected $_searchCriteria;
 public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, OrderRepositoryInterface $orderRepository, SearchCriteriaBuilder $criteria, array $components = [], array $data = [])
 {
 $this->_orderRepository = $orderRepository;
 $this->_searchCriteria = $criteria;
 parent::__construct($context, $uiComponentFactory, $components, $data);
 }
 public function prepareDataSource(array $dataSource)
 {
 if (isset($dataSource['data']['items'])) {
 foreach ($dataSource['data']['items'] as & $item) {
 $order = $this->_orderRepository->get($item["entity_id"]);
 $region= $order->getShippingAddress()->getData('region');
 $item[$this->getData('name')] = $region;
 }
 }
 return $dataSource;
 }
}

Iksula/InternationalShipping/Ui/Component/Listing/Column/City.php

<?php
namespace Iksula\InternationalShipping\Ui\Component\Listing\Column;
use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;
class City extends Column
{
 protected $_orderRepository;
 protected $_searchCriteria;
 public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, OrderRepositoryInterface $orderRepository, SearchCriteriaBuilder $criteria, array $components = [], array $data = [])
 {
 $this->_orderRepository = $orderRepository;
 $this->_searchCriteria = $criteria;
 parent::__construct($context, $uiComponentFactory, $components, $data);
 }
 public function prepareDataSource(array $dataSource)
 {
 if (isset($dataSource['data']['items'])) {
 foreach ($dataSource['data']['items'] as & $item) {
 $order = $this->_orderRepository->get($item["entity_id"]);
 $city= $order->getShippingAddress()->getData('city');
 $item[$this->getData('name')] = $city;
 }
 }
 return $dataSource;
 }
}
asked Nov 22, 2019 at 12:54

2 Answers 2

1

Since your filters are not working. replace this

<item name="filter" xsi:type="string">text</item>

By

<item name="filter" xsi:type="boolean">false</item>

and for sorting

<item name="sortable" xsi:type="boolean">false</item>

This will solve your problem, pls mark it as solution if it works for you.

answered Nov 22, 2019 at 13:03
3
  • filter is working but sorting is not working Commented Nov 22, 2019 at 13:07
  • check my updated answer Commented Nov 22, 2019 at 14:49
  • but this is not the solution to my question. I need filters and sorting to work Commented Nov 25, 2019 at 5:48
0

Add Sort order to the column

<item name="sortOrder" xsi:type="string">40</item>

answered Jan 11, 2020 at 11:13

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.