Some summary of the use of YII2 front-end tools

Data provider new fields

The data provider DataProviter provides data to the gridview. When the data provider is ActiveDataProvider, the data source is model. When additional fields are given in the model query, calling the extra fields in the gridview will prompt undefined. The solution is as follows

Data provider

$query = User::find()->select("*,count(id) as num")->groupBy(['uid']);//num为新增的额外字段
$dataProvider = new ActiveDataProvider(['query'=>$query]);

Open model, User.php, and add code in the file:public $num;

OK, then you can call num directly in the gridview like other attributes

Data filter new field

In addition to the data provider using the new field, the filter may also use the new field.

For example, when we filter data, we pass the time period parameter to the filter, but there is no such field in the data table, as follows

We first added a new act_time field in gridwiew and used it to filter

[
       'attribute' => 'act_time',
       'label'     => '选择请求时段',
       'class' => DateColumn::className(),
       'value' => function($data){
           //return $end_time;
           //return '';
       }
 ]

Then we go to the corresponding screening model and register the new field we defined, as follows

//Inherit the attributes of the parent model and add new attributes

public function attributes()
{
      return array_merge(parent::attributes(),['act_time']);
}
//设置新字段的验证规则
public function rules()
{
    return [
        [['id', 'uid', 'created_at'], 'integer'],
        [['path', 'method', 'statuscode', 'ip','phone','act_time'], 'safe'],
    ];
}

So we can use the new filter field in searchModel

Edit the backend file generated by gii

The backend files generated by gii and we meet our needs. What should I do when I need to introduce other js, css, or add js code directly on the page?

use$this->registerJsFile();

Bring in resources

$this->registerJsFile('static/js/echarts.min.js');
$this->registerJsFile('static/js/china.js');

Add js code (at the end of the file)

<?php
 $this->registerJs("
       alert('Hello world');
 ", \yii\web\View::POS_END);

gridview content and button parameters

  [
             //列名称
            'label' => '状态', 
            //该列对应的数据提供器中的字段。可以不填。填写的话,当value不设值时,会自动读取字段值
            'attribute' => 'last_a_time',
            //列内容的渲染状态,raw表示自定义html,还有image,text可选
            'format' => 'raw', 
            //设置表格头部样式
            'headerOptions' => ['width' => '100px','style'=>'text-align:center;'],
            //设置表格内容列样式
            'contentOptions' => ['style'=>'text-align:center;'],
            //自定义返回值
            'value' =>  function($model){
                return $model->status==0?'<span style="color:orange;">待回复</span>':'<span style="color:green;">已回复</span>';
            },
            //当visible值为真时,显示整列,为假,不显示整列
            ‘visible’	=> (1==2),
            //当该字段在searchmodel中设为筛选字段时,可以设置该字段的筛选样式
            //传false,不显示筛选。传数组,展示下拉框筛选
            'filter' => [0=>'待回复',1=>'已回复'],
            //设置筛选框的元素属性
            'filterInputOptions' => [
	            'id' => 'myselect',  //设置筛选框的id
	            'placeholder'=>'input的默认值',//筛选框是文本框时,设置占位字符
	            'prompt' => '全部'.//筛选框下拉框时,设置下拉框默认值
            ],
    ],
   [
            'class' => 'yii\grid\ActionColumn',
            'header'        => '状态',  //列名称
            'template'  => '{status}',  //定义按钮
            'headerOptions' => ['width' => '150px','style'=>'text-align:center;'],
            'contentOptions' => ['style'=>'text-align:center;'],
            'buttons'       => [
            	//试下按钮方法
                'status' => function($url, $model, $key){
                    $status = $model->status==1?'<span style="color:green;">正常</span>':'<span style="color:red;">禁用</a>';
                    $url = Url::to(['statusedit','id'=>$key]);
                    return '<a href="javascript:;" onclick="status_edit(\''.$url.'\',this)">'.$status.'</a>';
                }
            ]
        ],

The problem of using external variables when displaying content using custom functions in Yii2 GridView

Insert picture description here

ActiveForm

When you need to upload a file, you need to set the enctype attribute. The second parameter indicates whether to validate the form. When the uploaded file needs special processing, it is best to set this field to false

ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data', 'class' => 'submit-form']],false);

Guess you like

Origin blog.csdn.net/u012830303/article/details/108683767