Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

179
Vistas
how to add select options dynamically using javascript variables in jinja2 loop

I am trying to dynamically populate a table's select dropbox, and to do that I need to loop over an array.

However, since the loop is done via jinja2 (check last line in code) and the array I am looping on is a javascript variable, it is not working.

Is there a workaround available that my research missed or is it impossible to do?

    var cardgroups = JSON.parse('{{ cardgroups | tojson | safe}}');
    var offices = JSON.parse('{{ offices | tojson | safe}}');
    const departments = JSON.parse('{{ departments | tojson | safe}}');

    const office = $('#office').val();
    let office_id = 0;
    for(let i = 0; i < offices.length; i++) {
        if (offices[i]['shortname'] == office) {
            office_id = offices[i]['id'];
            break;
        }
    }

    var office_cardgroups = []
    for(let i = 0; i < cardgroups.length; i++) {
        if (cardgroups[i]['office'] == office_id) {
            office_cardgroups.push(cardgroups[i]);
        }
    }

    const table = document.getElementById("table");
    var tr = document.createElement('tr');
       table.appendChild(tr);

    {% for department in departments %}
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
        row.insertCell(0).innerHTML = "{{ department.name }}";
        row.insertCell(1).innerHTML = '<td className="select"><select id="cardgroups_{{ department.id }}">{% for cardgroup in office_cardgroups %}<option value="{{ cardgroup.id }}">{{cardgroup.name}}</option>{% endfor %}</select></td>';

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda