0

I have create a ui-component grid in magento2 .I need to set the default symbol (if the end date is empty in that field) like -- for End column in grid

<column name="date" class="Magento\Ui\Component\Listing\Columns\Date">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="filter" xsi:type="string">dateRange</item>
                        <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
                        <item name="dataType" xsi:type="string">date</item>
                        <item name="label" xsi:type="string" translate="true">From Date</item>
                    </item>
                </argument>
            </column>

OUTPUT:

enter image description here

EXPECTED OUTPUT:

enter image description here

Thanks

Array ( [post_id] => 5
[publish_time] => 2018-11-21 06:13:24 [from_date] => 2018-11-21 05:30:00 [to_date] => -- [is_active] => 1 )

  • Can you please explain more or add screenshot? – Rohan Hapani Nov 21 '18 at 5:50
  • Is it working for you? – Rohan Hapani Nov 21 '18 at 6:58
  • Not working ... – Mano M Nov 21 '18 at 7:14
  • Please check your field name should be "date" in database. – Rohan Hapani Nov 21 '18 at 7:20
  • I have changed that but not working – Mano M Nov 21 '18 at 7:20
4

You can change the format of the date. Add this line to your code

<item name="dateFormat" xsi:type="string">Y-MM-dd HH:mm:ss</item>

modify the date format according to you . For more reference check this link UI component - display the time to 'Y-m-d H:i:s' format(ISO time format)

  • I need to add symbol ' - - ' if the date is empty in that field – Mano M Nov 21 '18 at 6:25
0

You need to add custom file to add customization :

<column name="date" class="VendorName\ModuleName\Ui\Component\Listing\Column\TestingFile">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="filter" xsi:type="string">dateRange</item>
            <item name="component" xsi:type="string">Magento_Ui/js/grid/columns/date</item>
            <item name="dataType" xsi:type="string">date</item>
            <item name="dateFormat" xsi:type="string">Y-MM-dd HH:mm:ss</item>
            <item name="label" xsi:type="string" translate="true">From Date</item>
        </item>
    </argument>
</column>

Create File at VendorName\ModuleName\Ui\Component\Listing\Column\TestingFile.php

<?php

namespace VendorName\ModuleName\Ui\Component\Listing\Column;

use Magento\Ui\Component\Listing\Columns\Column;

class TestingFile extends Column {

    public function __construct(
        \Magento\Framework\View\Element\UiComponent\ContextInterface $context,
        \Magento\Framework\View\Element\UiComponentFactory $uiComponentFactory,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }
    public function prepareDataSource(array $dataSource) {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as $r => $v) {
                if(strtotime($v['date']) < 0){
                    $dataSource['data']['items'][$r]['date'] = "-----";
                } else{
                    $dataSource['data']['items'][$r]['date'] = date('Y-MM-dd HH:mm:ss', strtotime($v['date']));
                }
            }
        }
        return $dataSource;
    }
}
  • same issue its not working for me – jafar pinjar Jul 4 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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