Guest User

_form

a guest
Sep 7th, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.01 KB | None | 0 0
  1. <?php
  2.  
  3. use yii\helpers\Html;
  4. use yii\widgets\ActiveForm;
  5.  
  6. /* @var $this yii\web\View */
  7. /* @var $model app\models\Component */
  8. /* @var $form yii\widgets\ActiveForm */
  9. ?>
  10. <div class="component-form">
  11.     <div class="col-lg-5">
  12.         <?php $form = ActiveForm::begin(); ?>
  13.  
  14.         <?= $form->field($model, 'name')->textInput(['maxlength' => true, 'placeholder' => 'Enter component name']) ?>
  15.  
  16.         <?= $form->field($model, 'instock')->textInput(['maxlength' => true, 'placeholder' => 'Enter inventory amount']) ?>
  17.  
  18.  
  19.         <?=
  20.         $form->field($model, 'tractormodel_id')->dropDownList(
  21.                 $tractorModels, ['prompt' => ' - Select  Tractor Model - '])->label('Tractor Model')
  22.         ?>
  23.  
  24.         <h1>  Dependancies </h1>
  25.  
  26.         <div class="table-responsive">  
  27.             <div   class="input_fields_wrap">
  28.  
  29.                 <?php if (isset($depModels)) : ?>
  30.  
  31.                     <?php foreach ($depModels as $model) : ?>
  32.  
  33.                         <div class="col-md-12 ">
  34.  
  35.                             <div class="col-md-6 ">
  36.                                 <input type="hidden" class="deleteMe" name="ids[<?= $model->id ?>][dependentId]" value="<?= $model->id ?>" />
  37.                                 <select name="ids[<?= $model->id ?>][id]" class="form-control">
  38.  
  39.                                     <?php foreach ($components as $component) : ?>
  40.                                         <option value="<?= $component->id ?>"
  41.                                                 <?= $model->dependent_id == $component->id ? 'selected' : '' ?>><?= $component->name ?></option>
  42.                                             <?php endforeach; ?>
  43.                                 </select>
  44.                             </div>
  45.                             <div class="col-md-4 ">
  46.                                 <input type="text" name="ids[<?= $model->id ?>][quantity] " value="<?= $model->count ?>" class="form-control" placeholder="Enter amount" required  >
  47.                             </div>
  48.  
  49.                             <button class="btn btn-default remove_field "><span class="glyphicon glyphicon-trash"></span></button>
  50.                         </div>
  51.  
  52.                     <?php endforeach; ?>
  53.  
  54.                     <div class="col-md-12 ">
  55.                         <div class="col-md-6 ">
  56.                             <select name="ids[0][id]" class="form-control">
  57.                                 <option>- Select component -</option>
  58.                                 <?php foreach ($components as $component) : ?>
  59.                                     <option value="<?= $component->id ?>"> <?= $component->name ?></option>
  60.                                 <?php endforeach; ?>
  61.                             </select>
  62.                         </div>
  63.                         <div class="col-md-4 ">
  64.                             <input type="text" name="ids[0][quantity]" class="form-control" placeholder="Enter amount"  >
  65.                         </div>
  66.  
  67.  
  68.                         <div class="btn btn-default add_field_button"><span class="glyphicon glyphicon-plus"></span></div>
  69.  
  70.                     </div>
  71.  
  72.  
  73.  
  74.                 <?php else : ?>
  75.  
  76.                     <div class="col-md-12 ">
  77.                         <div class="col-md-6 ">
  78.                             <b> Dependent Component</b>
  79.                             <input type="hidden" class="deleteMe" name="ids[0][dependentId]" value="<?= $model->id ?>" />
  80.                             <select name="ids[0][id]" class="form-control">
  81.                                 <option>- Select component -</option>
  82.                                 <?php foreach ($components as $component) : ?>
  83.                                     <option value="<?= $component->id ?>"><?= $component->name ?></option>
  84.                                 <?php endforeach; ?>
  85.                             </select>
  86.                         </div>
  87.  
  88.                         <div class="col-md-4 ">
  89.                             <b> Amount Needed:</b>
  90.                             <input type="text" name="ids[0][quantity]" class="form-control" placeholder="Enter amount" required >
  91.                         </div>
  92.                         <div style="visibility: hidden"> b  </div>
  93.                         <div class="btn btn-default add_field_button"><span class="glyphicon glyphicon-plus"></span></div>
  94.                     </div>
  95.  
  96.                 <?php endif; ?>
  97.  
  98.             </div>
  99.         </div>
  100.  
  101.         <div id="deletedIds" ></div>
  102.  
  103.  
  104.  
  105.         <br>
  106.         <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
  107.     </div>
  108.     <?php ActiveForm::end(); ?>
  109. </div>
  110.  
  111. <script type="text/javascript">
  112.     $(document).ready(function () {
  113.         var max_fields = 50; //maximum input boxes allowed
  114.         var wrapper = $(".input_fields_wrap"); //Fields wrapper
  115.         var add_button = $(".add_field_button"); //Add button ID
  116.         var x = 1; //initlal text box count
  117.         add_button.click(function (e) { //on add input button click
  118.             e.preventDefault();
  119.             if (x < max_fields) { //max input box allowed
  120.                 x++; //text box increment
  121.                 wrapper.append('<div class="col-md-12 ">\
  122.                                     <div class="col-md-6">\
  123.                                         <input type="hidden" class="deleteMe" name="ids[' + x + '][dependentId]" value="" />\
  124.                                         <select name="ids[' + x + '][id]" class="form-control">\\n\
  125.                                                     <option>- Select component -</option>\
  126.                                         <?php foreach ($components as $component) : ?>\
  127.                                             <option value="<?= $component->id ?>"><?= $component->name ?></option>\
  128.                                         <?php endforeach; ?>\
  129.                                         </select>\
  130.                                     </div>\
  131.                                     <div class="col-md-4 ">\
  132.                                         <input type="text" name="ids[' + x + '][quantity]" class="form-control" placeholder="Enter amount" required>\
  133.                                     </div>\
  134.                                     <button class="btn btn-default  remove_field ">\n\
  135.                                     <span class="glyphicon glyphicon-trash"></span></button>\
  136.                                 </div>'
  137.                         );
  138.             }
  139.         });
  140.  
  141.  
  142.         wrapper.on("click", ".remove_field", function (e) {
  143.             var clickedId = $(this).parent().find('input[class="deleteMe"]').val();
  144.  
  145.             var deletedIdsArray = $('#deletedIDs');
  146.             console.log($('#deletedIDs'));
  147.             if (clickedId) {
  148.                 $('#deletedIds').append('<input type="hidden" name="deletedIds[]" value="' + clickedId + '">');
  149.             }
  150.             e.preventDefault();
  151.             $(this).parent('div').remove();
  152.             x--;
  153.         });
  154.     });
  155.  
  156. </script>
Add Comment
Please, Sign In to add comment