Advertisement
joris

Form Dinamic (jQuery Append)

Oct 10th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.99 KB | None | 0 0
  1. <style>
  2. .small{
  3.     width:130px;
  4. }
  5. .big{
  6.     width:220px;
  7. }
  8. .btn{
  9.     line-height: 15px;
  10. }
  11. .mini{
  12.     width:40px;
  13. }
  14. </style>
  15.  
  16. <script>
  17. $(document).ready(function(e) {
  18.     var count = 1;
  19.    
  20.     /*$(".mini").spinedit({
  21.         minimum: 0,
  22.         maximum: 1000,
  23.         step: 1
  24.     });*/
  25.    
  26.     DataProvideType();
  27.    
  28.     $(document).on('click', '.btn-danger', function() {
  29.         var tableData = $('#customFields tr').length;
  30.         var total = parseInt(tableData)-1;
  31.         alert(total);
  32.     });
  33.    
  34.     $("#customFields").on('change', '.small', function() {
  35.         var $this = $(this),
  36.         id = $this.val();
  37.         var prev = $this.parents('tr').children('td').eq(0).children().val();
  38.         if(prev==1){
  39.             if(id==0){
  40.                 $this.parents('tr').children('td').eq(2).children().val('');
  41.             }else{
  42.                 $.ajax({
  43.                     type: 'POST',
  44.                     url: '<?php echo Yii::app()->createAbsoluteUrl("distributionplan/ambilassetmap"); ?>',
  45.                     dataType: 'json',
  46.                     data:{
  47.                         id: id
  48.                     },
  49.                     cache: false,
  50.                     success: function (result) {
  51.                         $this.parents('tr').children('td').eq(2).children().val(result[0].total);
  52.                     },
  53.                     error: function(jqXHR, exception){
  54.                         alert('Error : Data Count');
  55.                     }
  56.                 });
  57.             }
  58.         }
  59.     });
  60.    
  61.     $("#customFields").on('change', '.big', function() {
  62.         var $this = $(this),
  63.         id = $this.val();
  64.         $this.parents('tr').children('td').eq(1).children().empty();
  65.         if(id==1){
  66.             $this.parents('tr').children('td').eq(2).children().val('');
  67.             $this.parents('tr').children('td').eq(2).children().attr("readonly","readonly");
  68.             $.ajax({
  69.                 type: 'GET',
  70.                 url: '<?php echo Yii::app()->createAbsoluteUrl("distributionplan/ambilpackage"); ?>',
  71.                 dataType: 'json',
  72.                 cache: false,
  73.                 success: function (result) {
  74.                     $this.parents('tr').children('td').eq(1).children().append('<option value="0">-- Pilih --</option>');
  75.                     $.each(result, function() {
  76.                         $this.parents('tr').children('td').eq(1).children().append($('<option>', {
  77.                             value: this.id,
  78.                             text : this.package_number
  79.                         }));
  80.                     });
  81.                 },
  82.                 error: function(jqXHR, exception){
  83.                     alert('Error : Query Package Data');
  84.                 }
  85.             });
  86.         }else if(id==2){
  87.             $this.parents('tr').children('td').eq(2).children().val('');
  88.             $this.parents('tr').children('td').eq(2).children().removeAttr("readonly");
  89.             $.ajax({
  90.                 type: 'GET',
  91.                 url: '<?php echo Yii::app()->createAbsoluteUrl("distributionplan/ambiladditional"); ?>',
  92.                 dataType: 'json',
  93.                 cache: false,
  94.                 success: function (result) {
  95.                     $this.parents('tr').children('td').eq(1).children().append('<option value="0">-- Pilih --</option>');
  96.                     $.each(result, function() {
  97.                         $this.parents('tr').children('td').eq(1).children().append($('<option>', {
  98.                             value: this.id,
  99.                             text : this.additional_asset_name
  100.                         }));
  101.                     });
  102.                 },
  103.                 error: function(jqXHR, exception){
  104.                     alert('Error : Query Additional Data');
  105.                 }
  106.             });
  107.         }else{
  108.             $this.parents('tr').children('td').eq(2).children().val('');
  109.             $this.parents('tr').children('td').eq(2).children().attr("readonly","readonly");
  110.             $this.parents('tr').children('td').eq(1).children().empty();
  111.         }
  112.     });
  113.    
  114.     $(".btn.btn-warning").click(function(){
  115.         count += 1;
  116.         var $row = $(
  117.         '<tr valign="top">'
  118.             + '<td>'+'</td>'
  119.                 + '<td><select class="small" id="data2_'+count+'" name="data2_'+count+'"></select></td>'
  120.                 + '&nbsp;'
  121.                 + '<td><input type="text" class="mini" id="data3_'+count+'" name="data3_'+count+'" autocomplete="off"/></td>'
  122.                 + '&nbsp;'
  123.                 + '<td><input type="text" class="mini" id="data4_'+count+'" name="data4_'+count+'" autocomplete="off"/>K+</td>'
  124.                 + '&nbsp;'
  125.                 + '<td><input type="text" class="mini" id="data5_'+count+'" name="data5_'+count+'" autocomplete="off"/></td>'
  126.                 + '&nbsp;'
  127.                 + '<td><input id="rows_' + count + '" name="rows[]" value="'+ count +'" type="hidden">'
  128.                 + '<a href="javascript:void(0);" class="remCF">Remove</a>'
  129.         + '</tr>').appendTo("#customFields");
  130.        
  131.         var copyData = $("#data1_1").clone();
  132.         var repID = copyData.attr('id', 'data1_'+count+'');
  133.         var repName = copyData.attr('name', 'data1_'+count+'');
  134.  
  135.         $row.find('td:first').append(repID);
  136.        
  137.         $("html, body").animate({ scrollTop: $(document).height() }, "fast");
  138.     });
  139.    
  140.     $("#customFields").on('click','.remCF',function(){
  141.         $(this).parent().parent().remove();
  142.         count -= 1;
  143.     });
  144.    
  145. });// Tutup Document Ready
  146.  
  147. function DataProvideType(){
  148.     $.ajax({
  149.         type: 'GET',
  150.         url: '<?php echo Yii::app()->createAbsoluteUrl("distributionplan/ambildefault"); ?>',
  151.         dataType: 'json',
  152.         cache: false,
  153.         success: function (result) {
  154.             $("#data1_1").append('<option value="0">-- Pilih --</option>');
  155.             $.each(result, function() {
  156.                 $("#data1_1").append($('<option>', {
  157.                     value: this.id,
  158.                     text : this.distribution_item_type
  159.                 }));
  160.             });
  161.         },
  162.         error: function(jqXHR, exception){
  163.             alert('Error : Data Provide Type');
  164.         }
  165.     });
  166. }
  167.  
  168.  
  169. </script>
  170.  
  171. <div class="formSep">
  172.                
  173.         <label>
  174.         <?php echo $form->labelEx($model,'distribution_plan_number'); ?>
  175.         </label>
  176.        
  177.         <?php echo $form->textField($model,'distribution_plan_number',array(
  178.             'size'=>60,
  179.             'maxlength'=>100,
  180.             'readonly'=>'readonly'
  181.         )); ?>
  182.         <span class="help-inline">
  183.         <?php echo $form->error($model,'distribution_plan_number'); ?>
  184.         </span>
  185.    
  186.    
  187.    
  188.         <label>
  189.         <?php echo $form->labelEx($model,'distribution_destination'); ?>
  190.         </label>
  191.        
  192.         <?php
  193.             $models = DistributionDestination::model()->publish()->findAll();
  194.             $list = CHtml::listData($models, 'id', 'distribution_destination');
  195.             echo $form->dropDownList($model, 'distribution_destination', $list);
  196.         ?>
  197.         <span class="help-inline">
  198.         <?php echo $form->error($model,'distribution_destination'); ?>
  199.         </span>
  200.    
  201.    
  202.    
  203.         <label>
  204.         <?php echo $form->labelEx($model,'destination_ro'); ?>
  205.         </label>
  206.        
  207.         <?php
  208.             $models = Region::model()->publish()->findAll();
  209.             $list = CHtml::listData($models, 'id', 'region_name');
  210.             echo $form->dropDownList($model, 'destination_ro', $list);
  211.         ?>
  212.         <span class="help-inline">
  213.         <?php echo $form->error($model,'destination_ro'); ?>
  214.         </span>
  215.    
  216.    
  217.    
  218.         <label>
  219.         <?php echo $form->labelEx($model,'destination_ic'); ?>
  220.         </label>
  221.        
  222.         <?php
  223.             $models = Partner::model()->publish()->findAll();
  224.             $list = CHtml::listData($models, 'id', 'partner_name');
  225.             echo $form->dropDownList($model, 'destination_ic', $list);
  226.         ?>
  227.         <span class="help-inline">
  228.         <?php echo $form->error($model,'destination_ic'); ?>
  229.         </span>
  230.    
  231.    
  232.    
  233.         <label>
  234.         <?php echo $form->labelEx($model,'expedition'); ?>
  235.         </label>
  236.        
  237.         <?php
  238.             $models = Expedition::model()->onlylogged()->findAll();
  239.             $list = CHtml::listData($models, 'id', 'expedition_name');
  240.             echo $form->dropDownList($model, 'expedition', $list);
  241.         ?>
  242.         <span class="help-inline">
  243.         <?php echo $form->error($model,'expedition'); ?>
  244.         </span>
  245.    
  246.    
  247.    
  248.    
  249.         <label>
  250.         <?php echo $form->labelEx($model,'published'); ?>
  251.         </label>
  252.        
  253.         <?php echo  
  254.             $form->dropDownList($model,'published',array('0'=>'No','1'=>'Yes'),
  255.             array('options' => array('1'=>array('selected'=>true))));
  256.         ?>
  257.         <span class="help-inline">
  258.         <?php echo $form->error($model,'published'); ?>
  259.         </span>
  260.    
  261.    
  262.         <label>
  263.         <?php echo $form->labelEx($model,'activate'); ?>
  264.         </label>
  265.        
  266.         <?php echo  
  267.             $form->dropDownList($model,'activate',array('0'=>'No','1'=>'Yes'),
  268.             array('options' => array('1'=>array('selected'=>true))));
  269.         ?>
  270.         <span class="help-inline">
  271.         <?php echo $form->error($model,'activate'); ?>
  272.         </span>
  273.    
  274. <br /><br />
  275.  
  276. <h3 class="heading">Distribution Item</h3>
  277.    
  278. <table class="form-table" id="customFields">
  279.     <tr valign="top" style="text-align:center;">
  280.         <td style="font-weight:bold;">Type</td>
  281.         <td style="font-weight:bold;">Item</td>
  282.         <td style="font-weight:bold;">Count</td>
  283.         <td style="font-weight:bold;">Alocation</td>
  284.     </tr>
  285.     <tr valign="top">
  286.         <td>
  287.         <select class="big" id="data1_1" name="data1_1"></select>
  288.         <input type="hidden" value="1" name="rows[]" id="rows1_1">
  289.         </td>
  290.         <td><select class="small" id="data2_1" name="data2_1"></select></td>
  291.         <td><input type="text" class="mini" id="data3_1" name="data3_1" autocomplete="off"/></td>
  292.         <td><input type="text" class="mini" id="data4_1" name="data4_1" autocomplete="off"/>K+</td>
  293.         <td><input type="text" class="mini" id="data5_1" name="data5_1" autocomplete="off"/></td>
  294.     </tr>
  295. </table>
  296. <a href="javascript:void(0);" class="btn btn-warning">Tambah Data</a>
  297. <!--<a href="javascript:void(0);" class="btn btn-danger">Cek</a>-->
  298. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement