【问题标题】:Magento2 dynamic massactionMagento2 动态质量
【发布时间】:2016-06-01 07:12:18
【问题描述】:

我正在研究 Magento2 动态批量处理,但我没有在订单网格中获取动态树批量处理部分。所以我引用了一个链接,我得到了以下解决方案,但我仍然没有得到想要的输出。让我知道我哪里出错了。

<massaction name="listing_massaction">
    <action name="magento_hello">
       <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
               <item name="type" xsi:type="string">magento_hello</item>
                <item name="label" xsi:type="string" translate="true">change to group buyer</item>
            </item>
       </argument>
       <argument name="actions" xsi:type="array">
       <argument name="class" xsi:type="string">Magento\Hello\Ui\Component\MassAction\Group\Options</argument>
          <argument name="data" xsi:type="array">
             <item name="urlPath" xsi:type="string">customertobuyer/masschangetobuyer</item>
             <item name="paramName" xsi:type="string">group</item>
             <item name="confirm" xsi:type="array">
             <item name="title" xsi:type="string" translate="true">change to group buyer</item>
             <item name="message" xsi:type="string" translate="true">Are you sure to change selected  customerto buyer and to assign sto new group buyer?</item>
              </item>
       </argument>
    </argument>
   </action>
</massaction>
<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Hello\Ui\Component\MassAction\Group;

use Magento\Framework\UrlInterface;
use Zend\Stdlib\JsonSerializable;
use Magento\Customer\Model\ResourceModel\Group\CollectionFactory;

/**
 * Class Options
 */
class Options implements JsonSerializable
{
    /**
     * @var array
     */
    protected $options;

    /**
     * @var CollectionFactory
     */
    protected $collectionFactory;

    /**
     * Additional options params
     *
     * @var array
     */
    protected $data;

    /**
     * @var UrlInterface
     */
    protected $urlBuilder;

    /**
     * Base URL for subactions
     *
     * @var string
     */
    protected $urlPath;

    /**
     * Param name for subactions
     *
     * @var string
     */
    protected $paramName;

    /**
     * Additional params for subactions
     *
     * @var array
     */
    protected $additionalData = [];

    /**
     * Constructor
     *
     * @param CollectionFactory $collectionFactory
     * @param UrlInterface $urlBuilder
     * @param array $data
     */
    public function __construct(
        CollectionFactory $collectionFactory,
        UrlInterface $urlBuilder,
        array $data = []
    ) {
        $this->collectionFactory = $collectionFactory;
        $this->data = $data;
        $this->urlBuilder = $urlBuilder;
    }

    /**
     * Get action options
     *
     * @return array
     */
    public function jsonSerialize()
    {
        if ($this->options === null) {
            $options = $this->collectionFactory->create()->setRealGroupsFilter()->toOptionArray();
            $this->prepareData();
            foreach ($options as $optionCode) {
                $this->options[$optionCode['value']] = [
                    'type' => 'customer_group_' . $optionCode['value'],
                    'label' => $optionCode['label'],
                ];

                // if ($this->urlPath && $this->paramName) {
                //     $this->options[$optionCode['value']]['url'] = $this->urlBuilder->getUrl(
                //         $this->urlPath,
                //         [$this->paramName => $optionCode['value']]
                //     );
                // }

                $this->options[$optionCode['value']] = array_merge_recursive(
                    $this->options[$optionCode['value']],
                    $this->additionalData
                );
            }

            $this->options = array_values($this->options);
        }
        return $this->options;
    }

    /**
     * Prepare addition data for subactions
     *
     * @return void
     */
    protected function prepareData()
    {
        foreach ($this->data as $key => $value) {
            switch ($key) {
                case 'urlPath':
                    $this->urlPath = $value;
                    break;
                case 'paramName':
                    $this->paramName = $value;
                    break;
                default:
                    $this->additionalData[$key] = $value;
                    break;
            }
        }
    }
}

【问题讨论】:

    标签: magento2


    【解决方案1】:

    请尝试使用此 xml 并根据需要更新您的控制器操作

    <massaction name="listing_massaction">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="selectProvider"xsi:type="string">vendorName_moduleName_list.vendorName_moduleName_columns.ids</item>
                <item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
                <item name="indexField" xsi:type="string">id</item>
            </item>
        </argument>
        <action name="magento_hello">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                <item name="type" xsi:type="string">magento_hello</item>
                <item name="label" xsi:type="string" translate="true">change to group buyer</item>
            <!-- set you action path-->
                <item name="url" xsi:type="url" path="path_to_controller"/>
                    <item name="confirm" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">change to group buyer</item>
                        <item name="message" xsi:type="string" translate="true">Are you sure to change selected customerto buyer and to assign sto new group buyer?</item>
                    </item>
                </item>
            </argument>
        </action>
    </massaction>
    

    【讨论】:

      【解决方案2】:

      我认为 UI 组件类中的 function jsonSerialize() 存在问题。

      请在下面尝试。

      public function jsonSerialize()
          {
              $i=0;
              if ($this->options === null) {
                  // get the massaction data from the database table
                  $collection = $this->collectionFactory->create()->setRealGroupsFilter()->toOptionArray();
      
                  if(!count($collection)){
                      return $this->options;
                  }
                  //make a array of massaction
                  foreach ($collection as $key => $badge) {
                      $options[$i]['value']=$badge->getEntityId();
                      $options[$i]['label']=$badge->getTitle();
                      $i++;
                  }
                  $this->prepareData();
                  foreach ($options as $optionCode) {
                      $this->options[$optionCode['value']] = [
                          'type' => 'customer_group_' . $optionCode['value'],
                          'label' => $optionCode['label'],
                      ];
      
                      if ($this->urlPath && $this->paramName) {
                          $this->options[$optionCode['value']]['url'] = $this->urlBuilder->getUrl(
                              $this->urlPath,
                              [$this->paramName => $optionCode['value']]
                          );
                      }
      
                      $this->options[$optionCode['value']] = array_merge_recursive(
                          $this->options[$optionCode['value']],
                          $this->additionalData
                      );
                  }
      
                  // return the massaction data
                  $this->options = array_values($this->options);
              }
              return $this->options;
          }
      

      【讨论】:

        猜你喜欢
        • 2012-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-30
        • 2018-07-04
        • 2022-01-10
        • 2021-12-21
        相关资源
        最近更新 更多