Magento 2.1:如何将自定义行添加到管理员自定义网格


8

我在admin中为“卖方委员会”创建了一个自定义网格。现在,我想显示所赚取的佣金总额,因此需要在表格末尾添加一行,其内容应为“总金额”。请帮忙。

Answers:


2

您可以使用渲染器显示自定义值或计算和显示佣金值。

例如在您的uicomponent网格xml中

<column name="commission" class="Vender\Modulename\Ui\Component\Listing\Column\Commission">
    <argument name="data" xsi:type="array">        
        <item name="config" xsi:type="array">
            <item name="filter" xsi:type="string">text</item>
            <item name="sorting" xsi:type="string">asc</item>
            <item name="label" translate="true" xsi:type="string">Commission</item>
        </item>
    </argument>
</column>

和您的渲染器文件

<?php

namespace Vender\Modulename\Ui\Component\Listing\Column;

class Commission extends \Magento\Ui\Component\Listing\Columns\Column {

    /**
     * @var \Magento\Directory\Api\CountryInformationAcquirerInterface
     */
    protected $countryInformation;

     /**
     * @param \Magento\Framework\View\Element\UiComponent\ContextInterface $context
     * @param \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory
     * @param array $components
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
        array $components = [],
        array $data = []
    ){
        $this->countryInformation = $countryInformation;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    /**
     * 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) {
                //do your stuff and set value of commission in here
            }
        }

        return $dataSource;
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.