I have created a form and the data of that form is saving in a custom table. Now I want to create a grid in admin which shows the saved data in that table. Please help
1 Answer
Try below code:
Vendor\Module\etc\di.xml
<virtualType name="Vendor\Module\Model\ResourceModel\Module\Grid\Collection" type="Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult">
<arguments>
<argument name="mainTable" xsi:type="string">custom_table_name</argument>
<argument name="eventPrefix" xsi:type="string">custom_table/argument>
<argument name="eventObject" xsi:type="string">custom_table</argument>
<argument name="resourceModel" xsi:type="string">Vendor\Module\\Model\ResourceModel\Module</argument>
</arguments>
</virtualType>
Vendor\Module\Model\Module.php
class Module extends \Magento\Framework\Model\AbstractModel
{
protected function _construct()
{
$this->_init('Vendor\Module\Model\ResourceModel\Module');
}
public function getDefaultValues()
{
$values = [];
return $values;
}
}
Vendor\Module\Model\ResourceModel\Module.php
class Module extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
protected function _construct()
{
$this->_init('custom_table_name', 'primiary_key');
}
}
Vendor\Module\Model\ResourceModel\Module\Collection.php
class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
{
protected $_idFieldName = 'primary_key';
/**
* Define resource model
*
* @return void
*/
protected function _construct()
{
$this->_init('Vendor\Module\Model\Module', 'Vendor\Module\Model\ResourceModel\Module');
}
}
Vendor\Module\Model\ResourceModel\Module\Grid\Collection.php
class Collection extends \FME\ProductSliders\Model\ResourceModel\ProductSliders\Collection implements \Magento\Framework\Api\Search\SearchResultInterface
{
protected $_aggregations;
public function __construct(
\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
\Psr\Log\LoggerInterface $logger,
\Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
\Magento\Framework\Event\ManagerInterface $eventManager,
$mainTable,
$eventPrefix,
$eventObject,
$resourceModel,
$model = 'Magento\Framework\View\Element\UiComponent\DataProvider\Document',
$connection = null,
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
) {
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
$this->_eventPrefix = $eventPrefix;
$this->_eventObject = $eventObject;
$this->_init($model, $resourceModel);
$this->setMainTable($mainTable);
}
/**
* @return \Magento\Framework\Search\AggregationInterface
*/
public function getAggregations()
{
return $this->_aggregations;
}
/**
* @param \Magento\Framework\Search\AggregationInterface $aggregations
* @return $this
*/
public function setAggregations($aggregations)
{
$this->_aggregations = $aggregations;
}
/**
* Retrieve all ids for collection
* Backward compatibility with EAV collection
*
* @param int $limit
* @param int $offset
* @return array
*/
public function getAllIds($limit = null, $offset = null)
{
return $this->getConnection()->fetchCol($this->_getAllIdsSelect($limit, $offset), $this->_bindParams);
}
/**
* Get search criteria.
*
* @return \Magento\Framework\Api\SearchCriteriaInterface|null
*/
public function getSearchCriteria()
{
return null;
}
/**
* Set search criteria.
*
* @param \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setSearchCriteria(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria = null)
{
return $this;
}
/**
* Get total count.
*
* @return int
*/
public function getTotalCount()
{
return $this->getSize();
}
/**
* Set total count.
*
* @param int $totalCount
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setTotalCount($totalCount)
{
return $this;
}
/**
* Set items list.
*
* @param \Magento\Framework\Api\ExtensibleDataInterface[] $items
* @return $this
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function setItems(array $items = null)
{
return $this;
}
}
-
I have created html form in frontend and its value is saved in a database table. Now i want to take data from that table and save in admin grid Commented Aug 23, 2019 at 8:11
-
check my updated answer hope you got some idea now its upto you whether you create grid using ui_component or Block Commented Aug 23, 2019 at 8:27