简体   繁体   中英

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):

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.

<?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>  
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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