Feb 15

How to add new column in october cms list

often you extended some plugin and added fields to the database and wanted to show that newly added field data into the record list.

fortunately, there is one very easy way if that record list is shown using ListController Behavior it's quite easy to add a new column to the list.

You need to extend Controller which is showing that record list

here for example scenario, we have Items controller and item model. this is developed by some other user. now anyhow we extended item database table and added the field quantity to it. as well extended Form field so we can insert and update it too.

Now we just need to show it into record list. use the below code to implement it.

Extending Controller from Plugin's Boot method
<?php namespace HardikSatasiya\SO;

use System\Classes\PluginBase;

use HardikSatasiya\SO\Controllers\Items;
use HardikSatasiya\SO\Models\Item;

class Plugin extends PluginBase
{
    public function boot() {

        // extending Controller which has ListController Behavior 
        Items::extendListColumns(function($list, $model) {

            // we want only our Item model be extended
            if (!$model instanceof Item) {
                return;
            }

            // adding quantity column
            $list->addColumns([
                'quantity' => [
                    'label' => 'Quantity',
                    'type' => 'number',
                    'searchable' => true
                ]
            ]);

        });
    }

    // ...
It will show Quantity column in list.

october-cms-extend-list

Please share the tutorial if you like it. Thanks for your time!

Ad
Ad