Sometimes we want to collect user input in batch mode. That is, you can enter information for multiple model instances and submit them at a time. We call this table input (tabularinput) because these input items are usually...
Sometimes we want to collect user input in batch mode. That is, you can enter information for multiple model instances and submit them at a time. We call thisTabular input)Because these input items are usually presented as HTML tables.
To use table input, we first need to create or fill in an array of model instances, depending on whether we want to insert or update data. Then we start from$_POST
Extract user input data from the variable and assign it to each model. It is slightly different from single model input: we need to use$_POST['ModelClass'][$i]
Extract input data instead of using$_POST['ModelClass']
.
Public function actionBatchUpdate () {// assume that each item (Item) is an instance of the 'item' class, // extract the items to be updated in batch mode $ items = $ this-> getItemsToUpdate (); if (isset ($ _ POST ['item']) {$ valid = true; foreach ($ items as $ I => $ item) {if (isset ($ _ POST ['item'] [$ I]) $ item-> attributes =$ _ POST ['item'] [$ I]; $ valid = $ valid & $ Item-> validate ();} if ($ valid) // if all projects are valid //... perform some operations here} // display the View collection table input $ this-> render ('batchupdate', array ('items '=> $ items ));}
We have prepared this action, and we need to continue.batchUpdate
View to display input items in an HTML table.
Name |
Price |
Count |
Description |
$item): ?>
|
|
|
|
Note that we used"[$i]name"
Instead"name"
The second parameter used to call CHtml: activeTextField.
If any verification error occurs, the corresponding input items are automatically highlighted, just like the single model input we described earlier.
The above is the Yii Framework official guide series 20-using forms: collecting the content of table input in batches. For more information, see PHP Chinese website (www.php1.cn )!