0

I am trying to show all my attachments in my grid but the grid is showing only the last one that I uploaded, but in the database and on the frontend it shows all and when i do filter it show all without filter its only show last one. can anyone please help me?

Thanks in Advance

this is my XML

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <argument name="data" xsi:type="array">
        <item name="js_config" xsi:type="array">
            <item name="provider" xsi:type="string">tekfile_file_listing.tekfile_file_listing_data_source</item>
            <item name="deps" xsi:type="string">tekfile_file_listing.tekfile_file_listing_data_source</item>
        </item>
        <item name="spinner" xsi:type="string">tekfile_file_columns</item>
    </argument>
    <dataSource name="tekfile_file_listing_data_source">
        <argument name="dataProvider" xsi:type="configurableObject">
            <argument name="class" xsi:type="string">AmfileFileGridDataProvider</argument>
            <argument name="name" xsi:type="string">tekfile_file_listing_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>
            </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>
    <container name="listing_top">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="template" xsi:type="string">ui/grid/toolbar</item>
            </item>
        </argument>
        <bookmark name="bookmarks">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="storageConfig" xsi:type="array">
                        <item name="namespace" xsi:type="string">tekfile_file_listing</item>
                    </item>
                </item>
            </argument>
        </bookmark>
        <component name="columns_controls">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="columnsData" xsi:type="array">
                        <item name="provider" xsi:type="string">tekfile_file_listing.tekfile_file_listing.tekfile_file_columns</item>
                    </item>
                    <item name="component" xsi:type="string">Magento_Ui/js/grid/controls/columns</item>
                    <item name="displayArea" xsi:type="string">dataGridActions</item>
                </item>
            </argument>
        </component>
        <filters name="listing_filters">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="columnsProvider" xsi:type="string">tekfile_file_listing.tekfile_file_listing.tekfile_file_columns</item>
                    <item name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">tekfile_file_listing.tekfile_file_listing.listing_top.bookmarks</item>
                        <item name="namespace" xsi:type="string">current.filters</item>
                    </item>
                    <item name="templates" xsi:type="array">
                        <item name="filters" xsi:type="array">
                            <item name="select" xsi:type="array">
                                <item name="component" xsi:type="string">Magento_Ui/js/form/element/ui-select</item>
                                <item name="template" xsi:type="string">ui/grid/filters/elements/ui-select</item>
                            </item>
                        </item>
                    </item>
                    <item name="childDefaults" xsi:type="array">
                        <item name="provider" xsi:type="string">tekfile_file_listing.tekfile_file_listing.listing_top.listing_filters</item>
                    </item>
                </item>
            </argument>
        </filters>
        <massaction name="listing_massaction">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="selectProvider" xsi:type="string">tekfile_file_listing.tekfile_file_listing.tekfile_file_columns.ids</item>
                  
                    <item name="indexField" xsi:type="string">history_id</item>
                </item>
            </argument>
            <action name="delete">
                <argument name="data" xsi:type="array">
                    <item name="config" xsi:type="array">
                        <item name="confirm" xsi:type="array">
                            <item name="title" xsi:type="string" translate="true">Delete items</item>
                            <item name="message" xsi:type="string" translate="true">Delete selected items?</item>
                        </item>
                        <item name="type" xsi:type="string">delete</item>
                        <item name="label" xsi:type="string" translate="true">Delete</item>
                        <item name="url" xsi:type="url" path="tekfile/file/massDelete"/>
                    </item>
                </argument>
            </action>
        </massaction>
        <paging name="listing_paging">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">tekfile_file_listing.tekfile_file_listing.listing_top.bookmarks</item>
                        <item name="namespace" xsi:type="string">current.paging</item>
                    </item>
                    <item name="selectProvider" xsi:type="string">tekfile_file_listing.tekfile_file_listing.tekfile_file_columns.ids</item>
                </item>
            </argument>
        </paging>
    </container>
    <columns name="tekfile_file_columns">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="storageConfig" xsi:type="array">
                    <item name="provider" xsi:type="string">tekfile_file_listing.tekfile_file_listing.listing_top.bookmarks</item>
                    <item name="namespace" xsi:type="string">current</item>
                </item>
                <item name="childDefaults" xsi:type="array">
                    <item name="storageConfig" xsi:type="array">
                        <item name="provider" xsi:type="string">tekfile_file_listing.tekfile_file_listing.listing_top.bookmarks</item>
                        <item name="root" xsi:type="string">columns.${ $.index }</item>
                        <item name="namespace" xsi:type="string">current.${ $.storageConfig.root }</item>
                    </item>
                </item>
            </item>
        </argument>
        <selectionsColumn name="ids">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="indexField" xsi:type="string">id</item>
                    <item name="sortOrder" xsi:type="number">0</item>
                </item>
            </argument>
        </selectionsColumn>
        <column name="id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="sorting" xsi:type="string">asc</item>
                    <item name="label" xsi:type="string" translate="true">ID</item>
                </item>
            </argument>
        </column>
        <column name="file_name" >
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">File Name</item>
                </item>
            </argument>
        </column>
        <column name="product_id">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">textRange</item>
                    <item name="label" xsi:type="string" translate="true">Product Id</item>
                </item>
            </argument>
        </column>
        <column name="sku">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">Product SKU</item>
                </item>
            </argument>
        </column>
        <column name="file_size" class="Tekglide\ProductAttachment\Ui\Component\Listing\Column\FileSize">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="filter" xsi:type="string">text</item>
                    <item name="label" xsi:type="string" translate="true">File Size</item>
                </item>
            </argument>
        </column>
        <actionsColumn name="actions" class="Tekglide\ProductAttachment\Ui\Component\Listing\Column\DownloadFile">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                    <item name="indexField" xsi:type="string">id</item>
                    <item name="url" xsi:type="string">tekfile/file/download</item>
                    <item name="urlEntityParamName" xsi:type="string">id</item>
                </item>
            </argument>
        </actionsColumn>
    </columns>
</listing>

Here is column:

<?php

namespace Tekglide\ProductAttachment\Ui\Component\Listing\Column\Attachment;

use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;

class Upload extends \Magento\Ui\Component\Listing\Columns\Column
{
    const NAME = 'attachment';

    const ALT_FIELD = 'name';

    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $objectManager;

    /**
     * @param ContextInterface                          $context
     * @param UiComponentFactory                        $uiComponentFactory
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     * @param array                                     $components
     * @param array                                     $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $components, $data);
        $this->objectManager = $objectManager;
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            $fieldName = $this->getData('name');
            $storeId = $this->context->getRequestParam('store');

            foreach ($dataSource['data']['items'] as &$item) {
                $item[$fieldName . '_form_key'] = $this->getFormKey();
                $item[$fieldName . '_upload_max_size'] = $this->getMaxFileSize();
                $item[$fieldName . '_store_id'] = $storeId;
                $item[$fieldName . '_upload_url'] = $this->getUploadUrl();
            }
        }

        return $dataSource;
    }


    /**
     * @param array $row
     *
     * @return null|string
     */
    protected function getAlt($row)
    {
        $altField = $this->getData('config/altField') ?: self::ALT_FIELD;
        return isset($row[$altField]) ? $row[$altField] : null;
    }

    /**
     * @return \Tekglide\ProductAttachment\Model\File
     */
    protected function getFileModel()
    {
        return $this->objectManager->get('Tekglide\ProductAttachment\Model\File');
    }

    protected function getFormKey()
    {
        /**
         * @var \Magento\Framework\Data\Form\FormKey $formKeyInstance
         */
        $formKeyInstance = $this->objectManager->get('Magento\Framework\Data\Form\FormKey');

        return $formKeyInstance->getFormKey();
    }

    protected function getUploadUrl()
    {
        /**
         * @var \Magento\Backend\Model\Url $urlBuilder
         */
        $urlBuilder = $this->objectManager->get('Magento\Backend\Model\UrlFactory')->create();

        return $urlBuilder->getUrl('tekfile/product/upload', ['_secure' => true]);
    }

    protected function getMaxFileSize() {
        /**
         * @var \Magento\Framework\File\Size $uploaderSize
         */
        $uploaderSize = $this->objectManager->create('Magento\Framework\File\Size');

        return $uploaderSize->getMaxFileSize();
    }
}

    <?php

namespace Tekglide\ProductAttachment\Ui\Component\Listing\Column;

use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;

class Attachment extends \Magento\Ui\Component\Listing\Columns\Column
{
    const NAME = 'attachment';

    const ALT_FIELD = 'name';

    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $objectManager;

    /**
     * @param ContextInterface                          $context
     * @param UiComponentFactory                        $uiComponentFactory
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     * @param array                                     $components
     * @param array                                     $data
     */
    public function __construct(
        ContextInterface $context,
        UiComponentFactory $uiComponentFactory,
        \Magento\Framework\ObjectManagerInterface $objectManager,
        array $components = [],
        array $data = []
    ) {
        parent::__construct($context, $uiComponentFactory, $components, $data);
        $this->objectManager = $objectManager;
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            $fieldName = $this->getData('name');
            $productIds = array_column($dataSource['data']['items'], 'entity_id');
            $storeId = $this->context->getRequestParam('store');
            $files = $this->getFileModel()->getFilesProductGrid($productIds, $storeId);
            foreach ($dataSource['data']['items'] as &$item) {
                $product = new \Magento\Framework\DataObject($item);
                $item[$fieldName . '_alt'] = $this->getAlt($item) . ' Attachment';
                $item[$fieldName . '_list'] 
                    = array_key_exists($product->getEntityId(), $files)
                        ? $files[$product->getEntityId()] : [];
            }
        }

        return $dataSource;
    }

    /**
     * @param array $row
     *
     * @return null|string
     */
    protected function getAlt($row)
    {
        $altField = $this->getData('config/altField') ?: self::ALT_FIELD;
        return isset($row[$altField]) ? $row[$altField] : null;
    }

    /**
     * @return \Tekglide\ProductAttachment\Model\File
     */
    protected function getFileModel()
    {
        return $this->objectManager->get('Tekglide\ProductAttachment\Model\File');
    }
}

frontend

1
  • Please share your di.xml and table primary key column name.
    – Msquare
    Oct 20, 2023 at 15:30

1 Answer 1

0

You need to add "storageConfig" in dataSource.

<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="storageConfig" xsi:type="array">
              <item name="indexField" xsi:type="string">history_id</item>
         </item>
      </item>
</argument>

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.