Hello everyone,
I’ve read almost all guides and docs conserning tabular input, but I still can’t figure out how to make it work finally (a lot of work done, so I don’t really ask global and abstract questions).
Need to say, I don’t use extensions like multimodelform, because they are still little buggy and don’t fulfill all my needs (such as “AT LEAST ONE” row should contain info, for example). The only thing I use is: esaverelatedbehavior . It really makes my life easier.
So, what is allready done:
Application model:
-
id
-
last_name
-
… 50+ fields
AppGoodsServices model:
-
id
-
goods_and_services
-
percentage
-
applicant_id
Relations are application HAS_MANY goods/servcies; goods/services BELONGS_TO application (under applicant_id).
My controller:
public function actionCreate()
{
$model=new Application;
$this->performAjaxValidation($model);
if(isset($_POST['Application']))
{
$model->attributes=$_POST['Application'];
if(isset($_POST['AppGoodsServices']))
{
$model->goodsservices = $_POST['AppGoodsServices'];
}
if ($model->saveWithRelated('goodsservices'))
{
$this->redirect(array('view', 'id' => $model->id));
} else {$model->addError('goodsservices', 'Error occured while saving goods/services.'); }
}
$this->render('create',array(
'model'=>$model,
));
}
public function actionUpdate($id)
{
$model=$this->loadModel($id);
$this->performAjaxValidation($model);
if(isset($_POST['Application']))
{
$model->attributes=$_POST['Application'];
if(isset($_POST['AppGoodsServices']))
{
$model->goodsservices = $_POST['AppGoodsServices'];
} else {
}
if ($model->saveWithRelated('goodsservices'))
{
$this->redirect(array('view', 'id' => $model->id));
} else {$model->addError('goodsservices', 'Error occured while saving goods/services.'); }
}
$this->render('update',array(
'model'=>$model,
));
}
_form.php:
// fields fields fields
<div class="goodsservices">
<?php
$index = 0;
foreach ($model->goodsservices as $id => $goods):
$this->renderPartial('goods/_form', array(
'model' => $goods,
'index' => $id,
));
$index++;
endforeach;
?>
</div>
// fields fields fields
and goods/_form.php
<div style="margin-bottom: 20px; width:100%; clear:left;" class="crow">
<div class="row" style="float: left;">
<?php echo CHtml::activeLabelEx($model, '['.$index.']goods_and_services'); ?>
<?php echo CHtml::activeTextField($model, '['.$index.']goods_and_services', array('size' => 30, 'maxlength' => 150)); ?>
<?php echo CHtml::error($model, '['.$index .']goods_and_services'); ?>
</div>
<div class="row" style="float: left;">
<?php echo CHtml::activeLabelEx($model, '['.$index .']percentage'); ?>
<?php echo CHtml::activeTextField($model, '['.$index.']percentage', array('size' => 5)); ?>
<?php echo CHtml::error($model, '['.$index.']percentage'); ?>
</div>
<div style="clear: both;"></div>
<div class="row">
<?php echo CHtml::link('Delete', '#', array('onclick' => 'deleteGoods(this, '.$index.'); return false;'));
?>
</div>
</div>
<div style="clear: both;"></div>
<?php
Yii::app()->clientScript->registerScript('deleteGoods', "
function deleteGoods(elm, index)
{
element=$(elm).parent().parent();
/* animate div */
$(element).animate(
{
opacity: 0.25,
left: '+=50',
height: 'toggle'
}, 500,
function() {
/* remove div */
$(element).remove();
});
}", CClientScript::POS_END);
All this stuff NOW shows me all related goods in actionUpdate (I’ve MANUALLY ADDED SOME TEST ITEMS INTO DB WITH REQUIRED applicant_id) and I can remove any be clicking DELETE.
[b]My problem is how to show the first empty row on actionCreate and a button near "ADD MORE ROW" to dynamically add some rows to save it all in a batch?
One thing also is that we need that for some "children" we need at least one row to be added. Hope for your help, thank you![/b]