PHP code example of mage / grid

1. Go to this page and download the library: Download mage/grid library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mage / grid example snippets



namespace Your\Module\Controller\Adminhtml\YourGrid;

use Magento\Backend\App\Action;
use Magento\Framework\View\Result\PageFactory;

class Index extends Action
{
    protected $resultPageFactory;

    public function __construct(
        Action\Context $context,
        PageFactory $resultPageFactory
    ) {
        parent::__construct($context);
        $this->resultPageFactory = $resultPageFactory;
    }

    protected function _isAllowed()
    {
        return $this->_authorization->isAllowed('Your_Module::your_grid');
    }

    public function execute()
    {
        $resultPage = $this->resultPageFactory->create();
        $resultPage->setActiveMenu('Your_Module::your_grid');
        $resultPage->getConfig()->getTitle()->prepend(__('Your Grid Title'));
        return $resultPage;
    }
}


namespace Your\Module\ViewModel;

use Mage\Grid\ViewModel\GenericViewModelGrid;

class YourCustomViewModel extends GenericViewModelGrid
{
    // Add your custom methods here
}


// In your layout file
<block class="Mage\Grid\Block\GenericGrid" name="your_grid">
    <arguments>
        <argument name="fields" xsi:type="array">
            <item name="id" xsi:type="string">ID</item>
            <item name="name" xsi:type="string">Name</item>
            <item name="status" xsi:type="string">Status</item>
        </argument>
    </arguments>
</block>

namespace Your\Module\Model\DataProcessor;

use Mage\Grid\Api\DataProcessorInterface;

class CustomProcessor implements DataProcessorInterface
{
    public function process($field, $value, $row)
    {
        // Your custom processing logic
        return $processedValue;
    }
}

   <?= $block->getAditionalHTML() 

namespace Your\Module\Model\Source;

use Mage\Grid\Model\Fields\DataSourceInterface;
use Magento\Framework\App\ResourceConnection;

class CustomSource implements DataSourceInterface
{
    protected $resource;
    
    public function __construct(ResourceConnection $resource)
    {
        $this->resource = $resource;
    }

    public function getValues($field)
    {
        // Your custom logic to fetch values
        return ['value1', 'value2', 'value3'];
    }
}


// Get grid data
$fields = array_keys($block->getFieldsNames());
$fieldsFull = $block->getFields();
$jsonGridData = $block->getGridJsonData();
$fieldsConfig = $block->getFieldsConfig();
$tableName = $block->getTableName();
$fieldsNames = $block->getFieldsNames();
$filters = $this->getRequest()->getParam('filter', []);
$processedFields = $block->getProcessedFields($fields, $fieldsConfig, $filters);


$fields = array_keys($block->getFieldsNames());
$fieldsFull = $block->getFields();
$data = $block->getViewModel()->getGridData();
$fieldsConfig = $block->getFieldsConfig();
$fieldsNames = $block->getFieldsNames();
$filters = $this->getRequest()->getParam('filter', []);
$processedFields = $block->getProcessedFields($fields, $fieldsConfig, $filters);

<!-- view/adminhtml/templates/grid/ag-grid-component.phtml -->
<div id="grid-wrapper">
    <?= $block->getFiltersHtml($filterData) 

// Using PHP
$client = new \Magento\Framework\HTTP\Client\Curl();
$client->addHeader('Authorization', 'Bearer ' . $token);
$client->get('https://your-store.com/rest/V1/grid/orders/data?page=1&pageSize=20');

// Using JavaScript
fetch('/rest/V1/grid/orders/data?page=1&pageSize=20', {
    headers: {
        'Authorization': 'Bearer ' + token
    }
})
.then(response => response.json())
.then(data => console.log(data));