摘要:是實(shí)現(xiàn)網(wǎng)格視圖的小部件,一般用于報表視圖的展示。就是連續(xù)的列,主要用于網(wǎng)格的行號,屬于自增式的列。指定處理的類,必須。
Yii2 GridView是實(shí)現(xiàn)yii網(wǎng)格視圖的小部件,一般用于報表視圖的展示。今天,結(jié)合DataProvider(ArrayDataProvider以及SqlDataProvider)說一下GridView中的幾個Columns(SerialColumn,DataColumn,ActionColumn)。
1. SerialColumnSerialColumn就是連續(xù)的列,主要用于網(wǎng)格的行號,屬于自增式的列。用法很簡單:
echo GridView::widget([ "dataProvider" => $dataProvider, "columns" => [ ["class" => "yiigridSerialColumn"], // 連續(xù)編號 // 其他數(shù)據(jù)列和操作列 ] ]);
展示結(jié)果:
# |
1 |
2 |
DataColumn主要展示數(shù)據(jù)的,所有跟數(shù)據(jù)有關(guān)的展示基本都在這個Column中實(shí)現(xiàn)。因此用法也很多,但是有一條,如果對數(shù)據(jù)不做處理,那么字段必須是ArrayDataProvider對象的allModels的二維數(shù)組的key或者是SqlDataProvider對象sql查詢結(jié)果二維數(shù)組的key。
用法一:表頭即字段名,首字母大寫
echo GridView::widget([ "dataProvider" => $dataProvider, "columns" => [ "orderNo", //訂單編號 "username" // 用戶名 ] ]);
展示結(jié)果:
OrderNo | Username |
123345698763 | Test用戶 |
236479547829 | Joyven |
用法二:定義表頭并格式化數(shù)據(jù)
echo GridView::widget([ "dataProvider" => $dataProvider, "columns" => [ "time:date:日期", "pv:raw:PV", "uv:raw:UV", ] ]);
展示結(jié)果:
日期 | PV | UV |
2016年9月10日 | 8500 | 6384 |
2016年9月9日 | 8378 | 6523 |
用法三:數(shù)據(jù)過濾,class、attribute、label、format均不是必須要的。class默認(rèn)是yiigridDataColumn,attribute是指定排序的字段key,一定是dataProvider中提供的數(shù)據(jù)的key,如果不指定,對于過濾的數(shù)據(jù),不能點(diǎn)擊表頭排序。
echo GridView::widget([ "dataProvider" => $dataProvider, "columns" => [ [ "value" => function($data) { if (isset($data["time"])) { return date("Y-m-d", $data["time"]); } }, "attribute" => "time", //用于排序,如果不寫,不能點(diǎn)擊表頭排序,非必須 "label" => "日期", // 自定義表頭,非必須 "format" => "raw", // 格式方式,非必須 ], "pv:raw:PV", [ "class" => "yiigridDataColumn", "value" => function ($data) { if (isset($data["orderCr"])) { return ($data["orderCr"] * 100) . "%"; } }, "attribute" => "orderCr", "label" => "下單轉(zhuǎn)化率", "format" => "raw", ], ] ]);
關(guān)于format支持的格式:
format | 說明 | 參數(shù) | 返回 | 備注 | |
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是對一行數(shù)據(jù)進(jìn)行操作的句柄。class指定處理的類,必須。 header指定表頭顯示,如果不寫,默認(rèn)為空。非必須。 template中默認(rèn)有三個:{view} {update} {delete},如果需要其他的,自行添加,比如下面添加了{onshelf} {offshelf} {robot}這三個。buttons除了默認(rèn)的三個可以不寫,其他都必須寫。
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" => "自動點(diǎn)贊", "class" => "post-autolike", "data" => ["method" => "post"]]) ; }, ] ], ], ]);
展示效果:
操作 |
查看 更新 刪除 上架商品 下架商品 自動點(diǎn)贊 |
把相應(yīng)的文字替換成icon圖標(biāo)以及鏈接即可。
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/30430.html
摘要:最終版編號生成時間用戶名性別省市搞定,在運(yùn)行程序,表頭改成編號用戶名生成時間等,不要英文這個問題搞定了。是代表對進(jìn)行什么樣的格式化,默認(rèn)來對其處理。分鐘后,我郁悶的回來了,知道這回客戶提了那些亂七八糟的需求么且聽下回分解睡衣篇 增刪改查,數(shù)據(jù)庫操作的四大法寶中最常用的就是查了,一條記錄、幾條記錄、一大堆記錄。對于yii2而言,尤其后臺,GridView是最常用且好用的數(shù)據(jù)列表部件,今天...
摘要:先來看看實(shí)現(xiàn)的效果關(guān)鍵代碼設(shè)置顯示最下面的設(shè)置每行數(shù)據(jù)的復(fù)選框?qū)傩詣h除設(shè)置刪除按鈕垮列顯示其他列每個都要增加項,設(shè)置為,達(dá)到隱藏單元格的目的啟用禁用管理操作獲取選擇的數(shù)據(jù)最后我們就可以提交到相應(yīng)的控制器。 先來看看實(shí)現(xiàn)的效果 showImg(https://segmentfault.com/img/bVJIqC?w=658&h=279); 關(guān)鍵代碼 獲取選擇的數(shù)據(jù) var i...
摘要:于是就會報出這樣一個錯誤要在兩個數(shù)據(jù)庫同一臺服務(wù)器上進(jìn)行關(guān)聯(lián)數(shù)據(jù)查詢,純語句如下轉(zhuǎn)化成語句時默認(rèn)不會在表明前添加數(shù)據(jù)庫名,于是在執(zhí)行語句時就會默認(rèn)此表在數(shù)據(jù)庫下。默認(rèn)是這樣的只需要在表明前添加數(shù)據(jù)庫名為了提高代碼穩(wěn)定性,可以這樣寫 背景:在一個mysql服務(wù)器上(注意:兩個數(shù)據(jù)庫必須在同一個mysql服務(wù)器上)有兩個數(shù)據(jù)庫: memory (存儲常規(guī)數(shù)據(jù)表) 中有一個 use...
摘要:今天把這個問題講明白了,看看是怎么個多表關(guān)聯(lián)以及如何去優(yōu)化這個關(guān)聯(lián)?,F(xiàn)需要在列表展示表的來源渠道,且該渠道可搜索。關(guān)聯(lián)表字段增加查詢中的搜索模型也是通過實(shí)現(xiàn)的,該模型通過控制著哪個字段可搜索,哪個字段不可搜索。 作者:白狼 出處:http://www.manks.top/yii2_many_ar_relation_search.html 本文版權(quán)歸作者,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留...
閱讀 2225·2019-08-30 15:53
閱讀 2454·2019-08-30 12:54
閱讀 1205·2019-08-29 16:09
閱讀 730·2019-08-29 12:14
閱讀 758·2019-08-26 10:33
閱讀 2483·2019-08-23 18:36
閱讀 2960·2019-08-23 18:30
閱讀 2121·2019-08-22 17:09