繁体   English   中英

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

[英]Yii. Dynamically add row on AJAX action

我在_form中有一个链接:

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

一些代码来渲染部分子窗体_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不起作用。 我在这里想念什么?

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

仅使用html,例如

$(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');
        }
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM