分类
服务器

Magento2 Grid 如何通过ID展示名称

#  \view\adminhtml\ui_component\sales_order_grid.xml
 <column name="distributor_id" class="Maijindou\Sales\Ui\Component\Listing\Column\Distributor">
            <settings>
                <label translate="true">Distributor ID</label>
                <bodyTmpl>ui/grid/cells/html</bodyTmpl>
                <sortable>false</sortable>
            </settings>
        </column>
# \Ui\Component\Listing\Column\Distributor.php
<?php


namespace Maijindou\Sales\Ui\Component\Listing\Column;


use FME\GoogleMapsStoreLocator\Model\Storelocator;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;

class Distributor extends Column
{
    private $storeKey = 'distributor_id';
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        array $components = [],
        array $data = [])
    {
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * Get data
     *
     * @param array $item
     * @return string
     */
    protected function prepareItem(array $item)
    {
        return '展示的内容';
    }

    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                $item[$this->getData('name')] = $this->prepareItem($item);
            }
        }
        return $dataSource;
    }
}