我想在Magento 2的Product Grid中添加一个新列。我在模块di.xml
文件夹中创建了etc
文件,其代码如下:
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Magento\Catalog\Block\Adminhtml\Product\Grid" type="Package\Module\Block\Grid" />
</config>
在Grid
区块中,我有以下代码:
namespace Package\Module\Block;
use Magento\Store\Model\Store;
class Grid extends \Magento\Catalog\Block\Adminhtml\Product\Grid
{
protected function _prepareColumns()
{
$this->addColumn(
'newfield',
[
'header' => __('Test Name'),
'index' => 'name',
'class' => 'xxx'
]
);
return parent::_prepareColumns();
}
}
有人可以指导我这个。
答案 0 :(得分:1)
您可以使用product_listing.xml添加 视图\ adminhtml \ ui_component \ product_listing.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="product_columns" class="Magento\Catalog\Ui\Component\Listing\Columns">
<column name="new_field">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="add_field" xsi:type="boolean">true</item>
<item name="label" xsi:type="string" translate="true">Ne Field</item>
<item name="sortOrder" xsi:type="number">75</item>
</item>
</argument>
</column>
</columns>
</listing>
答案 1 :(得分:0)
只需将 fieldClass 属性添加到列配置:
<column name="column_name">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="fieldClass" xsi:type="string">xxx-class-name</item>
</item>
</argument>
</column>