1

I have created a custom admin grid using UI Component with created_date filed. the requirement is to show single data per created_date. currently, the grid shows multiple data in created_date.

EX: I have got more than one entry for a single date. I want to show only first entry for that particular date.

<column name="creation_time" 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">Created At</item>
                    <item name="sortOrder" xsi:type="number">140</item>
                </item>
            </argument>
        </column>
0

3 Answers 3

2

You need to apply your custom class(Namespace\Modulename\Ui\Component\Listing\Columns\Date) instead of default magento class (Magento\Ui\Component\Listing\Columns\Date).

Now create above given class file located at :

Namespace\Modulename\Ui\Component\Listing\Columns\Date

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Namespace\Modulename\Ui\Component\Listing\Columns;

use Magento\Framework\Stdlib\BooleanUtils;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

/**
 * @api
 * @since 100.0.2
 */
class Date extends \Magento\Ui\Component\Listing\Columns\Date
{

    /**
     * @inheritdoc
     */
    public function prepareDataSource(array $dataSource)
    {
        //Du stuff your logic
    }
}

UPDATE :

Suppose you need to add filters to your collection and you want to display your grid using ui_component as they allow you to configure the page manipulating the UI components, then follow the steps below.

Now suppose you want to display products whose price is greater than 100.

Add this code in your

Namespace/ModuleName/view/adminhtml/ui_component/example.xml

<dataSource name="test_products_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
    <argument name="class" xsi:type="string">Namespace\ModuleName\Ui\DataProvider\Product\ProductDataProvider</argument>
    <argument name="name" xsi:type="string">test_products_data_source</argument>
    <argument name="primaryFieldName" xsi:type="string">entity_id</argument>
    <argument name="requestFieldName" xsi:type="string">entity_id</argument>
    <argument name="data" xsi:type="array">
    <item name="config" xsi:type="array">
        <item name="update_url" xsi:type="url" path="mui/index/render"/>
    </item>
    </argument>
    </argument>
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
        </item>
    </argument>
</dataSource>

n the code above,

<argument name=”class” xsi:type=”string”>Namespace\ModuleName\Ui\DataProvider\Product\ProductDataProvider</argument>

Now in file ProductDataProvider.php in

Namespace/ModuleName/Ui/DataProvider/Product

namespace Namespace\ModuleName\Ui\DataProvider\Product;

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;

class ProductDataProvider extends \Magento\Ui\DataProvider\AbstractDataProvider
{

    public function __construct(
        CollectionFactory $collectionFactory,
        $name,
        $primaryFieldName,
        $requestFieldName,
        array $meta = [],
        array $data = []
    ) {
        $collection = $collectionFactory->create();
        parent::__construct(
            $name,
            $primaryFieldName,
            $requestFieldName,
            $meta,
            $data
        );
        $this->collection = $collectionFactory->create()
                          ->addAttributeToSelect('*')
                          ->addAttributeToFilter('price', ['gt' => 100]);
    }
}

Note : Above code is not tested. You need to add logic as per your requirement.

I hope it helps!

2
  • nice solution let me try May 1, 2019 at 7:02
  • Your code is also working properly !!! Nice and New Idea !!! May 1, 2019 at 9:23
2

You have to apply below function to your datasource grid collection.

<?php

protected function _initSelect()
{
    parent::_initSelect();

    $tableName = $this->getTable('yout_table_name');

    $this->getSelect()
    ->where("main_table.id IN SELECT min(id) FROM $tableName GROUP BY DATE(created_date)")
    ;
}
4
  • You mean this path? Vendor\Module\Model\ResourceModel\ModuleStatistics\Grid\Collection.php; May 1, 2019 at 8:26
  • 1
    Yes that one is correct ...
    – Yash Shah
    May 1, 2019 at 8:41
  • Is it a good idea to change directly in the grid collection? or find any other way like extend or override. May 1, 2019 at 9:34
  • As this collection is specifically made from grid only, you can change into it. This collection is not being used anywhere else ...
    – Yash Shah
    May 1, 2019 at 10:16
0

I have applies this function to sort date from the collection it's working.

<?php

protected function _initSelect()
{
    parent::_initSelect();

    $tableName = $this->getTable('yout_table_name');

    $this->getSelect()->group('DATE(creation_time)');
    ;
}

Your Answer

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

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