Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

156
Visualizações
Is there a way to filter data from jinja2 with using javascript?

Simply I have a data which coming from jinja2. I want to filter data when I clicked the button. Another solution might be to pass the data to a javascript variable.

I have created an example for more clear definition. In this case I want to delete numbers which are less then 5. Then I list them in html.

In template;

{% set data = [1,2, 3, 4, 5, 6, 7, 8, 9] %}
     
<ul> 
  {% for item in data %}
    <li>{{ item }}</li>
  {% endfor %}
</ul>

<button class="btn">Filter Data</button>

In Script

const btn = document.querySelector('.btn');

btn.addEventListener('click', () => {
// filter here
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The simplest solution is that you also provide the data value as an attribute of the list item, e.g. data-index:

{% set data = [1,2, 3, 4, 5, 6, 7, 8, 9] %}
     
<ul class="mylist"> 
  {% for item in data %}
    <li data-index="{{ item }}">{{ item }}</li>
  {% endfor %}
</ul>

<button class="btn">Filter Data</button>

Then you loop over the list items after the click event and based on the value of el.dataset.index, you can remove the respective items:

const btn = document.querySelector('.btn');

btn.addEventListener('click', () => {
    let items = document.querySelectorAll(".mylist li")
    Array.from(items, el => {
        if (el.dataset.index < 5) {
            el.remove()
            // Or: el.style.display = 'none'
        }
    })    
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda