Sub items

Method enableSubItems

Without parameters

$subItems = $grid->enableSubItems();

Grid item types

Grid item addGridItem

Returns: Mesour\DataGrid\Extensions\SubItem\Items\GridItem

Parameter Type Description
$name string Sub item name
$description string Shows in subitem
$grid Mesour\UI\DataGrid Grid instance, will be cloned

Callback parameters

Parameter Type Description
$name Mesour\UI\DataGrid Sub grid instance
$rowData Mesour\Sources\ArrayHash / Entity / ActiveRow Data for current row

Code

$i = 0;
$subItems->addGridItem($name, $description, Mesour\UI\DataGrid $grid)
    ->setCallback(function (Mesour\UI\DataGrid $subGrid, $rowData) use ($sources, & $i) {
        $_source = $sources[$i];
        $subGrid->setSource($_source);
        $i++;
    });

Component item addComponentItem

Returns: Mesour\DataGrid\Extensions\SubItem\Items\ComponentItem

Parameter Type Description
$name string Sub item name
$description string Shows in subitem
$control Mesour\UI\Control / callable (which returns Mesour\UI\Control) Control instance

Callback parameters

Parameter Type Description
$name Mesour\UI\Control Instance of some control
$rowData Mesour\Sources\ArrayHash / Entity / ActiveRow Data for current row

Code

$subItems->addComponentItem($name, $description, $control)
    ->setCallback(function (Mesour\UI\Button $button, $rowData) {
        // do something on $button with $rowData
    });

Callback item addCallbackItem

Returns: Mesour\DataGrid\Extensions\SubItem\Items\CallbackItem

Parameter Type Description
$name string Sub item name
$description string Shows in subitem
$callback callable Callable

Callback parameters

Parameter Type Description
$rowData Mesour\Sources\ArrayHash / Entity / ActiveRow Data for current row

Code

$subItems->addCallbackItem($name, $description, function($rowData) {

});

Template item addTemplateItem

Returns: Mesour\DataGrid\Extensions\SubItem\Items\TemplateItem

Parameter Type Description
$name string Sub item name
$description string Shows in subitem
$templatePath string Path to template file
$tempDir string Path to writable dir
$block callable Specify block in template (NULL = no block)

Callback parameters

Parameter Type Description
$template Mesour\DataGrid\TemplateFile Instance of current template
$rowData Mesour\Sources\ArrayHash / Entity / ActiveRow Data for current row

Code

$subItems->addTemplateItem($name, $description, $templatePath, $tempDir, $block = NULL)
    ->setCallback(function (Mesour\DataGrid\TemplateFile $template, $rowData) {
        $template->name = $rowData['name'];
    });

The same method for all grid items setCheckCallback

Info called before rendering
Parameter Type Description
$callback callable Callable callback

Callback parameters

Parameter Type Description
$rowData Mesour\Sources\ArrayHash / Entity / ActiveRow Data for current row
$item Mesour\DataGrid\Extensions\SubItem\Items\Item Instance of current sub item

Code

$subItems->addGridItem('groups', 'User groups', $this->getSubGrid())
    ->setCheckCallback(function($rowData, Mesour\DataGrid\Extensions\SubItem\Items\Item $item) {
        if($rowData->user_id == 1) {
            $item->setDisabled(); //! disable item for row with ID = 1
        } else {
            $item->setDisabled(FALSE); //! for others enable it, because it is same instance for all rows
        }
    });

Usage

Grid

$mesourApp = //instance Mesour\Components\Application\IApplication
$source = //some <a href="http://components.mesour.com/version3/component/sources/" target="_blank">data source</a> or two-dimensional array

//! here is clonning, but you will use specific source for all items
$sources = [];
for ($x = 0; $x < 8; $x++) {
    $sources[] = clone $source;
}

$grid = new Mesour\UI\DataGrid('filterDataGrid', $mesourApp);

$grid->setSource($source);

$grid->enablePager(5);

//! here enable sub items extension
$subItems = $grid->enableSubItems();

//! add callback item
$subItems->addCallbackItem('test', 'Test callback item', function () {
    return '123';
});

//! add grid item
$i = 0;
$subItems->addGridItem('groups', 'User groups', $this->getSubGrid())
    ->setCheckCallback(function($rowData, Mesour\DataGrid\Extensions\SubItem\Items\Item $item) {
        if($rowData->user_id == 1) {
            $item->setDisabled();
        } else {
            $item->setDisabled(FALSE);
        }
    })
    ->setCallback(function (Mesour\UI\DataGrid $subGrid, $data) use ($sources, & $i) {
        $_source = $sources[$i];
        $subGrid->setSource($_source);
        $i++;
    });

//! add component item
$subItems->addComponentItem('button', 'Test button', [$this, 'createTestButton'])
    ->setCallback(function(Mesour\UI\Button $button, $data) {

    });

//! add template item
$subItems->addTemplateItem(
    'description',
    'Long description',
    __DIR__ . '/../templates/test.latte',
    __DIR__ . '/../../../temp/cache',
    'test'
)
    ->setCallback(function(Mesour\DataGrid\TemplateFile $template, $data) {
        $template->name = $data['name'];
    });

$grid->addText('name', 'Name');

$grid->addText('surname', 'Surname');

$grid->addText('email', 'E-mail');

$grid->render()

Method $this->getSubGrid()

private function getSubGrid()
{
    $grid = new Mesour\UI\DataGrid('subGrid');

    $grid->enablePager(5);

    $grid->enableEditable();

    $grid->addText('name');

    $grid->addText('surname');

    $grid->enableFilter();

    $selection = $grid->enableRowSelection()
        ->getLinks();

    $selection->addLink('Active');

    return $grid;
}

Method $this->createTestButton()

public function createTestButton(Mesour\Components\ComponentModel\IComponent $parent, $name)
{
    $button = new Mesour\UI\Button($name, $parent);

    $button->setText('To mesour.com >>');

    $button->setAttribute('href', $button->link('http://mesour.com'))
        ->setAttribute('target', '_blank');

    return $button;
}

File test.latte

{define test}
<b>test</b> {$name}
{/define}

{define test2}
<b>test2</b> {$name}
{/define}

Demo


NameSurnameE-mail
JohnDoejohn.doe@test.xx
Test callback item
Test button
Long description
PeterLarsonpeter.larson@test.xx
Test callback item
User groups
Test button
Long description
ClaudeGravesclaude.graves@test.xx
Test callback item
User groups
Test button
Long description
StuartNormanstuart.norman@test.xx
Test callback item
User groups
Test button
Long description
KathyArnoldkathy.arnold@test.xx
Test callback item
User groups
Test button
Long description