Yii的。在AJAX操作上动态添加行

时间:2014-04-23 11:02:34

标签: php ajax yii tabular yii-relations

我在_form中有一个链接:

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

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>

使用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);
?>

在我的控制器中:

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

最后我的孩子_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);
?>

我需要在单击按钮时,应该动态呈现子_form,但id不起作用。我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

而是使用yii的小部件

$value = $_COOKIE['whatever'];
setcookie('whatever', $value, time() + (86400 * 3), "/");

仅使用html,byexample

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