Hi!
I want use Tabular Input for quick models creating in administration page. But I need that the count of model’s forms changed dynamically (by means of JQuery/JavaScript).
User open form for creating model and can press JavaScript-powered button "add item" to add one more model form.
Is any ready decision for it? May bee any extension?
joblo
(Joe)
September 6, 2012, 6:27pm
3
Take a look at the extension multimodelform.
It works with simple input elements (textfield, dropdown, checkboxlists, datepicker), but not with more complex jquery based components (select2, autocomplete…).
Thanks for replies!
But I decide use custom JQuery code to solve this problem such as this code.
<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>
<script type="text/javascript">
$(document).ready(function(){
var template = $('#all_answers .answer_form').clone();
var answersCount = 1;
window.addAnswer = function(){
answersCount++;
var answer = template.clone().find(':input').each(function(){
var newId = this.id.substring(0, this.id.length-1) + answersCount;
this.name = this.name.replace(/\[0\]/g,'['+(answersCount-1)+']');
newId = this.id.replace(/_0_/g,'_'+(answersCount-1)+'_');
$(this).prev().attr('for', newId);
this.id = newId;
}).end()
.attr('id', 'answer_form_' + (answersCount-1))
.appendTo('#all_answers')
.find('h3').text("Header №"+answersCount);
$('#Answer_'+(answersCount-1)+'_number').val(answersCount);
}
$('.add_answer_link').click(addAnswer);
});
</script>
//....
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'generate-answers-form',
'enableAjaxValidation'=>false,
)); ?>
<div id="all_answers">
<?php foreach($answers as $i=>$answer): ?>
<div class="answer_form">
<h3>Header №<?php echo ($i+1); ?></h3>
<?php echo $form->errorSummary($answer); ?>
<div class="row">
<?php echo $form->labelEx($answer,"[$i]number"); ?>
<?php echo $form->textField($answer,"[$i]number",array('size'=>4)); ?>
<?php echo $form->error($answer,"[$i]number"); ?>
</div>
<div class="row">
<?php echo $form->labelEx($answer,"[$i]content"); ?>
<?php echo $form->textArea($answer,"[$i]content",array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($answer,"[$i]content"); ?>
</div>
<div class="row">
<?php echo $form->labelEx($answer,"[$i]points"); ?>
<?php echo $form->textField($answer,"[$i]points"); ?>
<?php echo $form->error($answer,"[$i]points"); ?>
</div>
<hr>
</div>
<?php endforeach; ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Go!'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->