I added an attribute in sales_order
, and display it as a column in sales_order_grid
, all is set but when I try to apply filter
on my custom column it gives me an error with a pop-up like: Attention: Something went wrong, Yes! I also get some errors in console and in network please see below images:
Here Is my code:
app/code/EC/SampleModule/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="sample_status" class="EC\SampleModule\Ui\Component\Listing\Column\Status">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">EC\SampleModule\Model\Source\printStatus</item>
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">select</item>
<item name="bodyTmpl" xsi:type="string">ui/grid/cells/html</item>
<item name="dataType" xsi:type="string">select</item>
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/select</item>
<item name="label" xsi:type="string" translate="true">Sample Status</item>
</item>
</argument>
</column>
</columns>
</listing>
app/code/EC/SampleModule/Model/Source/printStatus.php
<?php
namespace EC\SampleModule\Model\Source;
class printStatus implements \Magento\Framework\Option\ArrayInterface
{
/**
* @return array
*/
public function toOptionArray()
{
$options = [
[
'value' => 0,
'label' => __('No')
],
[
'value' => 1,
'label' => __('Yes')
]
];
return $options;
}
}
app/code/EC/SampleModule/Ui/Component/Listing/Column/Status
<?php
namespace EC\SampleModule\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;
use \Magento\Framework\App\ResourceConnection;
class Status extends Column
{
protected $_orderRepository;
protected $_searchCriteria;
protected $_resourceConnection;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
OrderRepositoryInterface $orderRepository,
SearchCriteriaBuilder $criteria,
ResourceConnection $resourceConnection,
array $components = [],
array $data = [])
{
$this->_orderRepository = $orderRepository;
$this->_searchCriteria = $criteria;
$this->_resourceConnection = $resourceConnection;
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']);
$sampleStatus = $order->getSampleStatus();
$item['sample_status'] = $sampleStatus;
}
}
return $dataSource;
}
}