Popular Select with XML - Logic

0

Hello, I need a popular select so they are dynamic. The customer is a car site, that is, the user can schedule the cars.

My work is popular select, however I'm having difficulties because XML seems to me poorly structured ...

I will have 4 selects city, store, brand and car.

That is, if the user selects São Paulo, it will show only the stores and cars available in sp ...

But, I think XML is poorly organized, because it returns the name of the city more than once ...

Below is the code ...

<entry>
    <g:id>109851</g:id> 
    <title>FIAT UNO 1.0 FIREFLY FLEX ATTRACTIVE 4P MANUAL</title> 
    <g:description>Nenhuma descrição encontrada.</g:description> 
    <link></link> 
    <g:image_link></g:image_link> 
    <g:availability>in stock</g:availability> 
    <g:price>34290 BRL</g:price> 
    <g:google_product_category>916</g:google_product_category> 
    <g:brand>FIAT</g:brand> 
    <g:identifier_exists>no</g:identifier_exists>
    <g:condition>used</g:condition> 
    <g:adult>no</g:adult> 
    <g:is_bundle>no</g:is_bundle> 
    <g:color>PRATA</g:color> 
    <state>Minas Gerais</state> 
    <city>Belo Horizonte</city> 
    <store>Belo Horizonte - Estoril</store> 
  </entry> 

In each entry there is a city, and if I look for this city with the jquery fetch, it returns me all the cities of all the entrys (the same city about 100 times) ...

The correct thing would be for me to ask them to rephrase this XML? or is it possible for me to turn around with him?

// edit

My code

    $.ajax({
            type: "GET",
            url: "googleshoppingcustom.xml",
            dataType: 'xml',
            success: function(xml){
              $(xml).find('entry').find('state').each(function(i){
                var state = $(this).text();
                $('#region').append('<option value=${state}>${state}</option>')

)}

My real problem is not even this repetition ... but how could I get EVERY car brand for EVERY car ... I find the xml a bit messy ...

strong>

    
asked by anonymous 03.01.2019 / 14:33

1 answer

0
  

Instead of using 2 .find , just do this: $(xml).find('entry state')... that already searches all <state> tags within <entry> .

You can go appending by checking whether a state has already been entered or not, resulting in unique options with the names of states:

$.ajax({
   type: "GET",
   url: "googleshoppingcustom.xml",
   dataType: 'xml',
   success: function(xml){
      $(xml).find('entry state').each(function(i){

         var state = $(this).text();

         // verifica se já existe um option com o nome do Edtado
         if( !$('#region option[value="'+ state +'"]').length ){
            $('#region').append('<option value='${state}'>${state}</option>');
         }
      });
   }
});
    
03.01.2019 / 17:28