php gridview,PHP编程:yii2-GridView在开发中常用的功能及技巧总结
《PHP編程:yii2-GridView在開發中常用的功能及技巧總結》要點:
本文介紹了PHP編程:yii2-GridView在開發中常用的功能及技巧總結,希望對您有用。如果有疑問,可以聯系我們。
相關主題:YII框架
數據網格或者說 GridView 小部件是Yii中最強大的部件之一.它有一個屬性名叫 dataProvider ,這個屬性能夠提供一個數據提供者的示例并且可以顯示所提供的數據,即使用 yii\grid\GridView::columns 屬性的一組列配置,在一個表格中渲染每一行數據.
例如,PHP編程
use yii\helpers\Html;
use yii\grid\GridView;
= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
]);?>
一、表格列
PHP編程
表格的列是通過 GridView 配置項中的 yii\grid\GridView::columns 屬性配置的.PHP編程
use yii\grid\GridView;
echo GridView::widget([
'dataProvider' => $dataProvider,
//表格列值搜索功能,注意一定要配合attribute才會顯示
//$searchModel = new ArticleSearch();
'filterModel' => $searchModel,
//重新定義分頁樣式
'layout'=> '{items}
{pager}','pager'=>[
//'options'=>['class'=>'hidden']//關閉分頁
'firstPageLabel'=>"First",
'prevPageLabel'=>'Prev',
'nextPageLabel'=>'Next',
'lastPageLabel'=>'Last',
]
'columns' => [
['class' => 'yii\grid\SerialColumn'],//序列號從1自增長
// 數據提供者中所含數據所定義的簡單的列
// 使用的是模型的列的數據
'id',
'username',
// 更復雜的列數據
[
'class' => 'yii\grid\DataColumn', //由于是默認類型,可以省略
'value' => function ($data) {
return $data->name;
// 如果是數組數據則為 $data['name'] ,
例如,使用 SqlDataProvider 的情形.
},
],
['label'=>'標題','value' => 'title'],
['label'=>'文章內容','format' => 'html','value' => 'content'],
[
'label'=>'文章類別',
/*'attribute' => 'cid',產生一個a標簽,點擊可排序*/
'value' => 'cate.cname' //關聯表
],
[
//動作列yii\grid\ActionColumn
//用于顯示一些動作按鈕,如每一行的更新、刪除操作.
'class' => 'yii\grid\ActionColumn',
'header' => '操作',
'template' => '{delete} {update}',//只需要展示刪除和更新
'headerOptions' => ['width' => '240'],
'buttons' => [
'delete' => function($url, $model, $key){
return Html::a(' 刪除',
['del', 'id' => $key],
[
'class' => 'btn btn-default btn-xs',
'data' => ['confirm' => '你確定要刪除文章嗎?',]
]
);
},
],
],
],
]);
?>
1. 處理時間
PHP編程
數據列的主要配置項是 yii\grid\DataColumn::format 屬性.它的值默認是使用 \yii\i18n\Formatter 應用組件.PHP編程
[
'label'=>'更新日期',
'format' => ['date', 'php:Y-m-d'],
'value' => 'updated_at'
],
//or
[
//'attribute' => 'created_at',
'label'=>'更新時間',
'value'=>function($model){
return date('Y-m-d H:i:s',$model->created_at);
},
'headerOptions' => ['width' => '170'],
],
2. 處理圖片
PHP編程
[
'label'=>'封面圖',
'format'=>'raw',
'value'=>function($m){
return Html::img($m->cover,
['class' => 'img-circle',
'width' => 30]
);
}
],
3. 數據列有鏈接
PHP編程
[
'attribute' => 'title',
'value' => function ($model, $key, $index, $column) {
return Html::a($model->title,
['article/view', 'id' => $key]);
},
'format' => 'raw',
],
4. 數據列顯示枚舉值(男/女)
PHP編程
[
'attribute' => 'sex',
'value'=>function ($model,$key,$index,$column){
return $model->sex==1?'男':'女';
},
//在搜索條件(過濾條件)中使用下拉框來搜索
'filter' => ['1'=>'男','0'=>'女'],
//or
'filter' => Html::activeDropDownList($searchModel,
'sex',['1'=>'男','0'=>'女'],
['prompt'=>'全部']
)
],
[
'label'=>'產品狀態',
'attribute' => 'pro_name',
'value' => function ($model) {
$state = [
'0' => '未發貨',
'1' => '已發貨',
'9' => '退貨,已處理',
];
return $state[$model->pro_name];
},
'headerOptions' => ['width' => '120']
]
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持維易PHP.PHP編程
總結
以上是生活随笔為你收集整理的php gridview,PHP编程:yii2-GridView在开发中常用的功能及技巧总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php滴滴平台接口,图片服务API文档
- 下一篇: 华硕B250pro主板biso关闭sec