popov / zfc-data-grid-plugin
ZF模块管理器,扩展了ZfcDatagrid的功能
Requires
- php: >=5.5
This package is auto-updated.
Last update: 2024-09-18 02:03:41 UTC
README
欢迎使用ZfcDataGrid官方文档。本文档将帮助您快速了解如何使用ZfcDataGrid。
如果您在文档中找不到某些信息,请提交一个问题!
基于ZfcDatagrid的ZF DataGrid插件模块,是一种超结构。其主要目标是降低使用复杂度并提高代码可读性。
此模块注册了新的全局配置键data_grid_plugins
并添加了ColumnFactory
。
工作原理类似于ZF2方式,例如Zend\Form
,它使用数组配置来创建表单元素。
重要!
DataGridPluginManager
设置$shareByDefault = false
,这允许在配置中避免冗余类声明。
用法
注册插件。为此,将vendor/popovserhii/zfc-data-grid-plugin/config/application.config.php.sample
的内容移动到全局config/application.config.php
通常您会创建一个新的Grid类,该类将负责您生态系统中具体的Grid。
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', ], ], ], ]); } }
列
列介绍
列定义是ZfcDataGrid
的核心部分,它们用于告诉网格显示哪些列以及如何显示。
列类型
选择
一个最简单的列定义如下所示
$this->add([ 'name' => 'Select', 'construct' => ['name', 'product'], 'label' => 'Name', ]);
GROUP_CONCAT
Doctrine
有时我们需要使用内置的数据库函数来聚合结果。为此,我们需要将\Zend\Db\Sql\Expression
或\Doctrine\ORM\Query\Expr\Func
作为参数传递给Select
列。
$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', ]);
高级用法
Doctrine不支持以下表达式并抛出异常[语法错误] 行0,列218:错误:意外的'NULL'
$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'], ]);
而不是上面的示例,您可以使用NULLIF
运算符(它在这里被支持)
$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'], ]);
注意:一些函数(如GROUP_CONCAT)只在单个数据库中表示,因此Doctrine默认不支持它,您需要将相关的包包含到您的项目中。
ZendTable
$this->add([ 'name' => 'Select', 'construct' => [new \Zend\Db\Sql\Expression ('GROUP_CONCAT(serial.number)'), 'serial_all'], 'label' => 'Serial number', ]);
列数据类型
数字
Number
(原始:Type\Number
)列数据类型用于使用PHP NumberFormatter格式化数字,因此您可以使用此数据类型中的NumberFormatter属性,为此您创建一个Number
对象,它可以按以下顺序接受以下参数
- 格式样式:默认
NumberFormatter::DECIMAL
- 格式类型:默认
NumberFormatter::TYPE_DEFAULT
- 区域设置:默认
Locale::getDefault()
对于此类型,您还可以执行以下操作
'type' => ['prefix' => 'prefix']
'type' => ['suffix' => 'suffix']
'type' => ['attribute' => ['attrName', 'attrValue']]
此列数据类型的用法示例如下
$this->add([ 'name' => 'Select', 'construct' => ['weight', 'product'], 'label' => 'Weight', 'type' => [ 'name' => 'Number', 'attribute' => [\NumberFormatter::FRACTION_DIGITS, 2], 'suffix' => ' kg' ], ]);
列数据样式
对齐
Align
用于更改网格行或列的文本方向,创建Align
的方法如下
$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] ]], ]);
其中参数是红色、绿色、蓝色的值。
有关如何在行或列上应用样式的说明,请参阅[应用样式](#Applying Style)部分。
粗体
粗体
样式会使文本加粗,您可以创建一个粗体样式如下:
$this->add([ 'name' => 'Select', 'construct' => ['name', 'product'], 'label' => 'Name', 'styles' => [[ 'name' => 'Bold' ]], ]);
有关如何在行或列上应用样式的说明,请参阅[应用样式](#Applying Style)部分。
颜色
颜色
用于改变网格行或列的颜色,创建一个颜色
的方法如下:
$this->add([ 'name' => 'Select', 'construct' => ['name', 'product'], 'label' => 'Name', 'styles' => [[ 'name' => 'Color', 'consturct' => [200, 200, 200] ]], ]);
其中参数是红色、绿色、蓝色的值。
有关如何在行或列上应用样式的说明,请参阅[应用样式](#Applying Style)部分。
斜体
斜体
样式会使文本倾斜,您可以创建一个斜体样式如下:
$this->add([ 'name' => 'Select', 'construct' => ['name', 'product'], 'label' => 'Name', 'styles' => [[ 'name' => 'Italic' ]], ]);
其中参数是红色、绿色、蓝色的值。
有关如何在行或列上应用样式的说明,请参阅[应用样式](#Applying Style)部分。
删除线
删除线
样式会使文本加删除线,您可以创建一个删除线样式如下:
$this->add([ 'name' => 'Select', 'construct' => ['name', 'product'], 'label' => 'Name', 'styles' => [[ 'name' => 'Strikethrough' ]], ]);
有关如何在行或列上应用样式的说明,请参阅[应用样式](#Applying Style)部分。
CSS类
CSS类
用于设置网格行或单元格的额外类属性,创建一个CSS类
的方法如下:
$this->add([ 'name' => 'Select', 'construct' => ['name', 'product'], 'label' => 'Name', 'styles' => [[ 'name' => 'CSSClass', 'class' => ['text-upper', 'product-name'] ]], ]);
应用样式
- 仅在列值:product_price: 等于 50 时应用
$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]] ], ], ]);
您可以使用ByValue
添加多个条件来应用样式,您可以将多个条件之间的运算符设置为'OR'或'AND',如下所示:
- 仅在列 ':product_price:' 的值在 20 到 40(含)之间时应用
$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] ] ], ], ]);
- 仅在列 ':order_quantity:' 的值大于或等于列 ':product_stock:' 的值时应用
$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]] ], ], ]);
注意。此功能尚未完全测试!
列数据格式化器
链接
链接格式化器将列内容显示为HTML链接,其中href是列内容,要使用它,请按照以下方法操作:
$this->add([ 'name' => 'Select', 'construct' => ['asin', 'product'], 'label' => 'Asin', 'formatters' => [[ 'name' => 'Link', 'link' => ['href' => '//www.amazon.de/dp/%s', 'placeholder_column' => 'product_asin'] ]], ]);
您也可以将列对象作为'placeholder_column'传递
$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] ]], ]);
链接格式化器也支持构建URL的多个占位符
$this->add([ 'name' => 'Select', 'construct' => ['asin', 'product'], 'label' => 'Asin', 'formatters' => [[ 'name' => 'Link', 'link' => ['href' => '//%s/dp/%s', 'placeholder_column' => ['marketplace_host', 'product_asin']] ]], ]);
内联
内联格式化器允许在一行中显示文本。这将替换所有<br>
、\n
以及与其他jqGrid完全兼容的一些过滤。
$this->add([ 'name' => 'Select', 'construct' => ['description', 'product'], 'label' => 'Description', 'formatters' => [[ 'name' => 'Inline', ]], ]);
外部链接
外部链接格式化器允许使用完整的URL,无需进行内部的rawurlencode
准备。
优先考虑
链接格式化器
。请在极端情况下使用。
$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'] ]], ]);
搜索面板中的下拉菜单
简单
只需将选项数组放入filter_select_options
中。请注意,选项被数组双重包装。
$this->add([ 'name' => 'Select', 'construct' => ['accepted', 'question'], 'label' => 'Accepted', 'width' => 1, 'filter_select_options' => [[ 0 => 'No', 1 => 'Yes' ]], ]);
Doctrine
filter_select_options
配置基于DoctrineModule
的Zend\Form
(一些选项需要实现)。
$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' ], ], ], ], ]);
编辑记录中的下拉菜单
Doctrine
它与filter_select_options
类似。column_select_options
将下拉菜单添加到编辑记录表单中,并包含选中数据。
$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'], ], ]);
注意。Intl使用默认的ISO日期格式(RFC 822)。有关详细格式的说明,请参阅ZF文档。
网格数据排序
默认网格数据排序可以通过sort_default
选项设置。如果之前没有应用任何其他用户过滤器,则将应用ASC
排序顺序到position
列。
$this->add([ 'name' => 'Select', 'construct' => ['position', 'product'], 'label' => 'Position', 'sort_default' => [1, 'ASC'] ]);
还可以设置几个默认排序顺序,只需将sort_default
应用到相关列。
$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'] ]);
网格数据过滤
默认网格数据排序可以通过filter_default_operation
选项设置。
如果之前没有应用任何其他用户过滤器,则默认应用like
过滤。
注意!存在问题,不允许从客户端应用过滤器。
$this->add([ 'name' => 'Select', 'construct' => ['sku', 'product'], 'label' => 'SKU', 'filter_default_operation' => \ZfcDatagrid\Filter::EQUAL ]);
按钮
按钮简介
为ZfcDataGrid
自定义按钮。
列选择器
对象表示法
$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, ], ], ]);