View|Brush a field when selecting data in Select

Asked

Viewed 108 times

1

In this code appears the select with the Cities grouped in the way I want, but I would like to click on any of the 'store' options of the select to be opened in some table below the address of the selected store only.

HTML

<div> <select id="cities"></select> </div>

<div> <select id="loja selecionada"></select> </div>

JAVASCRIPT

var values = [
{ "Estado": "São Paulo",
  "OrderID": "Loja1",
  "Endereco":"Rua 10"},
{"Estado": "São Paulo",
  "OrderID": "Loja2",
  "Endereco":"Rua 10"}, 
{ "Estado": "São Paulo",
  "OrderID": "Loja3",
  "Endereco":"Rua 10"}, 
{ "Estado": "Rio de Janeiro",
  "OrderID": "Loja1",  
  "Endereco":"Rua 10"},
{ "Estado": "São Paulo", 
  "OrderID": "Loja4",
  "Endereco":"Rua 10"}, 
{ "Estado": "Mato Grosso",
  "OrderID": "Loja5",
  "Endereco":"Rua 10"},
{ "Estado": "São Paulo", 
  "OrderID": "Loja6",
  "Endereco":"Rua 10"}, 
{ "Estado": "Minas Gerais",
  "OrderID": "Loja1",
  "Endereco":"Rua 10"},
{ "Estado": "São Paulo",
  "OrderID": "Loja7",
  "Endereco":"Rua 10"},
{ "Estado": "Minas Gerais", 
  "OrderID": "Loja2",
  "Endereco":"Rua 10"}, 
{ "Estado": "São Paulo", 
  "OrderID": "Loja8",
  "Endereco":"Rua 10"}
];

var items = {},
base, key;
$.each(values, function(index, val) {
key = val.Estado;
if (!items[key]) {
   items[key] = [];
}
items[key].push(val.OrderID);
});

var $select = $('#cities');
$.each(items, function(index, val) {

var optgroup = $('<optgroup>');
    optgroup.attr('label', index);

$.each(val, function(index, val) {
    optgroup.append($('<option>', {
    text: val
    }));
});

$select.append(optgroup);
});

1 answer

1


Change the options mount to save the index:

$.each(val, function(index, val) {
  optgroup.append($('<option>', {
    text: val,
    value: index //index
  }));
});

then just implement in select change to take the address and display in the field:

$('#cities').change(function() {
  var endereco = values[$(this).val()].Endereco;
  $('#endereco').text(endereco);
});

HTML:

<label>Endereço: <span id="endereco"></span></label>

Follows jsfiddle :)

  • Bruno, this way he takes the position of the field, note that in Fiddle is possible to verify that when selecting the 'loja1' below 'Rio de Janeiro' it does not show 'Street 4' that would be the respective street the store in Rio de Janeiro and yes 'Street 1' picking from the first position

  • 1

    take a look at this fiddle, really was wrong, the change I made was to save the index of the element in the base array. If it is correct let me know that I will update in the reply :)

  • Bruno, thanks for the help, if I want to insert another field for example: { "Status": "São Paulo", "Orderid": "Loja8", "Address":"Street 10", "Quantity":"19"} And show on the screen along with the 'Address' to 'Quantity', how should I proceed?

  • @CRAJ, it’s quite simple, I did an exeplo on own fiddle if you have any questions I’m available :)

  • Brunno, I expressed myself badly, what I want is in this case to create a field in this way: { "State": "São Paulo", "Orderid": "Loja8", "Endereco":"Rua 10", "src":"endereconoMaps"} and make the value inside the html in the <iframe src='addressMaps' width='600' height='450' frameborder='0' style='border:0' allowfullscreen></iframe> change when the user clicks on the desired store, it is possible to?

  • @CRAJ, which Api generates this iframe for you ?

  • Google’s own My Business API

  • @CRAJ I don’t know the API, more likely in the documentation must have some method that accepts as a parameter the strings and re-draws the map. In case it would only be to call him inside the change, passing the ordinances. More because I have no knowledge of the API I can not help you much hehe'

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.