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

192
Visualizações
Select2 filter already loaded data without sending a request

i have litle problem with select2.

I want to do when the user selects a country to get all the regions for that country by country ID number. I achieved this without any problems. The problem is when I want to search already loaded regions. I type the name of the region in the input field and it sends the GET id request.

How to filter already loaded data without sending a new request to the database?

Here is code.

    var countries =  $('.js-country-select');
    var states = $('.js-state-select');
    var city = $('.js-city-select');


      // Init all countries

      countries.select2({
        ajax: {
            url: '/admin/drzave/',
            dataType: 'json',         
        }
    });

    // Select country and get states based on Country ID

    countries.on('select2:selecting', function() {

        // when is country selected than load states by id

        states.select2({
                ajax: {
                    url: '/admin/region/',
                    data: function() {
                        var query = {
                            country_id: countries.val(), // <-- selected country ID
                        }
                        return query;
                    },
                    dataType: 'json',  
                }
        });
    });

SQL: SELECT * FROM regions WHERE country_id = ID

Here is photo

On photo you can see, country request and region. Region all time send id and cant be filtered by region name.

screenshot

about 4 years ago · Juan Pablo Isaza
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