PHP code example of popov / zfc-data-grid-plugin

1. Go to this page and download the library: Download popov/zfc-data-grid-plugin library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

popov / zfc-data-grid-plugin example snippets


namespace Popov\Invoice\Block\Grid;

use Popov\ZfcDataGrid\Block\AbstractGrid;

class InquiryGrid extends AbstractGrid
{
    public function init()
    {
        $grid = $this->getDataGrid();
        $grid->setId('invoice');
        $grid->setTitle('Invoices');
        $grid->setRendererName('jqGrid');
        
        // native configuration
        #$colId = new Column\Select('id', 'invoice');
        #$colId->setIdentity();
        #$grid->addColumn($colId);
        
        // array configuration
        $colId = $this->add([
            'name' => 'Select',
            'construct' => ['id', 'invoice'],
            'identity' => true,
        ])->getDataGrid()->getColumnByUniqueId('invoice_id');
        
        // native configuration
        #$col = new Column\Select('code', 'invoice');
        #$col->setLabel('Invoice code');
        #$col->setIdentity(false);
        #$grid->addColumn($col);
        
        // array configuration
        $this->add([
            'name' => 'Select',
            'construct' => ['code', 'invoice'],
            'label' => 'Код инвойса',
            'identity' => false,
        ]);
        
        // native configuration
        #$colType = new Type\DateTime();
        #$col = new Column\Select('createdAt', 'invoice');
        #$col->setLabel('Date Create');
        #$col->setTranslationEnabled();
        #$col->setType($colType);
        #$col->setWidth(2);
        #$grid->addColumn($col);
        
        // array configuration
        $this->add([
            'name' => 'Select',
            'construct' => ['createdAt', 'invoice'],
            'label' => 'Date Create',
            'translation_enabled' => true,
            'width' => 2,
            'type' => ['name' => 'DateTime'],
        ]);
        
        // native configuration
        #$col = new Column\Select('name', 'contractor');
        #$col->setLabel('Contractor');
        #$col->setTranslationEnabled();
        #$col->setWidth(3);
        #$col->setUserSortDisabled(true);
        #$col->setUserFilterDisabled(true);
        #$grid->addColumn($col);
        
        // array configuration
        $this->add([
            'name' => 'Select',
            'construct' => ['name', 'contractor'],
            'label' => 'Contractor',
            'width' => 3,
            'user_sort_disabled' => true,
            'user_filter_disabled' => true,
        ]);
        
        // native configuration
        #$bg = new Style\BackgroundColor([224, 226, 229]);
        #$fmtr = new Column\Formatter\Link();
        #$fmtr->setAttribute('class', 'pencil-edit-icon');
        #$fmtr->setLink('/invoice/view/' . $fmtr->getColumnValuePlaceholder($colId));
        #$actions = new Column\Action('edit');
        #$actions->setLabel(' ');
        #$actions->setTranslationEnabled();
        #$actions->setFormatters([$fmtr]);
        #$actions->addStyle($bg);
        #$actions->setWidth(1);
        #$grid->addColumn($actions);
        
        // array configuration
        $this->add([
            'name' => 'Action',
            'construct' => ['edit'],
            'label' => ' ',
            'width' => 1,
            'styles' => [
                [
                    'name' => 'BackgroundColor',
                    'construct' => [[224, 226, 229]],
                ],
            ],
            'formatters' => [
                [
                    'name' => 'Link',
                    'attributes' => ['class' => 'pencil-edit-icon'],
                    // next two line are identical
                    //'link' => ['href' => '/invoice/view/%s', 'placeholder_column' => 'invoice_id'],
                    'link' => ['href' => '/invoice/view/%s', 'placeholder_column' => $colId], // special config
                ],
            ],
        ]);
        
        $formatter = <<<FORMATTER_JS
      function (value, options, rowObject) {
        return '<div class="input-btn-group">'
        	+ '<button class="btn btn-default btn-xs barcode-print" title="{$this->__('Print Bar code')}">' + value + '</button>'
          + '</div>';
      }
FORMATTER_JS;
        
        // native configuration
        #$formatterLink = new Formatter\Barcode();
        #$formatterLink->setSourceAttr('data-barcode');
        #$formatterLink->setAttribute('class', 'barcode-icon');
        #$formatterLink->setBasedOn($grid->getColumnByUniqueId('product_id'));
        #$actions = new Column\Action('barcode');
        #$actions->setLabel(' ');
        #$actions->setTranslationEnabled();
        #$actions->setFormatters([$formatterLink]);
        #$actions->setRendererParameter('formatter', $formatterJs, 'jqGrid');
        #$actions->setWidth(1);
        #$grid->addColumn($actions);
        
        // array configuration
        $this->add([
            'name' => 'Action',
            'construct' => ['barcode'],
            'label' => ' ',
            'translation_enabled' => true,
            'width' => 1,
            'renderer_parameter' => ['formatter', $formatter, 'jqGrid'],
            'formatters' => [
                [
                    'name' => 'Barcode',
                    'source_attr' => 'data-barcode',
                    //'placeholder_column' => $grid->getColumnByUniqueId('product_id'),
                    'attributes' => [
                        'class' => 'barcode-icon',
                    ],
                ],
            ],
        ]);
    }
}

$this->add([
    'name' => 'Select',
    'construct' => ['name', 'product'],
    'label' => 'Name',
]);

$this->add([
	'name' => 'Select',
	'construct' => [new \Doctrine\ORM\Query\Expr\Select("GROUP_CONCAT(serial.number)"), 'serial_all'], // Doctrine usage
	// or 
	'construct' => [new \Doctrine\ORM\Query\Expr\Func('GROUP_CONCAT', ['serial.number']), 'serial_all'], // Doctrine usage
	'label' => 'Serial Number',
]);

$this->add([
	'name' => 'Select',
	// @see https://github.com/doctrine/orm/issues/5801
	'construct' => [new Expr\Func('GROUP_CONCAT', ['CASE WHEN serial.cartItem > 0 THEN serial.number ELSE NULL END']), 'serial_id'],
]);

$this->add([
	'name' => 'Select',
	'construct' => [new Expr\Select('GROUP_CONCAT(CASE WHEN serial.cartItem > 0 THEN serial.number ELSE NULLIF(1,1) END)'), 'serial_id'],
]);
 

$this->add([
	'name' => 'Select',
	'construct' => [new \Zend\Db\Sql\Expression ('GROUP_CONCAT(serial.number)'), 'serial_all'],
	'label' => 'Serial number',
]);

$this->add([
    'name' => 'Select',
    'construct' => ['weight', 'product'],
    'label' => 'Weight',
    'type' => [
        'name' => 'Number',
        'attribute' => [\NumberFormatter::FRACTION_DIGITS, 2],
        'suffix' => ' kg'
    ],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['price', 'product'],
    'label' => 'Asin',
    'styles' => [[
        'name' => 'Align',
        'construct' => ['right'],
    ]],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['price', 'product'],
    'label' => 'Asin',
    'styles' => [[
        'name' => 'Align',
        'construct' => [\ZfcDatagrid\Column\Style\Align::$RIGHT],
    ]],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['price', 'product'],
    'label' => 'Asin',
    'type' => [
        'name' => 'Number'
    ],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['temperature', 'planet'],
    'label' => 'Temperature',
    'styles' => [[
        'name' => 'BackgroundColor',
        'constuct' => [200, 200, 200]
    ]],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['name', 'product'],
    'label' => 'Name',
    'styles' => [[
        'name' => 'Bold'
    ]],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['name', 'product'],
    'label' => 'Name',
    'styles' => [[
        'name' => 'Color',
        'consturct' => [200, 200, 200]
    ]],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['name', 'product'],
    'label' => 'Name',
    'styles' => [[
        'name' => 'Italic'
    ]],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['name', 'product'],
    'label' => 'Name',
    'styles' => [[
        'name' => 'Strikethrough'
    ]],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['name', 'product'],
    'label' => 'Name',
    'styles' => [[
        'name' => 'CSSClass',
        'class' => ['text-upper', 'product-name']
    ]],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['price', 'product'],
    'label' => 'Price',
    'styles' => [
        [
            'name' => 'Color',
            'construct' => [\ZfcDatagrid\Column\Style\Color::$RED],
            'byValue' => [[':product_price:', 50, \ZfcDatagrid\Filter::EQUAL]]
        ],
    ],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['price', 'product'],
    'label' => 'Price',
    'styles' => [
        [
            'name' => 'Color',
            'construct' => [\ZfcDatagrid\Column\Style\Color::$RED],
            'byValueOperator' => 'AND',
            'byValue' => [
                [':product_price:', 20, \ZfcDatagrid\Filter::GREATER_EQUAL],
                [':product_price:', 40, \ZfcDatagrid\Filter::LESS_EQUAL]
            ]
        ],
    ],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['quantity', 'order'],
    'label' => 'Price',
    'styles' => [
        [
            'name' => 'Color',
            'construct' => [\ZfcDatagrid\Column\Style\Color::$GREEN],
            'byValue' => [[':order_quantity:', ':product_stock:', \ZfcDatagrid\Filter::LESS_EQUAL]]
        ],
    ],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['asin', 'product'],
    'label' => 'Asin',
    'formatters' => [[
        'name' => 'Link',
        'link' => ['href' => '//www.amazon.de/dp/%s', 'placeholder_column' => 'product_asin']
    ]],
]);

$colId = $this->add([
    'name' => 'Select',
    'construct' => ['id', 'product'],
    'identity' => true,
])->getDataGrid()->getColumnByUniqueId('product_id');
        
$this->add([
    'name' => 'Select',
    'construct' => ['asin', 'product'],
    'label' => 'Asin',
    'formatters' => [[
        'name' => 'Link',
        'link' => ['href' => '//www.amazon.de/dp/%s', 'placeholder_column' => $colId]
    ]],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['asin', 'product'],
    'label' => 'Asin',
    'formatters' => [[
        'name' => 'Link',
        'link' => ['href' => '//%s/dp/%s', 'placeholder_column' => ['marketplace_host', 'product_asin']]
    ]],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['description', 'product'],
    'label' => 'Description',
    'formatters' => [[
        'name' => 'Inline',
    ]],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['url', 'customer'],
    'label' => 'Customer Url',
    'hidden' => true
]);
$this->add([
    'name' => 'Select',
    'construct' => ['name', 'customer'],
    'label' => 'Customer Name',
    'formatters' => [[
        'name' => 'ExternalLink',
        'link' => ['href' => '%s', 'placeholder_column' => 'customer_url']
    ]],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['accepted', 'question'],
    'label' => 'Accepted',
    'width' => 1,
    'filter_select_options' => [[
        0 => 'No',
        1 => 'Yes'
    ]],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['value', 'handbook'],
    'label' => 'MarketOrder Type',
    'filter_select_options' => [
        'options' => [
            'object_manager' => $this->getObjectManager(),
            'target_class' => Handbook::class,
            'identifier' => 'value',
            'property' => 'value',
            'is_method' => true,
            'find_method' => [
                'name' => 'findAllByTypeId',
                'params' => [
                    'type' => 'purposeBid',
                    'field' => 'type'
                ],
            ],
        ],
    ],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['value', 'handbook'],
    'label' => 'MarketOrder Type',
    'column_select_options' => [
        'options' => [
            'object_manager' => $this->getObjectManager(),
            'target_class' => Handbook::class,
            'identifier' => 'value',
            'property' => 'value',
            'is_method' => true,
            'find_method' => [
                'name' => 'findAllByTypeId',
                'params' => [
                    'type' => 'purposeBid',
                    'field' => 'type'
                ],
            ],
        ],
    ],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['createdAt', 'question'],
    'label' => 'Date Create',
    'translation_enabled' => false,
    'width' => 1,
    'filter_default_operation' => Filter::LIKE_RIGHT, // LIKE "2018-03-16%"
    'type' => [
        'name' => 'DateTime',
        //'output_pattern' => 'yyyy-MM-dd HH:mm:ss',
        'output_pattern' => 'yyyy-MM-dd',
        'source_dateTime_format' => 'Y-m-d' // this date format will be used in WHERE statment
    ],
    'renderer_parameters' => [
        #['editable', true, 'jqGrid'],
        ['formatter', 'date', 'jqGrid'], // it is important for datepicker
        ['formatoptions', ['srcformat' => 'Y-m-d', 'newformat' => 'Y-m-d'], 'jqGrid'],
        ['searchoptions', ['sopt' => ['eq']], 'jqGrid'],
    ],
]);

$this->add([
    'name' => 'Select',
    'construct' => ['position', 'product'],
    'label' => 'Position',
    'sort_default' => [1, 'ASC']
]);

$this->add([
    'name' => 'Select',
    'construct' => ['inStock', 'product'],
    'label' => 'Position',
    'sort_default' => [1, 'DESC']
]);

$this->add([
    'name' => 'Select',
    'construct' => ['position', 'product'],
    'label' => 'Position',
    'sort_default' => [2, 'ASC']
]);

$this->add([
    'name' => 'Select',
    'construct' => ['sku', 'product'],
    'label' => 'SKU',
    'filter_default_operation' => \ZfcDatagrid\Filter::EQUAL
]);

$button = new ColumnChooserButton();
        $button->setTitle('Choose columns');
        $button->setCaption('Choose');
        $button->setOptions([
                'width' => 500,
                'height' => 300,
        ]);

$this->addButton([
            'name' => 'ColumnChooser',
            'title' => 'Choose columns',
            'caption' => 'Choose',
            'options' => [
                [
                    'width' => 500,
                    'height' => 300,
                ],
            ],
        ]);
config/application.config.php