0

I have already create admin grid in which there is one column status i want to add two functionality Approve and Disapprove which are store in database how can i do that?

1
  • It would help if you could post your code
    – coderGeek
    Jul 5, 2019 at 13:18

3 Answers 3

1

You can add your status field in InstallSchema.php file

        ->addColumn(
            'status',
            \Magento\Framework\DB\Ddl\Table::TYPE_BOOLEAN,
            '',
            ['nullable' => true],
            '0 = Approved, 1= Disapproved'
        )`

You can add status column in listing.xml

    <column name="status">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">Vendor\Module\Model\Config\Source\Test\Status</item>
            <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">select</item>
                <item name="label" xsi:type="string" translate="true">Status</item>
                <item name="dataType" xsi:type="string">select</item>
            </item>
            <item name="sortOrder" xsi:type="number">6</item>
        </argument>
    </column>

You can add status file here Vendor\Module\Model\Config\Source\Test\Status

<?php
namespace Vendor\Module\Model\Config\Source\Test;

use Magento\Framework\Option\ArrayInterface;

class Status implements ArrayInterface
{
    const STATUS_APPROVE  = 0;
    const STATUS_DISAPPROVE = 1;

    /**
     * @return array
     */
    public function toOptionArray()
    {
        $options = [];
        foreach ($this->toArray() as $value => $label) {
            $options[] = [
                'value' => $value,
                'label' => $label
            ];
        }

        return $options;
    }

    /**
     * Get options in "key-value" format
     *
     * @return array
     */
    public function toArray()
    {
        return [
            self::STATUS_APPROVE => __('Approved'),
            self::STATUS_DISAPPROVE  => __('Disapproved'),
        ];
    }
}
1

you can create mass action to approve or disapprove status in admin grid

0

I think you are asking how to create a mass action with yes no style option.

I literally just coded this about 30 mins ago

So in my example I've added a column to sales admin grid with yes no mass action and a controller to catch yes no value

https://github.com/DominicWatts/AutoShipment/blob/master/view/adminhtml/ui_component/sales_order_grid.xml#L20-L47

    <listingToolbar name="listing_top">
        <massaction name="listing_massaction">
            <action name="status">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="type" xsi:type="string">status</item>
                        <item name="label" xsi:type="string" translate="true">Prevent Auto Ship</item>
                    </item>
                </argument>
                <argument name="actions" xsi:type="array">
                    <item name="0" xsi:type="array">
                        <item name="type" xsi:type="string">enable</item>
                        <item name="label" xsi:type="string" translate="true">Yes</item>
                        <item name="url" xsi:type="url" path="autoshipment/index/massPrevent">
                            <param name="status">1</param>
                        </item>
                    </item>
                    <item name="1" xsi:type="array">
                        <item name="type" xsi:type="string">disable</item>
                        <item name="label" xsi:type="string" translate="true">No</item>
                        <item name="url" xsi:type="url" path="autoshipment/index/massPrevent">
                            <param name="status">2</param>
                        </item>
                    </item>
                </argument>
            </action>
        </massaction>
    </listingToolbar>

controller logic

https://github.com/DominicWatts/AutoShipment/blob/master/Controller/Adminhtml/Index/MassPrevent.php#L33-L66

/**
     * Execute mass action
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        $ids = $this->getRequest()->getPost('selected');
        $status = $this->getRequest()->getParam('status');
        if ($ids && $status) {
            $collection = $this->orderCollectionFactory->create()
                ->addAttributeToSelect('*')
                ->addFieldToFilter('entity_id', ['in' => $ids]);
            $collectionSize = $collection->getSize();
            $updatedItems = 0;
            foreach ($collection as $item) {
                try {
                    $item->setPreventAutomaticShipment($status);
                    $item->save();
                    $updatedItems++;
                } catch (\Exception $e) {
                    $this->messageManager->addErrorMessage($e->getMessage());
                }
            }
            if ($updatedItems != 0) {
                if ($collectionSize != $updatedItems) {
                    $this->messageManager->addErrorMessage(
                        __('Failed to update %1 order(s).', $collectionSize - $updatedItems)
                    );
                }
                $this->messageManager->addSuccessMessage(
                    __('A total of %1 order(s) have been updated.', $updatedItems)
                );
            }
        }
        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
        return $resultRedirect->setPath('sales/order/index');
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.