I have create two columns "Sending" and "Remaining". The value of them is based on the "In stock" column. When i tried to sort the order of one of column then it doesn't work. It gives me an error. eq In Stock = 10. Entered 3 in "Sending" Remaining column will change to 7.
The sorting doesn't work for both columns.
-
can you show your error log?Morgan Smith– Morgan Smith2019年06月07日 19:06:08 +00:00Commented Jun 7, 2019 at 19:06
-
{"error":"UI component could not be rendered because of system exception","errorcode":"42"Avesh Naik– Avesh Naik2019年06月07日 19:09:39 +00:00Commented Jun 7, 2019 at 19:09
-
magento.stackexchange.com/questions/109224/… this looks similar maybe start here?Morgan Smith– Morgan Smith2019年06月07日 19:11:25 +00:00Commented Jun 7, 2019 at 19:11
-
Not able to get the solution.Avesh Naik– Avesh Naik2019年06月07日 19:16:58 +00:00Commented Jun 7, 2019 at 19:16
-
so your file permissions are right and you aren't getting any ajax issues?Morgan Smith– Morgan Smith2019年06月07日 19:22:04 +00:00Commented Jun 7, 2019 at 19:22
1 Answer 1
This is because you don't have a column in the database that matches the name of your two added fields. Magento takes the name from the xml file in which you specify the grid view and when you want to sort by this column it performs a database query in which it takes the column name from the xml file and tries to add it to the SQL query.
All you have to do is override method \Magento\Ui\Component\Listing\Columns\Column::applySorting()
You can do it simply by modifying your xml like that:
<?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="your_column" class="Vendor\Module\Ui\Component\YourColumn">
<settings>
<sortable>true</sortable>
</settings>
</column>
</columns>
</listing>
And write your own class Vendor\Module\Ui\Component\YourColumn that extends \Magento\Ui\Component\Listing\Columns\Column. I think something like that should work:
<?php
declare(strict_types=1);
namespace Vendor\Module\Ui\Component;
use Magento\Ui\Component\Listing\Columns\Column;
class YourColumn extends Column
{
/**
* Apply sorting
*
* @return void
*/
protected function applySorting()
{
$sorting = $this->getContext()->getRequestParam('sorting');
$isSortable = $this->getData('config/sortable');
if (
$isSortable !== false
&& !empty($sorting['field'])
&& !empty($sorting['direction'])
&& $sorting['field'] === $this->getName()
) {
$this->getContext()->getDataProvider()->addOrder(
'enter here your db column name by which you want to sort',
strtoupper($sorting['direction'])
);
}
}
}
-
Thank you Bartosz for pointing me in the right direction. Because my ui_grid column was based on an entity_id but showed the related increment id, when sorting I would get this "UI component could not be rendered because of system exception". A quick fix based on your answer was to add <settings><sortable>false</sortable></settings> to my custom column so the customer no longer got this error. Now it works as it should.Isolde– Isolde2020年09月10日 18:47:49 +00:00Commented Sep 10, 2020 at 18:47
-
It would been better if you had given an example for Vendor\Module\Ui\Component\YourColumnAvesh Naik– Avesh Naik2020年12月14日 09:38:44 +00:00Commented Dec 14, 2020 at 9:38
-
@AveshNaik I added example, let me know what do you think.Bartosz Boguszewski– Bartosz Boguszewski2020年12月16日 12:14:18 +00:00Commented Dec 16, 2020 at 12:14
Explore related questions
See similar questions with these tags.