groc426
(Groc426)
March 3, 2011, 9:48pm
1
I have an array of dataproviders that I loop through and provide to the CListView widget. All of this data comes from one model/table (but many instances of the model). I wrap all these CListView widgets in one form. Each table data is an input field with the associated data. Of course when I post the data I only get the first record.
I’ve read through this: http://www.yiiframework.com/doc/guide/1.1/en/form.table but I can’t make it work with this situation.
My question: How would I retrieve and update this data in a batch? I will be eternally grateful for any help!! I’ve been beating my head for days on this.
Below is my code:
The View:
<?php echo CHtml::beginForm(); ?>
<?php foreach ($dataProvider as $data): ?>
<table>
<?php
$data->pagination->pageSize = $data->totalItemCount;
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $data,
'enablePagination' => FALSE,
'itemView' => '_tableView',
'template' => '{items}',
));
?>
</table>
<br>
<?php endforeach; ?>
<?php echo CHTML::button('Update'); ?>
<?php echo CHtml::endForm(); ?>
CListView View:
<tr>
<td><?php echo $data->account; ?></td>
<td><?php echo CHtml::TextField('description', $data->description); ?></td>
<td><?php echo CHtml::TextField('july', $data->july, 2); ?></td>
<td><?php echo CHtml::TextField('august', $data->august, 2); ?></td>
<td><?php echo CHtml::TextField('september', $data->september, 2); ?></td>
</tr>
jkofsky
(Jkofsky)
March 3, 2011, 11:44pm
2
groc426:
I have an array of dataproviders that I loop through and provide to the CListView widget. All of this data comes from one model/table (but many instances of the model). I wrap all these CListView widgets in one form. Each table data is an input field with the associated data. Of course when I post the data I only get the first record.
I’ve read through this: http://www.yiiframework.com/doc/guide/1.1/en/form.table but I can’t make it work with this situation.
My question: How would I retrieve and update this data in a batch? I will be eternally grateful for any help!! I’ve been beating my head for days on this.
Below is my code:
The View:
<?php echo CHtml::beginForm(); ?>
<?php foreach ($dataProvider as $data): ?>
<table>
<?php
$data->pagination->pageSize = $data->totalItemCount;
$this->widget('zii.widgets.CListView', array(
'dataProvider' => $data,
'enablePagination' => FALSE,
'itemView' => '_tableView',
'template' => '{items}',
));
?>
</table>
<br>
<?php endforeach; ?>
<?php echo CHTML::button('Update'); ?>
<?php echo CHtml::endForm(); ?>
CListView View:
<tr>
<td><?php echo $data->account; ?></td>
<td><?php echo CHtml::TextField('description', $data->description); ?></td>
<td><?php echo CHtml::TextField('july', $data->july, 2); ?></td>
<td><?php echo CHtml::TextField('august', $data->august, 2); ?></td>
<td><?php echo CHtml::TextField('september', $data->september, 2); ?></td>
</tr>
Just a quick thought: Maybe you could wrap with a dynamic <fieldset name="$data-> "> tag somehow and maybe that would give you an array to loop through in the saving routine.
groc426
(Groc426)
March 4, 2011, 4:53pm
3
I’ll have to give a try and report back.
jkofsky
(Jkofsky)
March 7, 2011, 5:53pm
4
groc426:
<snip>
<td><?php echo CHtml::TextField('july', $data->july, 2); ?></td>
<td><?php echo CHtml::TextField('august', $data->august, 2); ?></td>
<td><?php echo CHtml::TextField('september', $data->september, 2); ?></td>
</tr>
I would like to ask what the ‘2’ at the end of the CHtml::textField() calls is? The docs say it is supposed to be an array().
groc426
(Groc426)
March 12, 2011, 7:29pm
5
I apologize for my late response. The 2 was a simple copy & paste mistake. I’ve fixed the problem with code that looks like the following:
<?php echo CHtml::beginForm(); ?>
<?php foreach ($query as $i => $data): ?>
<table id="<?php echo $i; ?>">
<?php foreach ($data as $row): ?>
<tr>
<td><?php echo $row->account; ?></td>
<td><?php echo CHtml::activeTextField($row, "[$row->id]description"); ?></td>
<td><?php echo CHtml::activeTextField($row, "[$row->id]july"); ?></td>
<td><?php echo CHtml::activeTextField($row, "[$row->id]august"); ?></td>
<td><?php echo CHtml::activeTextField($row, "[$row->id]september"); ?></td>
</tr>
<?php endforeach; ?>
<tr>
<td>Account</td>
<td><input type="text" name="New[<?php echo $i; ?>][description]"></td>
<td><input type="text" name="New[<?php echo $i; ?>][july]"></td>
<td><input type="text" name="New[<?php echo $i; ?>][august]"></td>
<td><input type="text" name="New[<?php echo $i; ?>][september]"></td>
</tr>
</table>
<?php endforeach; ?>
<?php echo CHTML::button('Update', array('submit' => Yii::app()->createUrl("budget/updateRecords"))); ?>
<?php echo CHtml::endForm(); ?>
I wasn’t able to get it to work with the CListView. Instead I loop through the array of instances of the model, then I loop through each record set. I also add a blank row of inputs at the end of each table. Hope it can help someone in the future.