Advertisement
RupeshAcharya60

js

Aug 26th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {% extends 'dashboard/base.html' %}
  2. {% block content %}
  3.  
  4. <style>
  5.     .delete-icon {
  6.         color: red;
  7.         cursor: pointer;
  8.         font-size: 1.2em;
  9.         background: none;
  10.         border: none;
  11.         padding: 0;
  12.     }
  13.     .address-form {
  14.         position: relative;
  15.         padding: 20px;
  16.         margin-bottom: 20px;
  17.     }
  18. </style>
  19.  
  20.  
  21. <!-- Add Font Awesome CSS in the head of your base template or here -->
  22. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
  23.  
  24.  
  25.  
  26. <div>
  27.     <div >
  28.     <div class="stepper stepper-pills stepper-column d-flex flex-column flex-xl-row flex-row-fluid gap-10" id="kt_create_account_stepper">
  29.  
  30. {% include 'dashboard/client/snippets/form_sidebar.html' with step=wizard.steps.step1 %}
  31. <div class="w-100">
  32.  
  33.     <form method="post" class="d-flex flex-column align-items-start gap-3 justify-content-between">
  34.         <!--begin::Progress-->
  35.        
  36.        <!--end::Progress-->
  37.      {% csrf_token %}
  38.      {{ wizard.management_form }}
  39.    
  40.    <div class="d-flex justify-content-around w-100">
  41.        <div class="d-flex flex-column align-items-center">
  42.            <h3 class="text-gray-600">Primary Applicant Addresses</h3>
  43.            {{ wizard.form.primary_addresses.management_form }}
  44.            <div id="primary-addresses" class="card card-flush h-lg-10 w-100">
  45.                {% for form in wizard.form.primary_addresses %}
  46.                    <div class="address-form bg-gray px-10 py-2 position-relative w-500px">
  47.                        <button class="btn btn-danger delete-btn position-absolute top-0 end-0 m-2" type="button" onclick="deleteForm('primary', this)" {% if forloop.first and forloop.last %}style="display: none;"{% endif %}>
  48.                            <i class="fas fa-trash-alt"></i>
  49.                        </button>
  50.    
  51.                        <div class="card-header " id="kt_chat_contacts_header">
  52.                        <div class="card-title">
  53.                            <h2 id="title">{{forloop.counter}} Residential Address</h2>
  54.                        </div>
  55.                    </div>
  56.                        
  57.                        {{ form.as_p }}
  58.                        <div class="separator"></div>
  59.    
  60.                    </div>
  61.                {% endfor %}
  62.            </div>
  63.            <div class="d-flex justify-content-center gap-2">
  64.                <button class="btn" type="button" onclick="addForm('primary')">
  65.                    <i class="ki-solid ki-plus-square fs-2x text-primary">
  66.                    </i>
  67.            
  68.                </button>
  69.    
  70.            </div>
  71.        </div>
  72.        
  73.        {% if "married" == relationship_val %}
  74.       <div class="d-flex flex-column align-items-center">
  75.         <h3 class="text-gray-600">Secondary Applicant Addresses</h3>
  76.         {{ wizard.form.secondary_addresses.management_form }}
  77.         <div id="secondary-addresses" class="card card-flush h-lg-10">
  78.             {% for form in wizard.form.secondary_addresses %}
  79.                 <div class="address-form px-10 py-2 position-relative w-500px">
  80.                     <button class="btn btn-danger delete-btn position-absolute top-0 end-0 m-2" type="button" onclick="deleteForm('secondary', this)" {% if forloop.first and forloop.last %}style="display: none;"{% endif %}>
  81.                         <i class="fas fa-trash-alt"></i>
  82.                     </button>
  83.  
  84.                     <div class="card-header ">
  85.                         <div class="card-title">
  86.                             <h2 id="title">{{forloop.counter}} Residential Address</h2>
  87.                         </div>
  88.                     </div>
  89.                        
  90.                     {{ form.as_p }}
  91.                 </div>
  92.             {% endfor %}
  93.         </div>
  94.         <div class="d-flex justify-content-center">
  95.  
  96.         <button class="btn" type="button" onclick="addForm('secondary')">
  97.             <i class="ki-solid ki-plus-square fs-2x text-primary">
  98.             </i>
  99.         </button>
  100.         </div>
  101.     </div>
  102.     {% endif %}
  103.    </div>
  104.    
  105.    <div class="d-flex justify-content-between m-2 p-2 gap-2">
  106.        <button class="btn btn-primary " type="submit" name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">Previous</button>
  107.        <button class="btn btn-primary " type="submit">{% if wizard.steps.prev %}Next Step{% else %}Submit{% endif %}</button>
  108.    </div>
  109.    
  110.    
  111.    
  112.    
  113.    
  114.    
  115.    
  116.    {% if form.errors %}
  117.    <div class="form-errors">
  118.        <ul>
  119.            {% for field in form %}
  120.                {% if field.errors %}
  121.                    <li><strong>{{ field.label }}:</strong> {{ field.errors }}</li>
  122.                {% endif %}
  123.            {% endfor %}
  124.        </ul>
  125.    </div>
  126.    {% endif %}
  127.    
  128.    </form>
  129. </div>
  130. </div>
  131. </div>
  132. </div>
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. <script>
  143. function addForm(prefix) {
  144.     const formsDiv = document.getElementById(`${prefix}-addresses`);
  145.     const totalForms = document.getElementById(`id_${prefix}-TOTAL_FORMS`);
  146.    
  147.     if (!totalForms) {
  148.         console.error(`Total forms element not found for ${prefix}`);
  149.         return;
  150.     }
  151.    
  152.     const newForm = formsDiv.children[0].cloneNode(true);
  153.     const formRegex = new RegExp(`${prefix}-(\\d+)-`, 'g');
  154.     const formNum = totalForms.value;
  155.  
  156.     Array.from(newForm.querySelectorAll('*')).forEach(element => {
  157.         if (element.id) {
  158.             element.id = element.id.replace(formRegex, `${prefix}-${formNum}-`);
  159.         }
  160.         if (element.name) {
  161.             element.name = element.name.replace(formRegex, `${prefix}-${formNum}-`);
  162.         }
  163.     });
  164.  
  165.     newForm.querySelector('#title').innerHTML = `${parseInt(formNum)+1} Residential Address`;
  166.  
  167.     // Clear input values in the new form
  168.     newForm.querySelectorAll('input, select, textarea').forEach(input => {
  169.         input.value = '';
  170.     });
  171.  
  172.     // Ensure the delete button is visible and has the correct onclick
  173.     const deleteBtn = newForm.querySelector('.delete-btn');
  174.     deleteBtn.style.display = 'block';
  175.     deleteBtn.onclick = function() { deleteForm(prefix, this); };
  176.  
  177.     formsDiv.appendChild(newForm);
  178.  
  179.     totalForms.value = parseInt(totalForms.value) + 1;
  180.     updateDeleteButtons(prefix);
  181. }
  182.  
  183. function deleteForm(prefix, button) {
  184.     const formsDiv = document.getElementById(`${prefix}-addresses`);
  185.     const totalForms = document.getElementById(`id_${prefix}-TOTAL_FORMS`);
  186.  
  187.     button.closest('.address-form').remove();
  188.     totalForms.value = parseInt(totalForms.value) - 1;
  189.  
  190.     // Renumber the remaining forms
  191.     const forms = formsDiv.children;
  192.     for (let i = 0; i < forms.length; i++) {
  193.         const title = forms[i].querySelector('#title');
  194.         title.innerText = `${i + 1} Residential Address`;
  195.  
  196.         const formRegex = new RegExp(`${prefix}-(\\d+)-`, 'g');
  197.         Array.from(forms[i].querySelectorAll('*')).forEach(element => {
  198.             if (element.id) {
  199.                 element.id = element.id.replace(formRegex, `${prefix}-${i}-`);
  200.             }
  201.             if (element.name) {
  202.                 element.name = element.name.replace(formRegex, `${prefix}-${i}-`);
  203.             }
  204.         });
  205.     }
  206.  
  207.     updateDeleteButtons(prefix);
  208. }
  209.  
  210. function updateDeleteButtons(prefix) {
  211.     const formsDiv = document.getElementById(`${prefix}-addresses`);
  212.     const deleteButtons = formsDiv.querySelectorAll('.delete-btn');
  213.  
  214.     if (deleteButtons.length === 1) {
  215.         deleteButtons[0].style.display = 'none';
  216.     } else {
  217.         deleteButtons.forEach(btn => btn.style.display = 'block');
  218.     }
  219. }
  220.  
  221. // Initialize delete buttons on page load
  222. document.addEventListener('DOMContentLoaded', function() {
  223.     updateDeleteButtons('primary');
  224.     updateDeleteButtons('secondary');
  225. });
  226. </script>
  227. {% endblock %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement