Advertisement
Guest User

Append Issue

a guest
Jan 10th, 2023
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 3.83 KB | None | 0 0
  1.  <form method="POST" enctype="multipart/form-data">
  2.     {% csrf_token %}
  3.     {{ form_set.management_form }}
  4.     <table class="table-striped" id="dynamic_form_table">
  5.       <tbody>
  6.       {% for aspect_form in form_set.forms %}
  7.         <tr class="dynamic-form">
  8.           <td class="col-sm-10">
  9.             {% form form=aspect_form %}
  10.             {% endform %}
  11.           </td>
  12.           <td id="delete_column">
  13.             <button id="remove-{{ aspect_form.prefix }}-row"
  14.                    class="delete-row btn">Delete
  15.             </button>
  16.           </td>
  17.         </tr>
  18.       {% endfor %}
  19.       </tbody>
  20.     </table>
  21.     <br>
  22.     <div class="container">
  23.       <button class="add-row btn" id="add_aspect">+ Add Aspect</button>
  24.     </div>
  25.     <button type="submit" class="btn">Submit</button>
  26.     <table class="table table-borderless">
  27.       <tbody>
  28.       <tr class="dynamic-form-adder" style="display: none">
  29.         <td class="col-sm-10">
  30.           {% form form=form_set.empty_form %}
  31.           {% endform %}
  32.         </td>
  33.         <td id="delete_column">
  34.           <button id="remove-form-__prefix__-row"
  35.                  class="delete-row btn">Delete
  36.           </button>
  37.         </td>
  38.       </tr>
  39.       </tbody>
  40.     </table>
  41.   </form>
  42.  
  43. <script>
  44.     $(function () {
  45.       $('.add-row').click(function () {
  46.         let runSelectTagPostProcessing = false;
  47.         return addForm(this, 'form', runSelectTagPostProcessing);
  48.       });
  49.       $('.delete-row').click(function () {
  50.         deleteForm(this, 'form');
  51.       })
  52.     });
  53.   </script>
  54.  
  55. function addForm(btn, prefix, select_post_processing) {
  56.     var formCount = parseInt($('#id_' + prefix + '-TOTAL_FORMS').val());
  57.     var row = $('.dynamic-form-adder').clone(true).get(0);
  58.     //change the cloned row class name
  59.     var brand_tr_class_name = $(row).attr('class');
  60.     $(row).removeClass(brand_tr_class_name);
  61.     $(row).addClass('dynamic-form');
  62.     //insert into the table as the last element
  63.     $(row).removeAttr('id');
  64.     $(row).removeAttr('style');
  65.     //replace the empty form __prefix__ with the actual form number count
  66.     var replace_prefix = $(row).html().replace(/__prefix__/g, formCount);
  67.     $(row).html(replace_prefix);
  68.  
  69.     $(row).find('*').each(function () {
  70.         updateElementIndex(this, prefix, formCount);
  71.         $(this).val('');
  72.     });
  73.     $(row).find('.delete-row').click(function () {
  74.         deleteForm(this, prefix);
  75.     });
  76.     $('#id_' + prefix + '-TOTAL_FORMS').val(formCount + 1);
  77.     $('#dynamic_form_table tbody').append($(row));
  78.     if (select_post_processing == true) {
  79.         selectPostProcessing(formCount);
  80.     }
  81.     return false;
  82. }
  83.  
  84. function selectPostProcessing(formCount) {
  85.     // Very hacky method which exists because there is duplication in select tag
  86.     // after it gets appended to the table. Essentially replace the child select(nested select duplicate) with the parent
  87.     // select tag, thus deduplicating. Remove once the root cause is identified.
  88.     let select_id = "#id_form-" + formCount + "-crawl_entity_container";
  89.     let select_to_be_replaced = $(select_id)[0].childNodes[3];
  90.     let select_replacee = $(select_id)[0].childNodes[3].childNodes[3];
  91.     $(select_to_be_replaced).replaceWith(select_replacee);
  92.     let select_options_parent = "#id_form-" + formCount + "-crawl_entity";
  93.     let select_options = $(select_options_parent)[0].childNodes;
  94.     let counter = 0;
  95.     // the below code exists because for some reason the value field for select tag
  96.     // is non existent. Remove once the root cause is identified
  97.     let values_copy = $.merge([], values);
  98.     $(select_options).each(function () {
  99.         if ($(this).prop('tagName') == "OPTION") {
  100.             if (counter != 0) {
  101.                 $(this).val(values_copy.pop());
  102.             }
  103.             counter = counter + 1;
  104.         }
  105.     });
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement