I am trying to upload csv that I have exported from existing admin grid. Adding additional data as perticular columnwise in same csv. Howcan I upload/import that new .csv in admin and update data in already created custom admin-grid/table ?
Magento – Magento 2 How to upload csv in admin and update data in custom admin-grid/table
customcustom-admin-grid
Related Solutions
I got solution for this. So sharing here:
You have to extend backend model file in your custom module like below:
- In system.xml, update code as below for input type="file"
<field id="customfile" translate="label" type="Magento\Config\Block\System\Config\Form\Field\File" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Custom file</label>
<comment></comment>
<backend_model>Vendor\Package\Model\Config\Backend\CustomFileType</backend_model>
<upload_dir>upload</upload_dir>
</field>
- In your custom module, Create file CustomFileType.php at below place:
Vendor\Package\Model\Config\Backend
- Override beforeSave method in that file as below:
<?php
namespace Vendor\Package\Model\Config\Backend;
class CustomFileType extends \Magento\Config\Model\Config\Backend\File
{
public function beforeSave()
{
$value = $this->getValue();
$file = $this->getFileData();
if (!empty($file)) {
/*** Here you can write your custom code to save data in custom table ***/
$uploadDir = $this->_getUploadDir();
try {
/** @var Uploader $uploader */
$uploader = $this->_uploaderFactory->create(['fileId' => $file]);
$uploader->setAllowedExtensions($this->_getAllowedExtensions());
$uploader->setAllowRenameFiles(true);
$uploader->addValidateCallback('size', $this, 'validateMaxSize');
$result = $uploader->save($uploadDir);
} catch (\Exception $e) {
throw new \Magento\Framework\Exception\LocalizedException(__('%1', $e->getMessage()));
}
$filename = $result['file'];
if ($filename) {
if ($this->_addWhetherScopeInfo()) {
$filename = $this->_prependScopeInfo($filename);
}
$this->setValue($filename);
}
} else {
if (is_array($value) && !empty($value['delete'])) {
$this->setValue('');
} elseif (is_array($value) && !empty($value['value'])) {
$this->setValue($value['value']);
} else {
$this->unsValue();
}
}
return $this;
}
}
?>
If you want to limit the collection that shows within the grid when using the UI component, you need to look at the DataProvider
class. There are methods in there which allow you to add "filters" that are always present (e.g. limiting rows to only those within a specific store).
For example, I have a custom module that keeps a history of changes made within this module on a per-item basis. So customers can have multiple "items" and a history is recorded when they act upon the item (e.g. update data). To show this history within the admin, I can't just show all the history in the grid, I need to filter it to the relevant history associated to an item ID.
So in my ui_component
xml document I look at the dataProvider
node to see which class is providing the data:
<!-- define the date source (must be the same than in argument/item/provider and argument/js_config/deps -->
<dataSource name="contacts_contact_data_source">
<argument name="dataProvider" xsi:type="configurableObject">
<!-- unique name for the grid -->
<argument name="class" xsi:type="string">ContactsGridDataProvider</argument>
<!-- name of the data source same as in argument/js_config/provider -->
In this case, the class is "ContactsGridDataProvider". Whatever class that points to should extend the class \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
. In that class you could override the prepareUpdateUrl
method and add any filter(s) you wish.
For example, my data provider overrides that function to add a filter for the "item id" the user is currently looking at:
class ScopedDataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
{
public function prepareUpdateUrl()
{
parent::prepareUpdateUrl();
if ($this->request->getParam('id')) {
$itemId = $this->request->getParam('id');
} else {
$itemId = $this->request->getParam('item_id');
}
if ($itemId) {
$this->addFilter(
$this->filterBuilder->setField('item_id')
->setValue($itemId)
->setConditionType('eq')
->create()
);
}
}
}
Adding this filter was enough to limit the scope of the grid to only those items related to the item currently being viewed. This seems like what you want to do with limiting the "ids" to only be shown if company_code = 1
.
Hope that helps you out.
Best Answer
I have finally done this by observer and event. Admin field where I upload .csv file with format of current admin grid like name,certificationId.
Vendor/Module/etc/adminhtml/System.xml:
Now Create events.xml: app/code/Vendor/Module/etc/adminhtml/events.xml Here I have used "nasmsetting" as defined in system.xml. Which will change/update in admin and call this observer.So event name "admin_system_config_changed_section_nasmsetting"
Observer where I read .csv and update table data: app/code/Vendor/Module/Observer/Update.php
}`