Yii cgridview basic use (I) First show their skills

Source: Internet
Author: User

In the automatically generated yii code, we can always see the cgridview figure on the admin interface. This is a very useful table control for displaying data, which can significantly speed up development. Next let's explore the basic use of cgridview:


For simplicity, we use the blog example in yii demo to modify the code. First, here are some modified MySQL statements:

Drop table if exists 'tbl _ user'; Create Table tbl_user ('user _ id' integer not null auto_increment comment 'Primary key', 'username' varchar (128) not null comment 'username', 'nickname' varchar (128) not null comment' nickname ', 'Password' varchar (128) not null comment 'Password ', 'email 'varchar (128) not null comment' mailbox ', 'is _ delete' tinyint not null default 0 comment' Delete flag', unique key ('username '), primary Key ('user _ id') engine = InnoDB default charset = utf8 comment = 'user table'; drop table if exists 'tbl _ post '; create Table tbl_post ('Post _ id' integer not null auto_increment comment 'Primary key', 'title' varchar (128) not null comment 'title ', 'content' text not null comment' Article content', 'tags' text comment' tags', 'status' integer not null comment' status, 0 = draft, 1 = approved, -1 = rejected review, 2 = release', 'create _ time' integer comment 'creation time', 'Update _ time' integer comment' update time ', 'author _ id' integer not null comment' author ', 'is _ delete' tinyint not null default 0 comment' Delete flag', constraint 'Post _ ibfk_1 'foreign key (author_id) references tbl_user ('user _ id') on Delete cascade on update restrict, primary key ('Post _ id') engine = InnoDB default charset = utf8 comment = 'Log table ';

Two tables store the author information and one storage log. The log has a foreign key associated with the user. The is_delete field in the two tables indicates whether the record is deleted, 0 indicates not deleted, and 1 indicates deleted. Let's take a look at the relation method of the post class generated using Gii:

 
/*** @ Return array relational rules. */public function relations () {// Note: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array ('comments' => array (SELF: has_comment, 'comment', 'Post _ id'), 'author' => array (SELF: belongs_to, 'user', 'author _ id '),);}
The author foreign key exists as the belongs_to relationship, which meets our expectation.

Let's take a look at the cgridview code in Admin. php In the automatically generated post:

<? PHP $ this-> widget ('zii. widgets. grid. cgridview ', array ('id' => 'post-grid', 'dataprovider' => $ model-> Search (), 'filter' => $ model, 'columns '=> array ('Post _ id', 'title', 'content', 'tags', 'status', 'create _ time ', 'Update _ time', 'author _ id', 'is _ delete', array ('class' => 'cbuttoncolumn',),);?>

Look! Although we did not write anything, this is the most basic use of this control. Dataprovider is the data provided by the search function in the model, filter... currently, this function is unavailable. Columns controls each column displayed. The cbuttoncolumn of the last item shows us three buttons: viewing updates and deleting.

Next we will make a little transformation.


Use cgridview to show the real data format:

Most of the time, what is in the database is not suitable for direct display to the user, we need to make some processing before it is suitable for reading. However, without modification, the cgridview will only present the database value intact, so we should modify the corresponding field. For example, the is_delete field stores 0 and 1 in the database, but reading here is not very good. We should change it to 1 to display 'yes' and 0 to show 'no '. Let's take a look at the following code. We use an array. The two keys are name and value, respectively. The field corresponding to name needs to be filled in with the model, and value is the data you want to display, you can write a PHP statement as the executable code. Do you think we can do a lot for this value? Some may ask, if the code I want to execute is long, do they all write in the value ?... I said, classmate, won't you write a function elsewhere and then call it here ??

 
<? PHP $ this-> widget ('zii. widgets. grid. cgridview ', array ('id' => 'post-grid', 'dataprovider' => $ model-> Search (), 'filter' => $ model, 'columns '=> array ('Post _ id', 'title', 'content', 'tags', 'status', 'create _ time ', 'Update _ time', 'author _ id', 'is _ delete', array ('name' => 'is _ delete ', 'value' => 'is _ delete? "Yes": "no" '// value can execute PHP statements.) array ('class' => 'cbuttoncolumn',),);?>
In addition, there are some common options that can be filled in the array. The following is a common usage (other part of the code is omitted ):

 
Array ('name' => 'is _ delete', 'value' => 'is _ delete? "Yes": "no" '// value indicates that PHP statements can be executed. 'filter' => array (0 => 'no', 1 =>' Yes '), // customize the search filtering method. Here, the drop-down menu 'htmlopexception' => array ('class' => 'delete') is set to yes or no '), // html options can be defined. Here, a class with a delete is defined ),
If we use name above, it is the original field in the model. If we want to display our new content, use the header:

 
Array ('header' => 'note', 'value' => 'display your data '),


PS: Here I will only talk about the common parts. For details, see the document: cgridview

basic use of yii cgridview (1) show your skills

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.