1

I have a link in _form:

<?php echo CHtml::link('Add Child', '#', array('id' => 'loadChildByAjax')); ?>

Some code to renderPartial child _form:

<div id="children">
    <?php
    $index = 0;
    foreach ($model->goodsservices as $id => $child):
        $this->renderPartial('goods/_form', array(
            'model' => $child,
            'index' => $id,
            'display' => 'block'
        ));
        $index++;
    endforeach;
    ?>
</div>

And script with AJAX:

<?php
Yii::app()->clientScript->registerScript('loadchild', '
var _index = ' . $index . ';
$("#loadChildByAjax").click(function(e){
e.preventDefault();
var _url = "' . Yii::app()->controller->createUrl("loadChildByAjax", array("load_for" => $this->action->id)) . '&index="+_index;
$.ajax({
    url: _url,
    success:function(response){
        $("#children").append(response);
        $("#children .crow").last().animate({
            opacity : 1, 
            left: "+50", 
            height: "toggle"
        });
    }
});
_index++;
});
', CClientScript::POS_END);
?>

In my controller:

public function actionLoadChildByAjax($index)
{
    $model = new AppGoodsServices;
    $this->renderPartial('goods/_form', array(
        'model' => $model,
        'index' => $index,
    ), false, true);
}

And at last me child _form:

<div style="margin-bottom: 20px; display: <?php echo!empty($display) ? $display : 'none'; ?>; 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' => 'deleteChild(this,     '.$index.'); return false;'));
    ?>
</div>
</div>
<div style="clear: both;"></div>
<?php
Yii::app()->clientScript->registerScript('deleteChild', "
function deleteChild(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);
?>

I need when my button being clicked, the child _form should be dynamically rendered, but id doesn't work. What do I miss here?

Decd
  • 65
  • 8
  • 3
    We need some more details. Why is it not working? Do you get an error in javascript (Check Firebug)? Do you get an PHP error (Check Firebug)? – Michiel Apr 23 '14 at 11:55
  • Thank you for response, mate, but due to so many problems encountered with this omgadble tabular input in yii and all my deadlines running out, I decided to use multimodelform again, even taking into account its imperfection, bugs and limitations. I really do hope that Yii2 would be better particularly in this moment. Thank you anyways! – Decd Apr 23 '14 at 20:00

1 Answers1

0

instead use the widget of yii

<?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'); ?>

use only html, byexample

$(function(){        
    $("#add").click(function(){

        if(5 > $(".attr").length) {
            var cycleBlock = '<input type="text" id="CategoryMstExt_0_attributes" name="CategoryMstExt['+i+'][attributes]" class="attrName'+i+'" maxlength="100" size="44"> ';
            var $cycleBlock = $(cycleBlock);
            $('#fields').append($cycleBlock);
            i++;
        } else {
            alert('Maximum attributes limit reached');
        }
    });
});
the_pete
  • 822
  • 7
  • 19