在Magento2后台网格中添加新的列

3.53K 浏览M2插件和模版分享区

在Magento2后台网格中添加新的列

首先,创建和现有网格相同名称的UI_ComponentXML文件。

/app/code/Webkul/Test/view/adminhtml/ui_component/add_column_example.xml,如产品的网格文件是prouct_listing.xml

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nOnamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">     <columns name="test_attribute_columns">         <column name="attributename" class="WebkulTestUiComponentListingColumnShowAttrAction">             <argument name="data" xsi:type="array">                 <item name="config" xsi:type="array">                     <item name="sortable" xsi:type="boolean">false</item>                     <item name="label" xsi:type="string" translate="true">AttributeName</item>                     <item name="sortOrder" xsi:type="number">1</item>                 </item>             </argument>         </column>     </columns> </listing>


  • name:必须和现有网格的名称相同。
  • class:包含类名,我们在其中定义该列的值。
  • label:用于列的名称,在网格中显示的标题。
  • sortOrder:用于列的排序(好像不管设置多少都会显示在最后面)

然后创建文件 app/code/Max/Test/Ui/Component/Listing/Column/ShowAttrAction.php

<?php namespace MaxTestUiComponentListingColumn;    use MagentoFrameworkViewElementUiComponentContextInterface; use MagentoFrameworkViewElementUiComponentFactory; use MagentoUiComponentListingColumnsColumn;    class ShowAttrAction extends Column {       /**      * Prepare Data Source      *      * @param array $dataSource      * @return array      */     public function prepareDataSource(array $dataSource)     {         if (isset($dataSource[&#39;data&#39;][&#39;items&#39;])) {             foreach ($dataSource[&#39;data&#39;][&#39;items&#39;] as & $item) {                 $item[$this->getData(&#39;name&#39;)] = "hello world"; //这是设置你想要展示的值             }         }         return $dataSource;     } }

这个文件用来传递数据,$DataSource返回包含字段值的数组。
$this->getData(&#39;name&#39;)是传递到网格中的值。

然后刷新缓存,在网格中就可以看到添加的列了。

0