我将自定义列添加到管理网格,像这样
<column name="customer_name" class="Vendor\Module\Ui\Component\Listing\Columns\CustomerName">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="editor" xsi:type="string">text</item>
<item name="sortable" xsi:type="string">true</item>
<item name="label" xsi:type="string" translate="true">Customer Name</item>
<item name="sortOrder" xsi:type="number">30</item>
</item>
</argument>
</column>
在我的CustomerName类中,我为此列创建值:
public function prepareDataSource(array $dataSource)
{
$fieldName = $this->getData('name');
foreach ($dataSource['data']['items'] as & $item) {
$customer = $this->customerRepository->getById($item['customer_id']);
$name = $customer ? $customer->getFirstName().' <'.$customer->getEmail().'>' : '';
$item[$fieldName] = $name;
}
return $dataSource;
}
它按预期显示在网格中。但是当我尝试按此列或过滤器排序时-发生错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customer_name' in 'order clause'
我怎样才能解决这个问题?
更新
现在,我尝试通过删除CustomerName类(并在xml的列标记中删除对其的引用)来解决此问题,而是在集合类中添加了_renderFiltersBefore()函数
protected function _renderFiltersBefore() {
$joinTable = $this->getTable('customer_entity');
$this->getSelect()->join($joinTable.' as customer_entity','main_table.customer_id = customer_entity.entity_id', array('*'));
$this->getSelect()->columns('CONCAT(firstname," <",email,">") as customer_name');
parent::_renderFiltersBefore();
}
现在排序工作正常,但过滤不起作用(得到相同的错误)