{"id":1337,"date":"2020-11-05T07:05:55","date_gmt":"2020-11-05T07:05:55","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/11\/05\/display-table-data-magento-2\/"},"modified":"2025-05-21T17:45:51","modified_gmt":"2025-05-21T12:15:51","slug":"display-table-data-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/display-table-data-magento-2\/","title":{"rendered":"How to Display Table Data in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In the previous tutorial, you learned&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/save-form-data-to-custom-table-in-magento-2\/\">how to save form data to the custom table<\/a>. Continuing with the same, we will now move further.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This tutorial demonstrates how to&nbsp;<em><strong>display table data in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As we have already learned how to save data to the table, let us understand&nbsp;<strong>how to display the saved data<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All the data which you have stored can be displayed on the frontend.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To do that, we need to create action so that we can display data on the frontend. The given path is an example:&nbsp;<strong><em>http:\/\/example.com\/extension\/index\/showdata&nbsp;<\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Programmatic Method to Display Table Data in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create a controller&nbsp;<strong>Showdata.php&nbsp;<\/strong>at&nbsp;<strong><em><strong><em>app\\code\\Meetanshi\\Extension\\Controller\\Index\\<\/em><\/strong><\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Meetanshi\\Extension\\Controller\\Index;\n\nuse Magento\\Framework\\App\\Action\\Context;\nuse Magento\\Framework\\View\\Result\\PageFactory;\nuse Magento\\Framework\\App\\Action\\Action;\n\nclass Showdata extends Action\n{\n    protected $resultPageFactory;\n\n    public function __construct(Context $context, PageFactory $resultPageFactory)\n    {\n        parent::__construct($context);\n        $this->resultPageFactory = $resultPageFactory;\n    }\n\n    public function execute()\n    {\n        return $this->resultPageFactory->create();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>extension_index_showdata.xml<\/strong>&nbsp;at&nbsp;<strong><em><strong><em>app\\code\\Meetanshi\\Extension\\view\\frontend\\layout\\<\/em><\/strong><\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;page layout=\"1column\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n      xsi:noNamespaceSchemaLocation=\"urn:magento:framework:View\/Layout\/etc\/page_configuration.xsd\">\n    &lt;body>\n        &lt;referenceContainer name=\"content\">\n            &lt;block class=\"Meetanshi\\Extension\\Block\\Showdata\" name=\"showdata\"\n                   template=\"Meetanshi_Extension::showdata.phtml\" cacheable=\"false\"\/>\n        &lt;\/referenceContainer>\n    &lt;\/body>\n&lt;\/page><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<strong>Showdata.php<\/strong>&nbsp;at&nbsp;<strong><em><strong><em>app\\code\\Meetanshi\\Extension\\Block\\<\/em><\/strong><\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Meetanshi\\Extension\\Block;\n\nuse Magento\\Framework\\View\\Element\\Template;\nuse Magento\\Backend\\Block\\Template\\Context;\nuse Meetanshi\\Extension\\Model\\ResourceModel\\Extension\\CollectionFactory;\n\nclass Showdata extends Template\n{\n\n    public $collection;\n\n    public function __construct(Context $context, CollectionFactory $collectionFactory, array $data = [])\n    {\n        $this->collection = $collectionFactory;\n        parent::__construct($context, $data);\n    }\n\n    public function getCollection()\n    {\n        return $this->collection->create();\n    }\n\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create&nbsp;<strong>showdata.phtml<\/strong>&nbsp;file at&nbsp;<strong><em><strong><em>app\\code\\Meetanshi\\Extension\\view\\frontend\\templates\\<\/em><\/strong><\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n    $collection = $block->getCollection();\nif ($collection->count()) {\n?>\n&lt;div class=\"table-wrapper orders-history\">\n    &lt;table class=\"data table table-order-items history\" id=\"my-orders-table\">\n        &lt;caption class=\"table-caption\">&lt;?php echo __('Grid Record') ?>&lt;\/caption>\n        &lt;thead>\n        &lt;tr>\n            &lt;th scope=\"col\" class=\"col id\">&lt;?php echo __('ID') ?>&lt;\/th>\n            &lt;th scope=\"col\" class=\"col name\">&lt;?php echo __('Name') ?>&lt;\/th>\n            &lt;th scope=\"col\" class=\"col email\">&lt;?php echo __('Email') ?>&lt;\/th>\n            &lt;th scope=\"col\" class=\"col telephone\">&lt;?php echo __('Telephone') ?>&lt;\/th>\n            &lt;th scope=\"col\" class=\"col createat\">&lt;?php echo __('Created At') ?>&lt;\/th>\n        &lt;\/tr>\n        &lt;\/thead>\n        &lt;tbody>\n        &lt;?php\n                foreach ($collection as $item): ?>\n        &lt;tr>\n            &lt;td data-th=\"&lt;?= $block->escapeHtml(__('ID')) ?>\" class=\"col id\">\n                &lt;?php echo $item->getId() ?>\n            &lt;\/td>\n            &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Name')) ?>\" class=\"col name\">\n                &lt;?php echo $item->getName() ?>\n            &lt;\/td>\n            &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Email')) ?>\" class=\"col email\">\n                &lt;?php echo $item->getEmail() ?>\n            &lt;\/td>\n            &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Telephone')) ?>\" class=\"col telephone\">\n                &lt;?php echo $item->getTelephone() ?>\n            &lt;\/td>\n            &lt;td data-th=\"&lt;?= $block->escapeHtml(__('Created At')) ?>\"\n                class=\"col title\">&lt;?php echo date('Y-m-d', strtotime($item->getCreatedAt())); ?>&lt;\/td>\n        &lt;\/tr>\n        &lt;?php endforeach; ?>\n        &lt;\/tbody>\n    &lt;\/table>\n&lt;\/div>\n&lt;?php\n    }\n    ?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After following these four steps, you will be able to display table data.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/10\/How-to-Display-Table-Data-in-Magento-2.jpg\" alt=\"How to Display Table Data in Magento 2\" class=\"wp-image-11226\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Done!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Consider sharing this post novice Magento developer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/meetanshi.com\/blog\/save-form-data-to-custom-table-in-magento-2\/\">How to Save Form Data to Custom Table in Magento 2<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/meetanshi.com\/blog\/add-delete-action-column-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Add Delete Action Column in Magento 2<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In the previous tutorial, you learned&nbsp;how to save form data to the custom table. Continuing with the same, we will now move further. This tutorial&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1337","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1337","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1337"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1337\/revisions"}],"predecessor-version":[{"id":13942,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1337\/revisions\/13942"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1337"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1337"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1337"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}