3

I've created custom Admin Grid with Default Magento's Customer Collection.

I am sharing my code for this.

Vendor/Module/etc/di.xml

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="credit_grid_listing_data_source" xsi:type="string">Magento\Customer\Model\ResourceModel\Customer\Collection</item>
        </argument>
    </arguments>
</type>
<virtualType name="Magento\Customer\Model\ResourceModel\Customer\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
    <arguments>
        <argument name="mainTable" xsi:type="string">customer_entity</argument>
        <argument name="resourceModel" xsi:type="string">Magento\Customer\Model\ResourceModel\Customer</argument>
    </arguments>
</virtualType>

<preference for="Vendor\Module\Api\Data\GridInterface" type="Vendor\Module\Model\Grid" />

Below is Layout for adminhtml

<body>
    <referenceContainer name="content">
        <uiComponent name="credit_grid_listing"/>
    </referenceContainer>
</body>

../layout/credit_grid_addrow.xml

 <referenceContainer name="content">
       <block class="Vednor\Module\Block\Adminhtml\Grid\AddRow" name="add_row" />
    </referenceContainer>

ui_component/credit_grid_listing.xml

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Ui/etc/ui_configuration.xsd">
<argument name="data" xsi:type="array">
    <item name="js_config" xsi:type="array">
        <item name="provider" xsi:type="string">credit_grid_listing.credit_grid_listing_data_source</item>
        <item name="deps" xsi:type="string">credit_grid_listing.credit_grid_listing_data_source</item>
    </item>
    <item name="spinner" xsi:type="string">grid_records_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" translate="true">Add New Row</item>
            <item name="class" xsi:type="string">primary</item>
            <item name="url" xsi:type="string">*/*/addrow</item>
        </item>
    </item>
</argument>
<dataSource name="credit_grid_listing_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider</argument>
        <argument name="name" xsi:type="string">credit_grid_listing_data_source</argument>
        <argument name="primaryFieldName" xsi:type="string">entity_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>
        </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>
<container name="listing_top">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="template" xsi:type="string">ui/grid/toolbar</item>
            <item name="stickyTmpl" xsi:type="string">ui/grid/sticky/toolbar</item>
        </item>
    </argument>
    <bookmark name="bookmarks"/>
    <columnsControls name="columns_controls"/>
    <filters name="listing_filters">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="columnsProvider" xsi:type="string">credit_grid_listing.credit_grid_listing.grid_records_columns</item>
                <item name="storageConfig" xsi:type="array">
                    <item name="provider" xsi:type="string">credit_grid_listing.credit_grid_listing.listing_top.bookmarks</item>
                    <item name="namespace" xsi:type="string">current.filters</item>
                </item>
                <item name="templates" xsi:type="array">
                    <item name="filters" xsi:type="array">
                        <item name="select" xsi:type="array">
                            <item name="component" xsi:type="string">Magento_Ui/js/form/element/ui-select</item>
                            <item name="template" xsi:type="string">ui/grid/filters/elements/ui-select</item>
                        </item>
                    </item>
                </item>
                <item name="childDefaults" xsi:type="array">
                    <item name="provider" xsi:type="string">credit_grid_listing.credit_grid_listing.listing_top.listing_filters</item>
                    <item name="imports" xsi:type="array">
                        <item name="visible" xsi:type="string">credit_grid_listing.credit_grid_listing.grid_records_columns.${ $.index }:visible</item>
                    </item>
                </item>
            </item>
            <item name="observers" xsi:type="array">
                <item name="column" xsi:type="string">column</item>
            </item>
        </argument>
    </filters>
    <massaction name="listing_massaction">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="selectProvider" xsi:type="string">credit_grid_listing.credit_grid_listing.grid_records_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>
        <!-- Mass actions which you want to add in your grid-->
        <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="grid/grid/massdelete"/>
                    <item name="confirm" xsi:type="array">
                        <item name="title" xsi:type="string" translate="true">Delete</item>
                        <item name="message" xsi:type="string" translate="true">Do you want to delete selected row record?</item>
                    </item>
                </item>
            </argument>
        </action>
    </massaction>
    <paging name="listing_paging">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="storageConfig" xsi:type="array">
                    <item name="provider" xsi:type="string">credit_grid_listing.credit_grid_listing.listing_top.bookmarks</item>
                    <item name="namespace" xsi:type="string">current.paging</item>
                </item>
                <item name="selectProvider" xsi:type="string">credit_grid_listing.credit_grid_listing.grid_records_columns.ids</item>
            </item>
        </argument>
    </paging>
</container>
<columns name="grid_records_columns">
    <selectionsColumn name="ids">
       <argument name="data" xsi:type="array">
           <item name="config" xsi:type="array">
               <item name="indexField" xsi:type="string">entity_id</item>
               <item name="sorting" xsi:type="string">desc</item>
               <item name="sortOrder" xsi:type="number">0</item>
           </item>
       </argument>
   </selectionsColumn>
   <column name="firstname">
       <argument name="data" xsi:type="array">
           <item name="config" xsi:type="array">
               <item name="filter" xsi:type="string">textRange</item>
               <item name="label" xsi:type="string" translate="true">firstname</item>
           </item>
       </argument>
   </column>
   <column name="lastname" >
       <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">lastname</item>
           </item>
       </argument>
   </column>

   <actionsColumn name="actions" class="Vendor\Module\Ui\Component\Listing\Grid\Column\Action">
       <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">id</item>
           </item>
       </argument>
   </actionsColumn>
</columns>

../Model/ResourceModel/Grid.php

protected function _construct()
{
    $this->_init('customer_entity', 'entity_id');
}

../Model/Grid.php

protected function _construct()
{
    $this->_init('Vendor\Module\Model\ResourceModel\Grid');
}

public function getEntityId()
{
    return $this->getData(self::ENTITY_ID);
}


public function setEntityId($entityId)
{
    return $this->setData(self::ENTITY_ID, $entityId);
}


public function getFirstname()
{
    return $this->getData(self::FIRSTNAME);
}


public function setFirstname($firstName)
{
    return $this->setData(self::FIRSTNAME, $firstName);
}


public function getLastname()
{
    return $this->getData(self::LASTNAME);
}


public function setLastname($lastName)
{
    return $this->setData(self::LASTNAME, $lastName);
}

../Controller/Adminhtml/Grid/AddRow.php

public function execute()
{

    $rowId = (int) $this->getRequest()->getParam('id'); 
    $rowData = $this->gridFactory->create();

    if ($rowId) {
        $rowData = $rowData->load($rowId);

        $rowTitle = $rowData->getFirstname().'&nbsp;'.$rowData->getLastname();
        if (!$rowData->getFirstName()) {
            $this->messageManager->addError(__('row data no longer exist.'));
            $this->_redirect('credit/grid/rowdata');
            return;
        }
    }

    $this->coreRegistry->register('row_data', $rowData);
    $resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
    $title = $rowId ? __('Edit Customer ').$rowTitle : __('Add Row Data');
    $resultPage->getConfig()->getTitle()->prepend($title);
    return $resultPage;
}

../Block/Adminhtml/Grid/Edit/Form.php

protected function _prepareForm()
{
    $id = $this->request->getParam('id');
    $dateFormat = $this->_localeDate->getDateFormat(\IntlDateFormatter::SHORT);
    $model = $this->_coreRegistry->registry('row_data');
    //print_r($model->getData()); exit;
    $form = $this->_formFactory->create(
        ['data' => [
                        'id' => 'edit_form',
                        'enctype' => 'multipart/form-data',
                        'action' => $this->getData('action'),
                        'method' => 'post'
                    ]
        ]
    );

    $form->setHtmlIdPrefix('troogrid__');
    if ($model->getFirstName()) {
        $fieldset = $form->addFieldset(
            'base_fieldset',
            ['legend' => __('Edit Customer Social Details'), 'class' => 'fieldset-wide']
        );
        $fieldset->addField('entity_id', 'hidden', ['name' => 'entity_id']);
    } else {
        $fieldset = $form->addFieldset(
            'base_fieldset',
            ['legend' => __('Add Row Data'), 'class' => 'fieldset-wide']
        );
    }

    $fieldset->addField(
        'First Name',
        'text',
        [
            'name' => 'firstname',
            'label' => __('First Name'),
            'value' => 'Data',
            'id' => 'firstname',
            'title' => __('First Name'),
            'class' => 'required-entry',
            'required' => true


        ]
    );

    $fieldset->addField(
        'Last Name',
        'text',
        [
            'name' => 'lastname',
            'label' => __('Last Name'),
            'id' => 'lastname',
            'title' => __('Last Name'),
            'class' => 'required-entry',
            'required' => true,

        ]
    );

    $form->setValues($model->getData());
    $form->setUseContainer(true);
    $this->setForm($form);

    return parent::_prepareForm();
    //return $form;
}

Now, If i am printing model than it will return me all details but when i edit row data isn't filling up with form.

Is there anything i am missing with this ??

Also Model can also able to update data.

but fails to render data.

2

Your Code seems to be correct, But i found one issue with your code that is in your Form.

Change it to below than check it should work. It will Also resolves your problem you described here : Magetno 2 : Get Customer FirstName and LastName with Custom Resource Model

$fieldset->addField(
    'firstname', // Update
    'text',
    [
        'name' => 'firstname',
        'label' => __('First Name'),
        'value' => 'Data',
        'id' => 'firstname',
        'title' => __('First Name'),
        'class' => 'required-entry',
        'required' => true


    ]
);

$fieldset->addField(
    'lastname', // Change Here
    'text',
    [
        'name' => 'lastname',
        'label' => __('Last Name'),
        'id' => 'lastname',
        'title' => __('Last Name'),
        'class' => 'required-entry',
        'required' => true,

    ]
);

Clear Cache and Check.

  • Thanks It worked, I had to switch to php form. :( But now it worked .!! – TBS Mage May 29 '18 at 9:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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