Selected html filter single fields

Asked

Viewed 315 times

1

I have the following Selected, how do I make it Filtre the single fields for example it is repeating the same option <option value='967'>GOIANIA - GO</option> several times I want it to show only once the repeated data, and possible to do this with jquery.

"<option selected="selected" value="0">Cidade</option><option value='967'>GOIANIA - GO</option><option value='967'>GOIANIA - GO</option><option value='967'>GOIANIA - GO</option><option value='967'>GOIANIA - GO</option>"
  • 1

    Where does this HTML come from? wouldn’t it be better to do this on the server or where this HTML is generated?

  • this Selected is being generated through an ajax that requests for the method and takes the return of the list in json ultilizando Asp mvc , la in the controller I have the list of its two attributes Id,I have last named the distinct but it is returning unique Egm.Lstcid = Egm.LstCid.Distinct() data. Tolist(); , I thought there was an easy way to easy using jquery

1 answer

1


Understand that the following script only treats the symptom, the problem will still continue, ideally perform the filter in your query populating select.

var values = [];
var select = document.querySelector("select");
[].forEach.call(select.options, function (option, indice) {
  if (values.indexOf(option.value) > -1) {
    select.removeChild(option);
  } else {
    values.push(option.value);
  }  
});
<select>
  <option value="">Selecione...</option>
  <option value="1">Opção 1</option>
  <option value="2">Opção 2</option>
  <option value="3">Opção 3</option>
  <option value="4">Opção 4</option>
  <option value="2">Opção 2</option>
  <option value="3">Opção 3</option>
  <option value="4">Opção 4</option>
  <option value="5">Opção 5</option>
</select>

EDIT - Suggestion

Reading your comment, I believe Distinct will work if you decrease the fields of your select.

egm.LstCid.Select(cid => new { Id = cid.Id, Nome = cid.Nome }).Distinct().ToList();
  • realemte ultilized your solution Egm.LstCid.Select(Cid => new { Id = Cid.Id, Name = Cid.Name }). Distinct(). Tolist(); and distinct worked :)

Browser other questions tagged

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