2

I want change text value on my grid custom module, in database I just allowed to save 0 or 1 but in grid I want change and show that value from 1/0 to be Yes/No,

enter image description here

here is my grid layout xml code :

 <?xml version="1.0"?>
 <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../../lib/internal/Magento/Framework/View/Layout/etc/page_configuration.xsd">
<body>
 <referenceContainer name="content">
 <block class="KS\Brand\Block\Adminhtml\Items" name="adminhtml.block.promo.quote.grid.container">
 <block class="Magento\Backend\Block\Widget\Grid" name="adminhtml.block.promo.quote.grid" as="grid">
 <arguments>
 <argument name="id" xsi:type="string">ks_brand_items_grid</argument>
 <argument name="dataSource" xsi:type="object">KS\Brand\Model\Resource\Items\Collection</argument>
 <argument name="default_sort" xsi:type="string">sort_order</argument>
 <argument name="default_dir" xsi:type="string">ASC</argument>
 <argument name="save_parameters_in_session" xsi:type="string">1</argument>
 </arguments>
 <block class="Magento\Backend\Block\Widget\Grid\ColumnSet" as="grid.columnSet" name="adminhtml.promo.quote.grid.columnSet">
 <arguments>
 <argument name="rowUrl" xsi:type="array">
 <item name="path" xsi:type="string">ks_brand/*/edit</item>
 <item name="extraParamsTemplate" xsi:type="array">
 <item name="id" xsi:type="string">getId</item>
 </item>
 </argument>
 </arguments>
 <block class="Magento\Backend\Block\Widget\Grid\Column" as="id">
 <arguments>
 <argument name="header" xsi:type="string" translate="true">ID</argument>
 <argument name="index" xsi:type="string">id</argument>
 <argument name="column_css_class" xsi:type="string">col-id</argument>
 <argument name="header_css_class" xsi:type="string">col-id</argument>
 </arguments>
 </block>
 <block class="Magento\Backend\Block\Widget\Grid\Column" as="name">
 <arguments>
 <argument name="header" xsi:type="string" translate="true">Name</argument>
 <argument name="index" xsi:type="string">name</argument>
 </arguments>
 </block>
 <block class="Magento\Backend\Block\Widget\Grid\Column" as="gateway">
 <arguments>
 <argument name="header" xsi:type="string" translate="true">Gateway</argument>
 <argument name="index" xsi:type="string">gateway</argument>
 </arguments>
 </block>
 <block class="Magento\Backend\Block\Widget\Grid\Column" as="is_enable">
 <arguments>
 <argument name="header" xsi:type="string" translate="true">Is Enable</argument>
 <argument name="index" xsi:type="string">is_enable</argument>
 </arguments>
 </block>
 <block class="Magento\Backend\Block\Widget\Grid\Column" as="storefront">
 <arguments>
 <argument name="header" xsi:type="string" translate="true">Storefront</argument>
 <argument name="index" xsi:type="string">storefront</argument>
 </arguments>
 </block>
 </block>
 </block>
 </block>
 </referenceContainer>
</body>
asked May 26, 2017 at 9:12

3 Answers 3

4

Add following line inside your column


<argument name="type" xsi:type="string">options</argument>
<argument name="options" xsi:type="options" model="KS\Brand\Model\Source\Status"/>

Now create KS\Brand\Model\Source\Status.php

namespace KS\Brand\Model\Source;
class Status implements \Magento\Framework\Option\ArrayInterface
{
 /**
 * Change 0,1 to No,Yes.
 *
 * @return array
 */
 public function toOptionArray()
 {
 return [
 ['value' => 0, 'label' => __('No')],
 ['value' => 1, 'label' => __('Yes')]
 ];
 }
}

Clear cache.

Check more detail

jaybong
2112 silver badges10 bronze badges
answered May 26, 2017 at 9:27
5
  • hi @Sohel Rana , I have follow link and I get error : 1 exception(s): Exception #0 (ReflectionException): Class Vendor\Module\Model\source\Status does not exist Commented May 26, 2017 at 10:53
  • In your case 'Vendor\Module' should be 'KS\Brand'. Commented May 26, 2017 at 11:05
  • Check updated answer, I just changed. Commented May 26, 2017 at 11:06
  • sorry for late reply, I got a lot of rest on the last two days....I have tried your edited code and this work, thank you very much. Commented May 29, 2017 at 1:39
  • can you please helped me multi selected value label in magento2 admin grid magento.stackexchange.com/questions/295585/… Commented Nov 14, 2019 at 9:49
2

If you want to render data from database and display in admin grid, then you need to follow these:

i. in Grid.php :

protected function _prepareColumns()
{
 ....
 $this->addColumn(
 'certification_body',
 [
 'header' => __('Certification Body'),
 'index' => 'certification_body',
 'class' => 'certification_body',
 'renderer' => 'Prithweema\CertificateBody\Block\Adminhtml\Certificatebody\Edit\Tab\Renderer\CertificateBody'
 ]
 );
 ....
 ....
}

ii. in your renderer file(Prithweema/CertificateBody/Block/Adminhtml/Certificatebody/Edit/Tab/Renderer/CertificateBody.php) :

<?php
namespace Prithweema\CertificateBody\Block\Adminhtml\Certificatebody\Edit\Tab\Renderer;
use Magento\Framework\DataObject;
class CertificateBody extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{
 public function render(DataObject $row)
 {
 $value = $row->getData($this->getColumn()->getIndex());
 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $body = $objectManager->get('Prithweema\CertificateBody\Model\Certificatebody')->load($value);
 return $body->getCertificateBody();
 }
}
answered Jul 12, 2017 at 7:03
2
  • 1
    I think use objectManager is not good idea, but I feel this will be useful for reference. I will vote up. thanks :) Commented Jan 16, 2018 at 0:51
  • Do not use object Manager(Magneto 2 discourage its usage), use Repositories or factories instead. Commented Sep 10, 2020 at 7:35
0
<?php
namespace Vendor\CustomReview\Block\Adminhtml\Customer\Grid\Renderer;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Framework\DataObject;
use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;
use Magento\Store\Model\StoreManagerInterface;
class CustomerEmails extends AbstractRenderer
{
 /**
 * @var CustomerRepositoryInterface
 */
 private $customerRepository;
 /**
 * @var StoreManagerInterface
 */
 private $storeManager;
 /**
 * @param StoreManagerInterface $storeManager
 * @param CustomerRepositoryInterface $customerRepository
 */
 public function __construct(StoreManagerInterface $storeManager, CustomerRepositoryInterface $customerRepository)
 {
 $this->customerRepository = $customerRepository;
 $this->storeManager = $storeManager;
 }
 public function render(DataObject $row)
 {
 $customerId = $row->getData();
 print_r($customerId);
// die();
 $customerEmail = '';
 if($customerId['customer_id']){
 $customer = $this->customerRepository->getById($customerId['customer_id']);
 $customerEmail = $customer->getEmail();
 }
 return $customerEmail;
 }
}
review_listing.xml
<?xml version="1.0" encoding="utf-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
 <columns name="review_columns" class="Magento\Ui\Component\Listing\Columns">
 <column name="customer_id" sortOrder="90">
 <settings>
 <filter>text</filter>
 <label translate="true">Customer Emails</label>
 </settings>
 </column>
 </columns>
</listing>

\Vendor\CustomReviews\Block\Adminhtml\Customer\Grid\Renderer\CustomerEmails

<?php
declare(strict_types=1);
namespace Vendor\CustomReview\Block\Adminhtml;
class Grid extends \Magento\Review\Block\Adminhtml\Grid
{
 public function __construct(
 \Magento\Backend\Block\Template\Context $context,
 \Magento\Backend\Helper\Data $backendHelper,
 \Magento\Review\Model\ReviewFactory $reviewFactory,
 \Magento\Review\Model\ResourceModel\Review\Product\CollectionFactory $productsFactory,
 \Magento\Review\Helper\Data $reviewData,
 \Magento\Review\Helper\Action\Pager $reviewActionPager,
 \Magento\Framework\Registry $coreRegistry, array $data = []
 ) {
 parent::__construct(
 $context,
 $backendHelper,
 $reviewFactory,
 $productsFactory,
 $reviewData,
 $reviewActionPager,
 $coreRegistry,
 $data);
 }
 /**
 * Prepare grid columns
 *
 * @return \Magento\Backend\Block\Widget\Grid
 * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
 */
 protected function _prepareColumns()
 {
 $this->addColumn(
 'review_id',
 [
 'header' => __('ID'),
 'filter_index' => 'rt.review_id',
 'index' => 'review_id',
 'header_css_class' => 'col-id',
 'column_css_class' => 'col-id'
 ]
 );
 $this->addColumn(
 'created_at',
 [
 'header' => __('Created'),
 'type' => 'datetime',
 'filter_index' => 'rt.created_at',
 'index' => 'review_created_at',
 'header_css_class' => 'col-date col-date-min-width',
 'column_css_class' => 'col-date'
 ]
 );
 if (!$this->_coreRegistry->registry('usePendingFilter')) {
 $this->addColumn(
 'status',
 [
 'header' => __('Status'),
 'type' => 'options',
 'options' => $this->_reviewData->getReviewStatuses(),
 'filter_index' => 'rt.status_id',
 'index' => 'status_id'
 ]
 );
 }
 $this->addColumn(
 'title',
 [
 'header' => __('Title'),
 'filter_index' => 'rdt.title',
 'index' => 'title',
 'type' => 'text',
 'truncate' => 50,
 'escape' => true
 ]
 );
 $this->addColumn(
 'nickname',
 [
 'header' => __('Nickname'),
 'filter_index' => 'rdt.nickname',
 'index' => 'nickname',
 'type' => 'text',
 'truncate' => 50,
 'escape' => true,
 'header_css_class' => 'col-name',
 'column_css_class' => 'col-name'
 ]
 );
 $this->addColumn(
 'detail',
 [
 'header' => __('Review'),
 'index' => 'detail',
 'filter_index' => 'rdt.detail',
 'type' => 'text',
 'truncate' => 50,
 'nl2br' => true,
 'escape' => true
 ]
 );
 /**
 * Check is single store mode
 */
 if (!$this->_storeManager->isSingleStoreMode()) {
 $this->addColumn(
 'visible_in',
 [
 'header' => __('Visibility'),
 'index' => 'stores',
 'type' => 'store',
 'store_view' => true,
 'sortable' => false
 ]
 );
 }
 $this->addColumn(
 'type',
 [
 'header' => __('Type'),
 'type' => 'select',
 'index' => 'type',
 'filter' => \Magento\Review\Block\Adminhtml\Grid\Filter\Type::class,
 'renderer' => \Magento\Review\Block\Adminhtml\Grid\Renderer\Type::class
 ]
 );
 $this->addColumn(
 'name',
 ['header' => __('Product'), 'type' => 'text', 'index' => 'name', 'escape' => true]
 );
 $this->addColumn(
 'sku',
 [
 'header' => __('SKU'),
 'type' => 'text',
 'index' => 'sku',
 'escape' => true
 ]
 );
 $this->addColumn(
 'action',
 [
 'header' => __('Action'),
 'type' => 'action',
 'getter' => 'getReviewId',
 'actions' => [
 [
 'caption' => __('Edit'),
 'url' => [
 'base' => 'review/product/edit',
 'params' => [
 'productId' => $this->getProductId(),
 'customerId' => $this->getCustomerId(),
 'ret' => $this->_coreRegistry->registry('usePendingFilter') ? 'pending' : null,
 ],
 ],
 'field' => 'id',
 ],
 ],
 'filter' => false,
 'sortable' => false
 ]
 );
 $this->addColumn(
 'customer_id',
 [
 'header' => __('Customer Emails'),
 'type' => 'text',
 'index' => 'customer_id',
 'renderer' => 'Vendor\CustomReview\Block\Adminhtml\Customer\Grid\Renderer\CustomerEmails',
 'escape' => true
 ]
 );
 $block = $this->getLayout()->getBlock('grid.bottom.links');
 if ($block) {
 $this->setChild('grid.bottom.links', $block);
 }
 $this->addExportType('*/*/exportCsv', __('CSV'));
 return $this;
 }
}
answered May 27, 2022 at 12:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.