php - Yii. Dynamically add row on AJAX action -
i have link in _form:
<?php echo chtml::link('add child', '#', array('id' => 'loadchildbyajax')); ?>
some code renderpartial child _form:
<div id="children"> <?php $index = 0; foreach ($model->goodsservices $id => $child): $this->renderpartial('goods/_form', array( 'model' => $child, 'index' => $id, 'display' => 'block' )); $index++; endforeach; ?> </div>
and script 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 controller:
public function actionloadchildbyajax($index) { $model = new appgoodsservices; $this->renderpartial('goods/_form', array( 'model' => $model, 'index' => $index, ), false, true); }
and @ 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 button being clicked, child _form should dynamically rendered, id doesn't work. miss here?
instead use 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 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'); } }); });
Comments
Post a Comment