AnsweredAssumed Answered

How to  get unique values from  an only field and display in a listbox

Question asked by d.diezr on Jun 17, 2015
Latest reply on Jun 21, 2015 by d.diezr

I am trying to  get unique values from  an only field from a  service and after displaying field values in a listbox.

 

Here (http://forums.esri.com/Thread.asp?c=158&f=2396&t=277241#top )there is an example that shows several fields in a first listbox, and after shows the fields values in a second listboxs

 

How could i do the same but with an only FIELD instead of an array of field.  I only need to display the values of an only field in a listbox.

 

Anyone could help??? Any idea about how to subsitute ana array of fields per an single field in this code html??

 

Thank you very much. I am not developer and this is quite difficult for me

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"

  "http://www.w3.org/TR/html4/strict.dtd">

 

<html lang="en">

  <head>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <title>requestfieldvalues</title>

 

  <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.3"></script>

 

  <script type="text/javascript" charset="utf-8">

  dojo.require("esri.utils");

  dojo.require("esri.tasks.query");

 

  var queryTask, query;

 

  function init() {

  queryTask = new esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/2");

 

  query = new esri.tasks.Query();

  query.where = "1=1";

 

  getFieldList();

  }

 

  dojo.addOnLoad(init);

 

  function getFieldList() {

  esri.request({

  url: "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0",

  content: { f:"json" },

  handleAs: "json",

  callbackParamName: "callback",

  load: function(response, io) {

  var fields = response.fields;

  var options = [];

  dojo.forEach(fields, function(field) {

  options.push("<option value=\"" + field.name + "\">" + field.alias + "</option>");

  });

  dojo.byId("fields").innerHTML = options.join("");

  },

  error: function(error) {

  alert(error.message);

  }

  });

  }

 

  function getFieldValues(field) {

  query.outFields = [field];

  queryTask.execute(query, dojo.partial(displayValues, field));

  }

 

  function displayValues(field, featureSet) {

  var options = [], values = [], value;

  dojo.forEach(featureSet.features, function(feature) {

  value = feature.attributes[field];

 

  //add all values

  // options.push("<option value=\"" + value + ">" + value + "</option>");

 

  //filter values to only display unique values

  if (dojo.indexOf(values, value) == -1) {

  values.push(value);

  options.push("<option value=\"" + value + "\">" + value + "</option>");

  }

  });

 

  dojo.byId("values").innerHTML = options.join("");

  }

 

  </script>

  </head>

  <body>

  <select id="fields" onchange="getFieldValues(this.value);"></select>

  <select id="values" onchange="getFieldValues(this.value);"></select>

  </body>

</html>

Outcomes