cancel
Showing results for 
Search instead for 
Did you mean: 

custom model mass action

SOLVED

custom model mass action

hello,

I've created my own custom model, and I got the grid working in adminhtml. This is my xml:

<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">hoop_contacts_contacts.hoop_contacts_contacts_data_source</item>
            <item name="deps" xsi:type="string">hoop_contacts_contacts.hoop_contacts_contacts_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">hoop_contacts_contacts_columns</item>
        <item name="buttons" xsi:type="array">
            <item name="add" xsi:type="array">
                <item name="name" xsi:type="string">add</item>
                <item name="label" xsi:type="string">Add New</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="string">*/Contact/new</item>
            </item>
        </item>
    </argument>
    <dataSource name="hoop_contacts_contacts_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Hoop\Contacts\Ui\Component\Listing\DataProviders\Hoop\Contacts\Contacts</argument>
            <argument name="name" xsi:type="string">hoop_contacts_contacts_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">hoop_contacts_contact_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">hoop_contacts_contact_id</item>
                    </item>
                </item>
            </argument>
        </argument>
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
            </item>
        </argument>
    </dataSource>
    <listingToolbar name="listing_top">
        <paging name="listing_paging"/>
    </listingToolbar>
    <columns name="hoop_contacts_contacts_columns">
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">55</item>
                    <item name="indexField" xsi:type="string">hoop_contacts_contact_id</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </selectionsColumn>
        <column name="hoop_contacts_contact_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="label" xsi:type="string" translate="true">ID</item>
                </item>
            </argument>
        </column>
        <actionsColumn name="actions" class="Hoop\Contacts\Ui\Component\Listing\Column\Hoopcontactscontacts\PageActions">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">107</item>
                    <item name="indexField" xsi:type="string">hoop_contacts_contact_id</item>
                    <item name="sortOrder" xsi:type="number">200</item>
                </item>
            </argument>
        </actionsColumn>
        <column name="firstname">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Firstname</item>
                    <item name="sortOrder" xsi:type="number">105</item>
                </item>
            </argument>
        </column>
        <column name="lastname">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Lastname</item>
                    <item name="sortOrder" xsi:type="number">106</item>
                </item>
            </argument>
        </column>
        <column name="email">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Email</item>
                    <item name="sortOrder" xsi:type="number">107</item>
                </item>
            </argument>
        </column>
        <column name="company">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Company</item>
                    <item name="sortOrder" xsi:type="number">108</item>
                </item>
            </argument>
        </column>
        <column name="phone">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Phone</item>
                    <item name="sortOrder" xsi:type="number">109</item>
                </item>
            </argument>
        </column>
        <column name="message">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Message</item>
                    <item name="sortOrder" xsi:type="number">110</item>
                </item>
            </argument>
        </column>
        <column name="newsletter">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Newsletter</item>
                    <item name="sortOrder" xsi:type="number">111</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

now I wanted to add delete mass action, so I am going to add this:

<massaction name="listing_massaction">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">hoop_contacts_contacts.hoop_contacts_contact.blog_post_columns.ids</item>
                    <item name="indexField" xsi:type="string">post_id</item>
                </item>
            </argument>
            <action name="delete">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="type" xsi:type="string">delete</item>
                        <item name="label" xsi:type="string" translate="true">Delete</item>
                        <item name="url" xsi:type="url" path="blog/post/massDelete"/>
                        <item name="confirm" xsi:type="array">
                            <item name="title" xsi:type="string" translate="true">Delete items</item>
                            <item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected items?</item>
                        </item>
                    </item>
                </argument>
            </action>
        </massaction>

inside the listingToolbar section.

But I don't understand how to set the selectProvider item, any help?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Re: custom model mass action

You can just pass your selectProvider like as below,

 

<argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">hoop_contacts_contacts.hoop_contacts_contacts.hoop_contacts_contacts_columns.ids</item>
                    <item name="indexField" xsi:type="string">hoop_contacts_contact_id</item>
                </item>
            </argument>

I have keep indexfiled as below in above code and check,

<item name="indexField" xsi:type="string">hoop_contacts_contact_id</item>

 

if issue solved, Click Kudos/Accept as solutions.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

View solution in original post

4 REPLIES 4

Re: custom model mass action

You can just pass your selectProvider like as below,

 

<argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">hoop_contacts_contacts.hoop_contacts_contacts.hoop_contacts_contacts_columns.ids</item>
                    <item name="indexField" xsi:type="string">hoop_contacts_contact_id</item>
                </item>
            </argument>

I have keep indexfiled as below in above code and check,

<item name="indexField" xsi:type="string">hoop_contacts_contact_id</item>

 

if issue solved, Click Kudos/Accept as solutions.

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: custom model mass action

Hello Rakesh,

thanks a lot for your answer, you're saving my day Smiley Happy

Re: custom model mass action

ok sorry, there must be a little mistake because now I can post the request and get to the controller, but I get this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '' in 'where clause', query was: SELECT COUNT(*) FROM `hoop_contacts_contact` AS `main_table` WHERE (`` IN('2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21'))

so I'm not sending the "id" field correctly which for me is hoop_contacts_contact_id.

this is my full hoop_contacts_contacts.xml:

<?xml version="1.0"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">hoop_contacts_contacts.hoop_contacts_contacts_data_source</item>
            <item name="deps" xsi:type="string">hoop_contacts_contacts.hoop_contacts_contacts_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">hoop_contacts_contacts_columns</item>
        <item name="buttons" xsi:type="array">
            <item name="add" xsi:type="array">
                <item name="name" xsi:type="string">add</item>
                <item name="label" xsi:type="string">Add New</item>
                <item name="class" xsi:type="string">primary</item>
                <item name="url" xsi:type="string">*/Contact/new</item>
            </item>
        </item>
    </argument>
    <dataSource name="hoop_contacts_contacts_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">Hoop\Contacts\Ui\Component\Listing\DataProviders\Hoop\Contacts\Contacts</argument>
            <argument name="name" xsi:type="string">hoop_contacts_contacts_data_source</argument>
            <argument name="primaryFieldName" xsi:type="string">hoop_contacts_contact_id</argument>
            <argument name="requestFieldName" xsi:type="string">id</argument>
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="update_url" xsi:type="url" path="mui/index/render"/>
                    <item name="storageConfig" xsi:type="array">
                        <item name="indexField" xsi:type="string">hoop_contacts_contact_id</item>
                    </item>
                </item>
            </argument>
        </argument>
        <argument name="data" xsi:type="array">
            <item name="js_config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
            </item>
        </argument>
    </dataSource>
    <listingToolbar name="listing_top">
        <paging name="listing_paging"/>
        <massaction name="listing_massaction">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">hoop_contacts_contacts.hoop_contacts_contacts.hoop_contacts_contacts_columns.ids</item>
                    <item name="indexField" xsi:type="string">post_id</item>
                </item>
            </argument>
            <action name="delete">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="type" xsi:type="string">delete</item>
                        <item name="label" xsi:type="string" translate="true">Delete</item>
                        <item name="url" xsi:type="url" path="hoop_contacts_contacts/contact/massDelete"/>
                        <item name="confirm" xsi:type="array">
                            <item name="title" xsi:type="string" translate="true">Delete items</item>
                            <item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected items?</item>
                        </item>
                    </item>
                </argument>
            </action>
        </massaction>
    </listingToolbar>
    <columns name="hoop_contacts_contacts_columns">
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">55</item>
                    <item name="indexField" xsi:type="string">hoop_contacts_contact_id</item>
                    <item name="sortOrder" xsi:type="number">10</item>
                </item>
            </argument>
        </selectionsColumn>
        <column name="hoop_contacts_contact_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="label" xsi:type="string" translate="true">ID</item>
                </item>
            </argument>
        </column>
        <actionsColumn name="actions" class="Hoop\Contacts\Ui\Component\Listing\Column\Hoopcontactscontacts\PageActions">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="resizeEnabled" xsi:type="boolean">false</item>
                    <item name="resizeDefaultWidth" xsi:type="string">107</item>
                    <item name="indexField" xsi:type="string">hoop_contacts_contact_id</item>
                    <item name="sortOrder" xsi:type="number">200</item>
                </item>
            </argument>
        </actionsColumn>
        <column name="firstname">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Firstname</item>
                    <item name="sortOrder" xsi:type="number">105</item>
                </item>
            </argument>
        </column>
        <column name="lastname">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Lastname</item>
                    <item name="sortOrder" xsi:type="number">106</item>
                </item>
            </argument>
        </column>
        <column name="email">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Email</item>
                    <item name="sortOrder" xsi:type="number">107</item>
                </item>
            </argument>
        </column>
        <column name="company">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Company</item>
                    <item name="sortOrder" xsi:type="number">108</item>
                </item>
            </argument>
        </column>
        <column name="phone">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Phone</item>
                    <item name="sortOrder" xsi:type="number">109</item>
                </item>
            </argument>
        </column>
        <column name="message">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Message</item>
                    <item name="sortOrder" xsi:type="number">110</item>
                </item>
            </argument>
        </column>
        <column name="newsletter">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="label" xsi:type="string">Newsletter</item>
                    <item name="sortOrder" xsi:type="number">111</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

any help?

thanks

Re: custom model mass action

I post also my MassDelete.php controller, still did not find the solution:

<?php
namespace Hoop\Contacts\Controller\Adminhtml\Contact;

use Magento\Framework\Controller\ResultFactory;
use Magento\Backend\App\Action\Context;
use Magento\Ui\Component\MassAction\Filter;
use Hoop\Contacts\Model\ResourceModel\Contact\CollectionFactory;

class MassDelete extends \Magento\Backend\App\Action
{
    /**
     * @var Filter
     */
    protected $filter;

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


    /**
     * @param Context $context
     * @param Filter $filter
     * @param CollectionFactory $collectionFactory
     */
    public function __construct(Context $context, Filter $filter, CollectionFactory $collectionFactory)
    {
        parent::__construct($context, $filter);
        $this->filter = $filter;
        $this->collectionFactory = $collectionFactory;
    }
    /**
     * Execute action
     *
     * @return \Magento\Backend\Model\View\Result\Redirect
     * @throws \Magento\Framework\Exception\LocalizedException|\Exception
     */
    public function execute()
    {
        $collection = $this->filter->getCollection($this->collectionFactory->create());
        $collectionSize = $collection->getSize();
        foreach ($collection as $item) {
            $item->delete();
        }

        $this->messageManager->addSuccess(__('A total of %1 record(s) have been deleted.', $collectionSize));

        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        return $resultRedirect->setPath('*/*/');
    }
}

any help would be much appreciated.

Thanks