Extensions

Default extensions

Extension name Interface Default extension class name
IPager Mesour\DataGrid\Extensions\Pager\IPager Mesour\DataGrid\Extensions\Pager\PagerExtension
IFilter Mesour\DataGrid\Extensions\Filter\IFilter Mesour\DataGrid\Extensions\Filter\FilterExtension
ISortable Mesour\DataGrid\Extensions\Sortable\ISortable Mesour\DataGrid\Extensions\Sortable\SortableExtension
ISelection Mesour\DataGrid\Extensions\Selection\ISelection Mesour\DataGrid\Extensions\Selection\SelectionExtension
IEditable Mesour\DataGrid\Extensions\Editable\IEditable Mesour\DataGrid\Extensions\Editable\EditableExtension
IOrdering Mesour\DataGrid\Extensions\Ordering\IOrdering Mesour\DataGrid\Extensions\Ordering\OrderingExtension
IExport Mesour\DataGrid\Extensions\Export\IExport Mesour\DataGrid\Extensions\Export\ExportExtension
ISubItem Mesour\DataGrid\Extensions\SubItem\ISubItem Mesour\DataGrid\Extensions\SubItem\SubItemExtension

Usage

Mesour\DataGrid\ExtendedGrid have some enable* methods. For example enablePager

$pagerExtensionInstance = $grid->enablePager(20);

//! this is equivalent
$pagerExtensionInstance = $grid->getExtension('IPager');
$pagerExtensionInstance->getPaginator()->setItemsPerPage(20);

Expansion of existing extensions

For example extend Mesour\DataGrid\Extensions\Pager\PagerExtension and create custom AdvancedPager

class AdvancedPager extends Mesour\DataGrid\Extensions\Pager\PagerExtension
{
    //! some your logic
}

Set this extension as IPager for grid.

//! advanced-pager = extension name
$pagerExtension = new AdvancedPager('advanced-pager');

//! IPager = extension name
$grid->setExtension($pagerExtension, 'IPager');
Info And now grid will use your extension.

Creating custom extensions

Create a interface that extends Mesour\DataGrid\Extensions\IExtension

namespace App;

interface IMyNewExtension extends Mesour\DataGrid\Extensions\IExtension{
}

Interface Mesour\DataGrid\Extensions\IExtension

Method Parameters Description
createInstance Mesour\DataGrid\Extensions\IExtension $extension, $name = NULL Fired after extension is attached to grid
gridCreate $data = [] Fired on start method create() in BaseGrid
afterGetCount $count Fired after get current count from data source
beforeFetchData $data = [] Fired before call fetchAll() on data source
afterFetchData $currentData, $data = [], $rawData = [] Fired after call fetchAll() on data source
attachToRenderer Mesour\DataGrid\Renderer\IGridRenderer $renderer, $data = [], $rawData = [] Fired for attaching extension to Mesour\DataGrid\Renderer\IGridRenderer
reset $hard = FALSE Fired if reset() on Mesour\UI\DataGrid is called

Create a class that implements IMyNewExtension interface

namespace App;

class MyNewExtension extends \Mesour\UI\Control implements IMyNewExtension
{
    public function yourOwnMethod() {
        //! do something
    }

    public function createInstance(\Mesour\DataGrid\Extensions\IExtension $extension, $name = NULL){}

    public function gridCreate($data = []){}

    public function afterGetCount($count){}

    public function beforeFetchData($data = []){}

    public function afterFetchData($currentData, $data = []){}

    public function attachToRenderer(\Mesour\DataGrid\Renderer\IGridRenderer $renderer, $data = []){}

    public function reset($hard = FALSE){}
}

Extend grid for new extension

Create a class that extends Mesour\UI\DataGrid

class MyDataGrid extends Mesour\UI\DataGrid
{
    public function __construct($name = NULL, Mesour\Components\IContainer $parent = NULL) {
        parent::__construct($name, $parent);

        $this->getExtensionStorage()
            ->addNewExtension(
                'IMyNewExtension', //! extension name
                'my_new_extension', //! component name
                App\IMyNewExtension::class, 
                App\MyNewExtension::class
            );
    }
}

And create some enable* method on MyDataGrid

public function enableTest()
{
    $testExtension = $this->getExtension('IMyNewExtension');
    $testExtension->yourOwnMethod();

    return $testExtension;
}