1

I am using magento 2 and I create sequence row number in grid view by add this column in xml

<column name="no" class="Webkul\Marketplace\Ui\Component\Listing\Column\SequenceNumber">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="sortable" xsi:type="boolean">false</item>
                <item name="label" xsi:type="string" translate="true">No</item>
                <item name="sortOrder" xsi:type="number">1</item>
            </item>
        </argument>
    </column>

And create class SequenceNumber

public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        $count = 1;
        foreach ($dataSource['data']['items'] as & $item) {
            $item[$this->getData('name')] = $count;
            $count += 1;
        }
    }
    return $dataSource;
}

This column can display on grid view but when I export to csv the column (no) is empty. Is there any way to fix that issue or how can export the data with sequence row number?

1

The thing is, your SequenceNumber class changes data items only for ui grid rows, when your_component_data_source is loaded, and if you want to get the same results with export, you should provide values for "No" column inside your_component_data_source collection class.

I suppose you have something like this in view/adminhtml/ui_component/your_component.xml:

<dataSource name="your_component_data_source">
    <argument name="dataProvider" xsi:type="configurableObject">
        <argument name="class" xsi:type="string">Vendor\Module\Ui\Component\ModelNameGridDataProvider</argument>
        <argument name="name" xsi:type="string">your_component_data_source</argument>
        <argument name="primaryFieldName" xsi:type="string">id</argument>
        <argument name="requestFieldName" xsi:type="string">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 name="component" xsi:type="string">Magento_Ui/js/grid/provider</item>
                <item name="storageConfig" xsi:type="array">
                    <item name="indexField" xsi:type="string">id</item>
                </item>
            </item>
        </argument>
    </argument>
</dataSource>

...and in your adminhtml/di.xml:

<type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
    <arguments>
        <argument name="collections" xsi:type="array">
            <item name="your_component_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\ModelName\Grid\Collection</item>
        </argument>
    </arguments>
</type>
...
<!--The following can be a virtualType. In this case you should create it as real class to be able to modify collection-->
<type name="Vendor\Module\Model\ResourceModel\ModelName\Grid\Collection"><!---->
    <arguments>
        <argument name="mainTable" xsi:type="string">your_table_name</argument>
        <argument name="resourceModel" xsi:type="string">Vendor\Model\ResourceModel\ModelName</argument>
    </arguments>
</type>

So, everything you need - add to your data_source collection class the following code:

//app/code/Vendor/Module/Model/ResourceModel/ModelName/Grid/Collection.php

namespace Vendor\Module\Model\ResourceModel\ModelName\Grid;

class Collection extends \Magento\Framework\View\Element\UiComponent\DataProvider\SearchResult
{
    protected function _afterLoad()
    {
        parent::_afterLoad();

        $counter = 0;
        $items = $this->getItems();
        foreach ($items as $item) {
            $item['no'] = ++$counter;
        }
        return $this;
    }
}

Then you should remove your Webkul\Marketplace\Ui\Component\Listing\Column\SequenceNumber class from UI component

and do not forget to clear cache (including m2root/var/di, since 2.2.0 m2root/generated/)

Still, you won't be able to sort rows by this column..

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.