Question

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>
Was it helpful?

Solution 3

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)');
    ;
}

OTHER TIPS

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)")
    ;
}

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top