2016-09-30 28 views
0

我是新手Magento2开发人员。 现在我正在制作一个小模块,并且卡在一个地方。 我建管理网格founded example这里是我di.xml在Magento 2的后端修改自定义网格

<preference for="Magento\Catalog\Model\Product" type="Vendor\Module\Model\Product" /> 
<virtualType name="Vendor\Module\Model\ResourceModel\Grid\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult"> 
     <arguments> 
      <argument name="mainTable" xsi:type="string">vendor_module</argument> 
      <argument name="resourceModel" xsi:type="string">Vendor\Module\Model\ResourceModel\Grid</argument> 
     </arguments> 
</virtualType> 
<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory"> 
     <arguments> 
      <argument name="collections" xsi:type="array"> 
       <item name="grid_record_grid_list_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\Grid\Grid\Collection</item> 
      </argument> 
     </arguments> 
</type> 

同时,我使用的布局XML文件硬编码列里:

... 
    <column name="customer" > 
     <argument name="data" xsi:type="array"> 
      <item name="config" xsi:type="array"> 
       <item name="filter" xsi:type="string">false</item> 
       <item name="label" xsi:type="string" translate="true">Customer</item> 
       </item> 
      </argument> 
    </column> 
... 

我的表中的列,如:产品ID,客户ID,价格,状态

而且我的问题是:

  • 如何将客户ID转换为名字+姓氏?
  • “状态”有3种不同的状态(0,1和2) - 我如何将它们转换为人类可读的单词? (undefined,good,bad)
  • 如何添加到同一网格另一列例如$ price + 10%

回答

1

在该组件的XML可以定义一个UI类以辅助内的Magento 2.显示自定义/可读的数据有很多的核心内的例子,如缩略图正被显示在目录网格视图。

使用,作为一个例子,这里的内catalog/view/adminhtml/ui_component/product_listing.xml列定义:

<column name="thumbnail" class="Magento\Catalog\Ui\Component\Listing\Columns\Thumbnail"> 
     <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">Thumbnail</item> 
       <item name="sortOrder" xsi:type="number">20</item> 
      </item> 
     </argument> 
    </column> 

正如你可以看到有几个参数可以传递到列定义,包括取决于类型的组件您尝试显示的数据。在这种情况下,它是一个缩略图。回顾JS文件显示,将下面方法中设置的数据作为实际缩略图显示是逻辑。这不一定是要求。

在列标签的定义类中,您会看到Magento\Catalog\Ui\Component\Listing\Columns\Thumbnail。这是一个为数据显示方式定义辅助方法的类,并且以定义的列组件可以正确呈现的方式解析要显示的数据。

狠抓方法该类中,prepareDataSource

/** 
* 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) { 
      $product = new \Magento\Framework\DataObject($item); 
      $imageHelper = $this->imageHelper->init($product, 'product_listing_thumbnail'); 
      $item[$fieldName . '_src'] = $imageHelper->getUrl(); 
      $item[$fieldName . '_alt'] = $this->getAlt($item) ?: $imageHelper->getLabel(); 
      $item[$fieldName . '_link'] = $this->urlBuilder->getUrl(
       'catalog/product/edit', 
       ['id' => $product->getEntityId(), 'store' => $this->context->getRequestParam('store')] 
      ); 
      $origImageHelper = $this->imageHelper->init($product, 'product_listing_thumbnail_preview'); 
      $item[$fieldName . '_orig_src'] = $origImageHelper->getUrl(); 
     } 
    } 

    return $dataSource; 
} 

你可以使用这个方法来格式化你显示成你需要的格式的数据。

例如,道路的价格通过其定义的列级显示在目录中电网(格式化,以正确的货币)是:

public function prepareDataSource(array $dataSource) 
{ 
    if (isset($dataSource['data']['items'])) { 
     $store = $this->storeManager->getStore(
      $this->context->getFilterParam('store_id', \Magento\Store\Model\Store::DEFAULT_STORE_ID) 
     ); 
     $currency = $this->localeCurrency->getCurrency($store->getBaseCurrencyCode()); 

     $fieldName = $this->getData('name'); 
     foreach ($dataSource['data']['items'] as & $item) { 
      if (isset($item[$fieldName])) { 
       $item[$fieldName] = $currency->toCurrency(sprintf("%f", $item[$fieldName])); 
      } 
     } 
    } 

    return $dataSource; 
} 

我希望这有助于弄清楚如何格式化列中的数据在网格上。

+0

非常感谢! –