Home  >  Article  >  PHP Framework  >  Three ways to use widgets in Yii views

Three ways to use widgets in Yii views

王林
王林forward
2021-02-19 10:07:462793browse

Three ways to use widgets in Yii views

The following summarizes three ways to use widgets in Yii views. I hope it can help everyone.

1. Display detailed information:

Three ways to use widgets in Yii views

2. Display the list, which can be conditionally restricted and paging:
Controller:

Three ways to use widgets in Yii views

View:

Three ways to use widgets in Yii views

## Subview:

getAttributeLabel('id')); ?>:// $data receives data from the parent view and uses getAttributeLabel() to get the label set in the model

id), array('view', 'id'=>$data->id)); ?>/ /Use $data->attribute to get the value.

3. Display the list, but the results will be displayed in the table

Three ways to use widgets in Yii views

Usage of loadModel in Yii: $this->loadModel($ modelId);

Generally in R/U/D operations, loadModel($modelId) is used. This is because at this time, when reading, updating and deleting, the id value of the model has been obtained through GET. You only need to Just load the model corresponding to the id.
But in C (create), there is no need to loadModel, but a new initialized model needs to be created.
After filling the attributes of the model respectively with the value of $_POST['Model'], you can save().

Different controllers in Yii use the same template:
In Yii's CRUD operation, C and U use the same subview. The difference is:
In operation C, all input values ​​are empty, and the newly created model is also in an initialized state and is ready to receive values ​​from POST.
In the U operation, all input values ​​need to be filled in through loadModel($id), and then prepare to receive updated values ​​from POST (some
values ​​have not been modified by the user);
Then , based on $model->isNewRecord(), determine whether the submit button displays C or U operation.
Finally use $model->save(); to store the record.

The way to render views in Yii:
Use $this->render() and $this->renderPartial() to render views;
The former generally renders an overall view, and in the overall view The latter may be used to render parts that need to be rendered repeatedly.
For example, a record list is generally displayed in indexAction. Since all records are rendered in the same way, sub-
views are used to render the same parts respectively. The index view only renders a large result, such as navigation bread crumbs, the number of records and
paging, etc.

From:

http://zhouyujiao163.blog.163.com/blog/static/48363054201161464156603/

Related recommendations:

yii framework

The above is the detailed content of Three ways to use widgets in Yii views. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete