摘要:是實現網格視圖的小部件,一般用于報表視圖的展示。就是連續的列,主要用于網格的行號,屬于自增式的列。指定處理的類,必須。
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圖標以及鏈接即可。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/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 本文版權歸作者,歡迎轉載,但未經作者同意必須保留...
閱讀 2219·2019-08-30 15:53
閱讀 2444·2019-08-30 12:54
閱讀 1187·2019-08-29 16:09
閱讀 718·2019-08-29 12:14
閱讀 746·2019-08-26 10:33
閱讀 2461·2019-08-23 18:36
閱讀 2950·2019-08-23 18:30
閱讀 2111·2019-08-22 17:09