只需在您的ui_component
布局文件中添加此标签
<column name="logo" class="NAMESPACE\MODULENAME\Ui\Component\Listing\Columns\Logo">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="component" xsi:type="string">Magento_Ui/js/grid/columns/thumbnail</item>
<!--<item name="add_field" xsi:type="boolean">true</item>-->
<item name="sortable" xsi:type="boolean">false</item>
<item name="altField" xsi:type="string">name</item>
<item name="has_preview" xsi:type="string">1</item>
<item name="label" xsi:type="string" translate="true">Brand Logo</item>
</item>
</argument>
</column>
并创建我们在ui_component
专栏中分配的新文件
<?php
namespace NAMESPACE\MODULENAME\Ui\Component\Listing\Columns;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
class Logo extends \Magento\Ui\Component\Listing\Columns\Column
{
const NAME = 'logo';
const ALT_FIELD = 'name';
protected $_storeManager;
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
\Magento\Framework\UrlInterface $urlBuilder,
array $components = [],
array $data = [],
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
parent::__construct($context, $uiComponentFactory, $components, $data);
$this->_storeManager = $storeManager;
$this->urlBuilder = $urlBuilder;
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
$fieldName = $this->getData('name');
foreach ($dataSource['data']['items'] as & $item) {
$mediaRelativePath=$this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA);
$logoPath=$mediaRelativePath.$item['logo'];
$item[$fieldName . '_src'] = $logoPath;
$item[$fieldName . '_alt'] = $this->getAlt($item);
$item[$fieldName . '_link'] = $this->urlBuilder->getUrl(
'brand/manage/edit',
['brand_id' => $item['brand_id'], 'store' => $this->context->getRequestParam('store')]
);
$item[$fieldName . '_orig_src'] = $logoPath;
}
}
return $dataSource;
}
/**
* @param array $row
*
* @return null|string
*/
protected function getAlt($row)
{
$altField = self::ALT_FIELD;
return isset($row[$altField]) ? $row[$altField] : null;
}
}
在prepareDataSource
函数中,您将获得每个列对象。
希望这会帮助你。
if($item[$fieldName] != '')
到if($item['url'] != '')
和'pathtoyourimage/'.$item[$fieldName]
到'pathtoyourimage/'.$item['url']
。我$fieldName
返回的是“图像”,但是我的数据库字段称为“ URL”。其余的$item[$fieldName . '***']
留在原地。