[英]How do I use ajax/jquery in yii to add rows to another table and use these values as foreign key in a form?
I have this form (I used crud generator and model generator): 我有这种形式(我使用了crud生成器和model生成器):
Create Pig 创建猪
[Name] [名称]
[Breed] [品种]
[Sickness] [Add sickness] [病] [加病]
How do I make add sickness create a new textfield everytime I click it? 我如何使每次点击都会产生新病? I already have a table for the multivalued sicknessid, I just need to access that table and add all the sicknesses, use one same id so I can use it as foreign key in the table.
我已经有一个用于多值疾病的表,我只需要访问该表并添加所有疾病,使用一个相同的ID,以便可以将其用作表中的外键。
<?php
/* @var $this PigController */
/* @var $model Pig */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'pig-form',
'enableAjaxValidation'=>true,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'name'); ?>
<?php echo $form->dropDownList($model,'name');?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'breed'); ?>
<?php echo $form->dropDownList($model,'breed', $model- >getBreedOptions()); ?>
<?php echo $form->error($model,'breed'); ?>
</div>
/*
ENTER CODE FOR SICKNESS HERE
*/
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
I also had the same scenario. 我也有同样的情况。 What i have done is with script.
我所做的是脚本。 Try this.
尝试这个。 It worked for me.
它为我工作。 Please do make necessary changes
请进行必要的更改
$(function(){
$("#add").click(function(){
if(5 > $(".attr").length) {
var cycleBlock = '<tr id="item'+i+'">';
cycleBlock += '<td style="width: 447px;"> <label for="CategoryMstExt_Attribute">Attribute</label> <input type="text" id="CategoryMstExt_0_attributes" name="CategoryMstExt['+i+'][attributes]" class="attrName'+i+'" maxlength="100" size="44"> </td> <td> <label for="CategoryMstExt_Data_Type">Data Type</label> <select id="CategoryMstExt_'+i+'_datatype" name="CategoryMstExt['+i+'][datatype]" class="attr" onchange="javascript:checkSelection(this.value,'+i+')"> <option value="17">Textarea</option> <option value="16">Textbox</option> <option value="18">Listbox</option> </select><img onclick="deleteElm(item'+i+'.id);" src="<?php echo Yii::app()->request->baseUrl; ?>/assets/81ff99cf/gridview/delete.png" alt=""> </td>';
cycleBlock += '</tr>';
var $cycleBlock = $(cycleBlock);
$('#fields').append($cycleBlock);
i++;
} else {
alert('Maximum attributes limit reached');
}
});
});
<?php
<img style="margin-left:600px;" id="add" src="<?php echo Yii::app()->request->baseUrl; ?>/media/images/add.png">
<table id="fields" >
<tr id="item1" >
<td style="width: 447px;">
<?php echo $form->labelEx($model, 'Attribute'); ?>
<?php echo $form->textField($model, '[0]attributes', array('size' => 44, 'maxlength' => 100, 'class' => 'attrName0')); ?>
</td>
<td >
<?php echo $form->labelEx($model, 'Data Type'); ?>
<?php echo $form->dropDownList($model, '[0]datatype', Array('17' => 'Textarea', '16' => 'Textbox', '18' => 'Listbox'), array('onChange' => 'javascript:checkSelection(this.value,0)', 'class' => 'attr')); ?>
<img onclick="deleteElm(item1.id);" src="<?php echo Yii::app()->request->baseUrl; ?>/media/images/delete_2.png" alt="">
</td>
</tr>
</table>
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.