previous tutorial, you learned how to save form data to the custom table. Continuing with the same, we will This tutorial demonstrates how to display table data in Magento 2. As we have already learned how to save data to the table, let us understand how to display the saved data. All the data which you have stored can be displayed on the frontend. To do that, we need to create action so that we can display data on the frontend. The given path is an example: http://example.com/extension/index/showdata
Programmatic Method to Display Table Data in Magento 2
1. Create a controller Showdata.php at app\code\Cozmot\Extension\Controller\Index\
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?php
namespace Cozmot\Extension\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\Action\Action;
class Showdata extends Action
{
protected $resultPageFactory;
public function __construct(Context $context, PageFactory $resultPageFactory)
{
parent::__construct($context);
$this->resultPageFactory = $resultPageFactory;
}
public function execute()
{
return $this->resultPageFactory->create();
}
}
|
1
2
3
4
5
6
7
8
9
10
|
<?xml version=“1.0”?>
<page layout=“1column” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:noNamespaceSchemaLocation=“urn:magento:framework:View/Layout/etc/page_configuration.xsd”>
<body>
<referenceContainer name=“content”>
<block class=“Cozmot\Extension\Block\Showdata” name=“showdata”
template=“Cozmot_Extension::showdata.phtml” cacheable=“false”/>
</referenceContainer>
</body>
</page>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<?php
namespace Cozmot\Extension\Block;
use Magento\Framework\View\Element\Template;
use Magento\Backend\Block\Template\Context;
use Cozmot\Extension\Model\ResourceModel\Extension\CollectionFactory;
class Showdata extends Template
{
public $collection;
public function __construct(Context $context, CollectionFactory $collectionFactory, array $data = [])
{
$this->collection = $collectionFactory;
parent::__construct($context, $data);
}
public function getCollection()
{
return $this->collection->create();
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
<?php
$collection = $block->getCollection();
if ($collection->count()) {
?>
<div class=“table-wrapper orders-history”>
<table class=“data table table-order-items history” id=“my-orders-table”>
<caption class=“table-caption”><?php echo __(‘Grid Record’) ?></caption>
<thead>
<tr>
<th scope=“col” class=“col id”><?php echo __(‘ID’) ?></th>
<th scope=“col” class=“col name”><?php echo __(‘Name’) ?></th>
<th scope=“col” class=“col email”><?php echo __(‘Email’) ?></th>
<th scope=“col” class=“col telephone”><?php echo __(‘Telephone’) ?></th>
<th scope=“col” class=“col createat”><?php echo __(‘Created At’) ?></th>
</tr>
</thead>
<tbody>
<?php
foreach ($collection as $item): ?>
<tr>
<td data–th=“<?= $block->escapeHtml(__(‘ID’)) ?>“ class=“col id”>
<?php echo $item->getId() ?>
</td>
<td data–th=“<?= $block->escapeHtml(__(‘Name’)) ?>“ class=“col name”>
<?php echo $item->getName() ?>
</td>
<td data–th=“<?= $block->escapeHtml(__(‘Email’)) ?>“ class=“col email”>
<?php echo $item->getEmail() ?>
</td>
<td data–th=“<?= $block->escapeHtml(__(‘Telephone’)) ?>“ class=“col telephone”>
<?php echo $item->getTelephone() ?>
</td>
<td data–th=“<?= $block->escapeHtml(__(‘Created At’)) ?>“
class=“col title”><?php echo date(‘Y-m-d’, strtotime($item->getCreatedAt())); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
}
?>
|
