


Yii2的相关学习记录,Gridview小部件使用及kartik-v/yii2-grid扩展(五),yii2gridview_PHP教程
Yii2的相关学习记录,Gridview小部件使用及kartik-v/yii2-grid扩展(五),yii2gridview
现在记录下Gridview的相关内容,也是强迫症犯了,Yii2自带的Gridview虽然不错,但是过滤栏如果一些字段用不着,不会自动合并成一行,当然也可以过滤栏不用,而是在最上方自己写一些需要检索的数据,但是这样很麻烦,还要自己去规划样式,写检索什么的。正好在搜索将检索栏和标题合并时,看到了
Data column(默认)、Action column(操作)、Checkbox column(可选中)、Serial column(带序列号)
2、标题名字会根据Model中的attributeLabels()方法,来自动替换成对应的中文。当然也可以通过‘label’来自己定义,而‘attribute’则是指定根据哪个字段排序。其它的像是'visible'来隐藏显示,'header'显示头部内容,'headerOptions'来定义css或style样式等等。
<span>[ </span>'label'=>'你想要的名称', 'attribute'=>'id',<span>//</span><span>可以排序的字段</span> <span>] </span>
3、自定义显示页数和排序字段,如果用到了searchModel,则需要在searchModel的search方法中定义,否则,需要在Controller中定义:
<span>$dataProvider</span> = <span>new</span><span> ActiveDataProvider([ </span>'query' => <span>$query</span>, 'pagination' =><span> [ </span>'pageSize' => 15,<span>//</span><span>如果不定义,默认为20</span> ], 'sort' => ['attributes' => ['id']],<span>//</span><span>如果定义,则只能按照id来排序,否则所有字段都可以</span> ]);
4、下拉菜单检索,好比根据下拉菜单检索用户状态,则需要先在model中定义相关的方法,然后在Gridview中再做处理:
<span>//</span><span>Model中,定义一个静态方法</span> <span>const</span> STATUS_DELETED = 0<span>; </span><span>const</span> STATUS_ACTIVE = 10<span>; </span><span>public</span> <span>static</span> <span>function</span> getZhStatus(<span>$status</span>=<span>false</span><span>){ </span><span>$status_array</span>=<span> [ </span>''=>'请选择',<span> self</span>::STATUS_DELETED=>'禁止',<span> self</span>::STATUS_ACTIVE=>'正常'<span> ]; </span><span>return</span> <span>$status</span>==<span>false</span>?<span>$status_array</span>:ArrayHelper::getValue(<span>$status_array</span>,<span>$status</span>,'未知'<span>); } </span><span>//</span><span>Gridview中</span> <span>[ </span>'attribute' => 'status', 'filter'=>Html::activeDropDownList(<span>$searchModel</span>,'status',User::getZhStatus(),['class' => 'form-control ']), 'value'=><span>function</span>(<span>$data</span><span>){ </span><span>return</span> User::getZhStatus(<span>$data</span>-><span>status); }</span>,<span> ]</span>,
5、格式化时间,有好几种方法:
方法一:类似上面的定义回调函数
<span>[ </span>'attribute'=>'created_at', 'value'=><span>function</span>(<span>$data</span><span>){ </span><span>return</span> <span>date</span>('Y-m-d',<span>$data</span>-><span>created_at); }</span>,<span> ]</span>
方法二:用Gridview自带的format配置
<span>[ </span>'attribute'=>'created_at', 'format'=>['date','php:Y-m-d'], 'value'=>'created_at',<span> ]</span>,
方法三:首先在config中配置好你的时间格式,否则会是英文格式的,然后再在Gridview中处理。
<span>//</span><span>在common/config/main.php中定义自己的时间、金钱等的格式</span> 'components' =><span> [ </span>'formatter' =><span> [ </span>'dateFormat' => 'yyyy-MM-dd', 'datetimeFormat' => 'yyyy-MM-dd HH:mm:ss', 'decimalSeparator' => ',', 'thousandSeparator' => ' ', 'currencyCode' => 'CNY',<span> ]</span>,<span> ]</span>,
这里用到了快速写的一种方式:“attribute:format:label”(属性:格式:标签)这种格式,详情点击这里的api文档。所以我们这样就可以了:(如果定义了上方,则方法二可以直接'format'=>'date'即可以正确显示)
'created_at:date',
6、显示超链接或图片,都是用的回调函数的方法,这里以超链接为例,注意format需要为'raw’,不对结果做任何格式化处理,具体的格式化方面可以点这里看看。
<span>[ </span>'attribute'=>'id', 'value'=><span>function</span>(<span>$model</span>, <span>$key</span>, <span>$index</span>, <span>$column</span><span>){ </span><span>return</span> Html::a(<span>$model</span>->id,['user/view','id'=><span>$model</span>->id],['class' => 'profile-link','target'=>'_blank'<span>]); }</span>, 'format' => 'raw',<span> ]</span>,
7、关联表单显示,这个地方直接看指南吧,点这里看,总之就是如果管理表单设置好后,直接用类似order.name这种就可以直接显示使用,如果想要检索排序,则需要在searchModel的rulers规则方法和attributes属性方法中将此字段写入,在search方法中添加andFilterWhere的检索,以及用$query->joinWith关联字段,$dataProvider->sort->attributes[]添加排序等。
8、自定义Action Column按钮,
<span>[ </span>'class' => 'yii\grid\ActionColumn', 'header'=>'操作', 'headerOptions'=>['width'=>'120'], 'template' => '{view} {update} {delete} {forbid} ', 'buttons'=><span>[ </span>'forbid'=><span>function</span>(<span>$url</span>,<span>$model</span><span>){ </span><span>return</span> Html::<span>a('<i class="glyphicon glyphicon-remove-circle"></i>',['user/forbid','id'=>$model->id]); } ]</span>,<span> ]</span>
以上是Yii2自带的Gridview的用法。再说下
,官方文档及demo讲的比较全了,它比原生的多了几个列的形式,例如Editable Column(可编辑)、Radio Column(单选框)等,还多了一些其它功能,例如滚动时可以固定标题栏方便查看、可以总计、导出svn,excel等格式。这里稍微说下:一开始安装完成后可能会报错pdf错误,因为这些关联的还没有安装,可以按照提示安装也可以先配置'export'=>false来取消导出功能,下面导出时会详细配置。
1、整体结构如下图:

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.