The following summarizes three ways to use widgets in Yii views. I hope it can help everyone.
1. Display detailed information:
2. Display the list, which can be conditionally restricted and paging:
Controller:
View:
## 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.
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.
http://zhouyujiao163.blog.163.com/blog/static/48363054201161464156603/
Related recommendations: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!