grid的使用笔记 [ 2.0 版本 ]
个人博文
请访问:诗意的边缘
或者直接访问 http://www.alan51.com
grid是最常用的列表生成工具;
[控制器代码]
public function actionIndex() {
$searchModel = new OrderSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'dataProvider'=>$dataProvider,
'searchModel'=> $searchModel
]);
}
[模型代码]
public function search($params)
{
$query = Order::find()->select('id,name,mobile,create_time,paid_time');
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination'=>[
'pagesize'=>10
]
]);
$this->load($params, '');
if (!$this->validate()) {
return $dataProvider;
}
$query->andOnCondition(['like', 'name', $this->name]);
$query->andFilterWhere([
'id' => $this->id,
'mobile' => $this->mobile ,
'create_time' => $this->create_time,
'paid_time' => $this->paid_time,
]);
return $dataProvider;
}
[视图代码]
php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel app\modules\manage\models\OrderSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = '列表';
$this->registerCssFile('/css/manage/common/bootstrap.min.css');
$this->registerJsFile('/js/common/jquery-1.8.0.min.js');
?>
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<?= GridView::widget([
'tableOptions' => ['class' => 'table table-hover'],
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'class' => 'yii\grid\SerialColumn',
'header'=>'编号'
],
[
'attribute' => 'id',
'label'=>'订单号',
'format' => 'text',
'options' => [
'width' => 75,
]
],
[
'attribute' => 'name',
'label'=>'姓名',
],
[
'attribute' => 'mobile',
'label'=>'手机号',
],
[
'attribute' => 'create_time',
'label'=>'下单时间',
'value'=>function($model) {
return Yii::$app->formatter->asDate($model->create_time, 'php:Y-m-d H:i:s');
}
],
[
'attribute' => 'paid_time',
'label'=>'支付时间',
'value'=>function($model) {
return $model->paid_time ? Yii::$app->formatter->asDate($model->paid_time) : '--';
}
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{update}',
'buttons' => [
'update' => function ($url, $model, $key) {
return Html::a('修改', ['update', 'id' => $key], ['class'=>'btn btn-sm btn-danger']);
}
],
'options' => [
'width' => 5
]
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{view}',
'buttons' => [
'view' => function ($url, $model, $key) {
return Html::a('显示', ['view', 'id' => $key], ['class'=>'btn btn-sm btn-info']);
}
],
'options' => [
'width' => 5
]
],
[
'class' => 'yii\grid\ActionColumn',
'template' => '{delete}',
'buttons' => [
'delete' => function ($url, $model, $key) {
return Html::a('删除', ['delete', 'id' => $key], ['class'=>'btn btn-sm btn-primary']);
}
],
'options' => [
'width' => 5
]
]
],
]); ?>
更多原文技术性的内容请访问我的博客:诗意的边缘 或者 访问:http://www.alan51.com
alan51
注册时间:2015-08-24
最后登录:2018-09-28
在线时长:8小时43分
最后登录:2018-09-28
在线时长:8小时43分
- 粉丝5
- 金钱125
- 威望60
- 积分805
共 0 条评论