1

I need to render email address for all customer while creating new order from admin. Adding renderer class for other grids is simple, But how to add renderer class when creating column using Magento\Backend\Block\Widget\Grid\Column class.

Below is the code of adding email column to grid (vendor/magento/module-customer/view/adminhtml/ui_component/customer_listing.xml)

<block class="Magento\Backend\Block\Widget\Grid\Column" name="adminhtml.customer.grid.columnSet.email" as="email">
<arguments>
    <argument name="header" xsi:type="string" translate="true">Email</argument>
    <argument name="index" xsi:type="string">email</argument>
</arguments></block>

I have tried adding "renderer" argument but didn't worked.

<argument name="renderer" xsi:type="string">{{render class path }}</argument>

How can we add renderer class here?

2 Answers 2

1

Was able to fix it .

Below is the code.

file : sales_order_create_customer_block.xml

    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
      <referenceBlock name="adminhtml.customer.grid.columnSet.email">
          <arguments>
              <argument name="renderer" xsi:type="string">Vendor\Module\Block\Grid\Renderer\Email</argument>
          </arguments>
      </referenceBlock>
    </body>
</page>

Renderer CLass

namespace Vendor\Module\Block\Grid\Renderer;

class Email extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer
{
    /**
     * Renders grid column
     *
     * @param   \Magento\Framework\DataObject $row
     * @return  string
     */
    public function render(\Magento\Framework\DataObject $row)
    {
        return '<span>'.$row->getEmail().'</span>';
    }
}
1

This worked very well. I built a few classes in Renderer, namely Name, Value and Description, and each class truncates its respective data so that it can fit into a Grid. Here's one of the classes:

namespace Vendor\Module\Block\Grid\Renderer;
/**
 * Truncates description to 50 chars + ... for display in Grid
 */
use Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer;

class Description extends AbstractRenderer 
{
    /**
     * Renders grid column
     *
     * @param   \Magento\Framework\DataObject $row
     * @return  string
     */
    public function render(\Magento\Framework\DataObject $row)
    {
        $str = $row->getDescription();
        $truncated = substr($str, 0, 50);
        if(strlen($str) > strlen($truncated)) {
            $str = $truncated . "...";
        }
        return htmlentities($str, ENT_QUOTES);
    }
}

and in the xml file, I have:

                    <block class="Magento\Backend\Block\Widget\Grid\Column" as="description">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Description</argument>
                            <argument name="renderer" xsi:type="string">Vendor\Module\Block\Grid\Renderer\Description</argument>
                            <argument name="type" xsi:type="string">text</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>

Thanks for posting your solution. It was exactly what I needed.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.