摘要:是实现网格视图的小部件,一般用于报表视图的展示。就是连续的列,主要用于网格的行号,属于自增式的列。指定处理的类,必须。
Yii2 GridView是实现yii网格视图的小部件,一般用于报表视图的展示。今天,结合DataProvider(ArrayDataProvider以及SqlDataProvider)说一下GridView中的几个Columns(SerialColumn,DataColumn,ActionColumn)。
1. SerialColumnSerialColumn就是连续的列,主要用于网格的行号,属于自增式的列。用法很简单:
echo GridView::widget([ "dataProvider" => $dataProvider, "columns" => [ ["class" => "yiigridSerialColumn"], // 连续编号 // 其他数据列和操作列 ] ]);
展示结果:
# |
1 |
2 |
DataColumn主要展示数据的,所有跟数据有关的展示基本都在这个Column中实现。因此用法也很多,但是有一条,如果对数据不做处理,那么字段必须是ArrayDataProvider对象的allModels的二维数组的key或者是SqlDataProvider对象sql查询结果二维数组的key。
用法一:表头即字段名,首字母大写
echo GridView::widget([ "dataProvider" => $dataProvider, "columns" => [ "orderNo", //订单编号 "username" // 用户名 ] ]);
展示结果:
OrderNo | Username |
123345698763 | Test用户 |
236479547829 | Joyven |
用法二:定义表头并格式化数据
echo GridView::widget([ "dataProvider" => $dataProvider, "columns" => [ "time:date:日期", "pv:raw:PV", "uv:raw:UV", ] ]);
展示结果:
日期 | PV | UV |
2016年9月10日 | 8500 | 6384 |
2016年9月9日 | 8378 | 6523 |
用法三:数据过滤,class、attribute、label、format均不是必须要的。class默认是yiigridDataColumn,attribute是指定排序的字段key,一定是dataProvider中提供的数据的key,如果不指定,对于过滤的数据,不能点击表头排序。
echo GridView::widget([ "dataProvider" => $dataProvider, "columns" => [ [ "value" => function($data) { if (isset($data["time"])) { return date("Y-m-d", $data["time"]); } }, "attribute" => "time", //用于排序,如果不写,不能点击表头排序,非必须 "label" => "日期", // 自定义表头,非必须 "format" => "raw", // 格式方式,非必须 ], "pv:raw:PV", [ "class" => "yiigridDataColumn", "value" => function ($data) { if (isset($data["orderCr"])) { return ($data["orderCr"] * 100) . "%"; } }, "attribute" => "orderCr", "label" => "下单转化率", "format" => "raw", ], ] ]);
关于format支持的格式:
format | 说明 | 参数 | 返回 | 备注 | |
raw | Formats the value as is without any formatting. This method simply returns back the parameter without any format.The only exception is a null value which will be formatted using [[nullDisplay]]. | @param mixed $value the value to be formatted. | @return string the formatted result. | - | |
text | Formats the value as an HTML-encoded plain text. | @param string $value the value to be formatted. | @return string the formatted result. | - | |
ntext | Formats the value as an HTML-encoded plain text with newlines converted into breaks. | @param string $value the value to be formatted. | @return string the formatted result. | - | |
html | Formats the value as HTML text.The value will be purified using [[HtmlPurifier]] to avoid XSS attacks.Use [[asRaw()]] if you do not want any purification of the value. | @param string $value the value to be formatted.@param array or null $config the configuration for the HTMLPurifier class. | @return string the formatted result. | - | |
date | Formats the value as a date. | @param integer or string or DateTime $value the value to be formatted. The following types of value are supported: 1.an integer representing a UNIX timestamp; 2.a string that can be parsed to create a DateTime object.The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given; 3.a PHP DateTime object @param string $format the format used to convert the value into a date string.If null, [[dateFormat]] will be used. This can be "short", "medium", "long", or "full", which represents a preset format of different lengths.It can also be a custom format as specified in the ICU manual.Alternatively this can be a string prefixed with php: representing a format that can be recognized by the PHP date()-function. | @return string the formatted result. | @throws InvalidParamException if the input value can not be evaluated as a date value. @throws InvalidConfigException if the date format is invalid. | |
time | Formats the value as a time. | @param integer or string or DateTime $value the value to be formatted. The following types of value are supported:1. an integer representing a UNIX timestamp; 2. a string that can be parsed to create a DateTime object.The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given; 3. a PHP DateTime object @param string $format the format used to convert the value into a date string.If null, [[timeFormat]] will be used.This can be "short", "medium", "long", or "full", which represents a preset format of different lengths.It can also be a custom format as specified in the ICU manual.Alternatively this can be a string prefixed with php: representing a format that can be recognized by the PHP date()-function. | @return string the formatted result. | @throws InvalidParamException if the input value can not be evaluated as a date value. @throws InvalidConfigException if the date format is invalid.@see timeFormat | |
paragraphs | Formats the value as HTML-encoded text paragraphs.Each text paragraph is enclosed within a tag.One or multiple consecutive empty lines divide two paragraphs. |
@param string $value the value to be formatted. | @return string the formatted result. | - | |
Formats the value as a mailto link. | @param string $value the value to be formatted. @param array $options the tag options in terms of name-value pairs. See [[Html::mailto()]]. | @return string the formatted result. | - | ||
image | Formats the value as an image tag. | @param mixed $value the value to be formatted. @param array $options the tag options in terms of name-value pairs. See [[Html::img()]]. | @return string the formatted result. | - | |
url | Formats the value as a hyperlink. | @param mixed $value the value to be formatted. @param array $options the tag options in terms of name-value pairs. See [[Html::a()]]. | @return string the formatted result. | - | |
boolean | Formats the value as a boolean. | @param mixed $value the value to be formatted. @return string the formatted result. | @see booleanFormat | - | |
datetime | Formats the value as a datetime. | @param integer or string or DateTime $value the value to be formatted. The following types of value are supported: 1. an integer representing a UNIX timestamp; 2. a string that can be parsed to create a DateTime object. The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given.; 3. a PHP DateTime object @param string $format the format used to convert the value into a date string. If null, [[dateFormat]] will be used. This can be "short", "medium", "long", or "full", which represents a preset format of different lengths. It can also be a custom format as specified in the ICU manual. Alternatively this can be a string prefixed with php: representing a format that can be recognized by the PHP date()-function. | @return string the formatted result. | @throws InvalidParamException if the input value can not be evaluated as a date value.@throws InvalidConfigException if the date format is invalid. @see datetimeFormat | - |
timestamp | Formats a date, time or datetime in a float number as UNIX timestamp (seconds since 01-01-1970). | @param integer or string or DateTime $value the value to be formatted. The followingtypes of value are supported:1. an integer representing a UNIX timestamp; 2. a string that can be parsed to create a DateTime object. The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given.; 3. a PHP DateTime object | @return string the formatted result. | - | |
relativetime | Formats the value as the time interval between a date and now in human readable form.This method can be used in three different ways:1. Using a timestamp that is relative to now.2. Using a timestamp that is relative to the $referenceTime.3. Using a DateInterval object. | @param integer or string or DateTime or DateInterval $value the value to be formatted. The following types of value are supported:1. an integer representing a UNIX timestamp; 2. a string that can be parsed to create a DateTime object.The timestamp is assumed to be in [[defaultTimeZone]] unless a time zone is explicitly given.; 3. a PHP DateTime object; 4. a PHP DateInterval object (a positive time interval will refer to the past, a negative one to the future) @param integer or string or DateTime $referenceTime if specified the value is used as a reference time instead of now when $value is not a DateInterval object. | @return string the formatted result. | @throws InvalidParamException if the input value can not be evaluated as a date value. | |
intger | Formats the value as an integer number by removing any decimal digits without rounding. | @param mixed $value the value to be formatted. @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. | @return string the formatted result. | @throws InvalidParamException if the input value is not numeric. | |
decimal | Formats the value as a decimal number.Property [[decimalSeparator]] will be used to represent the decimal point. The value is rounded automatically to the defined decimal digits. | @param mixed $value the value to be formatted. @param integer $decimals the number of digits after the decimal point. If not given the number of digits is determined from the [[locale]] and if the PHP intl extension is not available defaults to 2. @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. | @return string the formatted result. | @throws InvalidParamException if the input value is not numeric.@see decimalSeparator @see thousandSeparator | |
percent | Formats the value as a percent number with "%" sign. | @param mixed $value the value to be formatted. It must be a factor e.g. 0.75 will result in 75%. @param integer $decimals the number of digits after the decimal point.@param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]].@param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. | @return string the formatted result. | @throws InvalidParamException if the input value is not numeric. | |
scientific | Formats the value as a scientific number. | @param mixed $value the value to be formatted. @param integer $decimals the number of digits after the decimal point. @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]].@param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. | @return string the formatted result. | @throws InvalidParamException if the input value is not numeric. | |
currency | Formats the value as a currency number.This function does not requires the PHP intl extension to be installed to work but it is highly recommended to install it to get good formatting results. | @param mixed $value the value to be formatted. @param string $currency the 3-letter ISO 4217 currency code indicating the currency to use.If null, [[currencyCode]] will be used.@param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. | @return string the formatted result. | @throws InvalidParamException if the input value is not numeric. @throws InvalidConfigException if no currency is given and [[currencyCode]] is not defined. | |
spellout | Formats the value as a number spellout.This function requires the PHP intl extension to be installed. | @param mixed $value the value to be formatted | @return string the formatted result. | @throws InvalidParamException if the input value is not numeric. @throws InvalidConfigException when the PHP intl extension is not available. | |
ordinal | Formats the value as a ordinal value of a number. This function requires the PHP intl extension to be installed. | @param mixed $value the value to be formatted | @return string the formatted result. | @throws InvalidParamException if the input value is not numeric. @throws InvalidConfigException when the PHP intl extension is not available. | |
shortsize | Formats the value in bytes as a size in human readable form for example 12 KB.This is the short form of [[asSize]]. If [[sizeFormatBase]] is 1024, binary prefixes (e.g. kibibyte/KiB, mebibyte/MiB, ...) are used in the formatting result. | @param integer $value value in bytes to be formatted. @param integer $decimals the number of digits after the decimal point. @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. | @return string the formatted result. | @throws InvalidParamException if the input value is not numeric. @see sizeFormat @see asSize | |
size | Formats the value in bytes as a size in human readable form, for example 12 kilobytes. If [[sizeFormatBase]] is 1024, binary prefixes (e.g. kibibyte/KiB, mebibyte/MiB, ...) are used in the formatting result. | @param integer $value value in bytes to be formatted. @param integer $decimals the number of digits after the decimal point. @param array $options optional configuration for the number formatter. This parameter will be merged with [[numberFormatterOptions]]. @param array $textOptions optional configuration for the number formatter. This parameter will be merged with [[numberFormatterTextOptions]]. | @return string the formatted result. | @throws InvalidParamException if the input value is not numeric. @see sizeFormat @see asShortSize |
ActionColumn是对一行数据进行操作的句柄。class指定处理的类,必须。 header指定表头显示,如果不写,默认为空。非必须。 template中默认有三个:{view} {update} {delete},如果需要其他的,自行添加,比如下面添加了{onshelf} {offshelf} {robot}这三个。buttons除了默认的三个可以不写,其他都必须写。
echo GridView::widget([ "dataProvider" => $dataProvider, "columns" => [ [ "class" => "yiigridActionColumn", "header" => "操作’, "template" => "{view} {update} {delete} {onshelf} {offshelf} {robot}", "buttons" => [ "onshelf" => function ($url, $model, $key){ return $model["status"] ? "" : Html::a("", ["item/shelf"], ["title" => "上架商品", "data" => ["method" => "post", "id" => $key, "type" => "on"], "class"=> "shelf"]); }, "offshelf" => function ($url, $model, $key){ return $model["status"] ? Html::a("", ["item/shelf"], ["title" => "下架商品", "data" => ["method" => "post", "id" => $key, "type" => "off"], "class"=> "shelf"]) : ""; }, "robot" => function ($url, $model, $key){ return Html::a("", ["robot/like", "id" => $model["pid"] ], ["title" => "自动点赞", "class" => "post-autolike", "data" => ["method" => "post"]]) ; }, ] ], ], ]);
展示效果:
操作 |
查看 更新 删除 上架商品 下架商品 自动点赞 |
把相应的文字替换成icon图标以及链接即可。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/30430.html
摘要:最终版编号生成时间用户名性别省市搞定,在运行程序,表头改成编号用户名生成时间等,不要英文这个问题搞定了。是代表对进行什么样的格式化,默认来对其处理。分钟后,我郁闷的回来了,知道这回客户提了那些乱七八糟的需求么且听下回分解睡衣篇 增删改查,数据库操作的四大法宝中最常用的就是查了,一条记录、几条记录、一大堆记录。对于yii2而言,尤其后台,GridView是最常用且好用的数据列表部件,今天...
摘要:先来看看实现的效果关键代码设置显示最下面的设置每行数据的复选框属性删除设置删除按钮垮列显示其他列每个都要增加项,设置为,达到隐藏单元格的目的启用禁用管理操作获取选择的数据最后我们就可以提交到相应的控制器。 先来看看实现的效果 showImg(https://segmentfault.com/img/bVJIqC?w=658&h=279); 关键代码 获取选择的数据 var i...
摘要:于是就会报出这样一个错误要在两个数据库同一台服务器上进行关联数据查询,纯语句如下转化成语句时默认不会在表明前添加数据库名,于是在执行语句时就会默认此表在数据库下。默认是这样的只需要在表明前添加数据库名为了提高代码稳定性,可以这样写 背景:在一个mysql服务器上(注意:两个数据库必须在同一个mysql服务器上)有两个数据库: memory (存储常规数据表) 中有一个 use...
摘要:今天把这个问题讲明白了,看看是怎么个多表关联以及如何去优化这个关联。现需要在列表展示表的来源渠道,且该渠道可搜索。关联表字段增加查询中的搜索模型也是通过实现的,该模型通过控制着哪个字段可搜索,哪个字段不可搜索。 作者:白狼 出处:http://www.manks.top/yii2_many_ar_relation_search.html 本文版权归作者,欢迎转载,但未经作者同意必须保留...
阅读 2218·2019-08-30 15:53
阅读 2443·2019-08-30 12:54
阅读 1186·2019-08-29 16:09
阅读 718·2019-08-29 12:14
阅读 745·2019-08-26 10:33
阅读 2461·2019-08-23 18:36
阅读 2949·2019-08-23 18:30
阅读 2110·2019-08-22 17:09