Jul 06

integrate import export csv with backend list in octobercms

To be able to Import and Export data from database is crutial part for data anylysis and migration. OctoberCMS has built in support for Import/Export but it need to be done manually. You need to have basic coding experiance to implement it.

Do not worry we will cover all start to end with example code in this article to make it really easy for you to integrate Import and Export in your list.

First thing first you can siply use OctoberCMS Builder Plugin to create list of your data.

Now lest's start integrating Import and Export in your backend listing.

In your controller file you need to add basic code snippet

  1. ImportExportController Behaviout to add action/routes
  2. Configration file to tell how to Import and Export Data
Add this code to your controller file
// if you have more behaviors add this one as extra
public $implement = [
    'Backend.Behaviors.ImportExportController',
];

// your config file
public $importExportConfig = 'config_import_export.yaml';
Code for `config_import_export.yaml` you need to put this file along side your `controller`
export:
    useList: true

When you use useList: true option it will fetch all the details from the list and

Now you need to add button or menu which can redirect you to export action of your controller, as we already implemented import-export behavior in our controller.

http://localhost/backend/<author_name>/<plugin_name>/<sontroller_name>/export

It will generate CSV no question asked that url will read all info from list config and let you directly download export.csv file

If you need more advanced and controlled Export/Import stuff you can add your configrations
Inside your config config_import_export.yaml add custom list definition
export:
    title: Export TimeLog
    modelClass: HardikSatasiyaTimeTrackerModelsTimeLogExport
    list: $/hardiksatasiya/timetracker/models/timelog/columns.yaml
    redirect: hardiksatasiya/timetracker/timelog

Now you need to create export.htm along side in your controller file.

Put `export.htm` along side in your `controller` file.
<?= Form::open(['class' => 'layout']) ?>

    <div class="layout-row">
        <?= $this->exportRender() ?>
    </div>

    <div class="form-buttons">
        <button
            type="submit"
            data-control="popup"
            data-handler="onExportLoadForm"
            data-keyboard="false"
            class="btn btn-primary">
            Export records
        </button>
    </div>

<?= Form::close() ?>

Now you can simple add Import and Export menu items in side menu bar. which can redirect user to respective URL.

  1. http://localhost/backend/<author_name>/<plugin_name>/<sontroller_name>/import
  2. http://localhost/backend/<author_name>/<plugin_name>/<sontroller_name>/export

integate-import-export

In this way you can have more control over importing and exporting data.

If you have any condfusion or question please add comments.

Ad
Ad