Yii. Динамически добавить строку в действие AJAX
У меня есть ссылка в _form:
<?php echo CHtml::link('Add Child', '#', array('id' => 'loadChildByAjax')); ?>
Некоторый код для рендеринга Partial 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 ответ
Вместо этого используйте виджет 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');
}
});
});