Simple question:
How do you get unique values (for instance for rendering or calculate statistics) from a feature service where the value can change dynamically?
Solved! Go to Solution.
Christian,
You can use a Query on the layer using returnDistinctValues property of the Query class in the JS API.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Query distinct values without map</title>
<script src="https://js.arcgis.com/3.18/"></script>
<script>
require([
"dojo/dom", "dojo/on",
"esri/tasks/query", "esri/tasks/QueryTask", "dojo/domReady!"
], function (dom, on, Query, QueryTask) {
var queryTask = new QueryTask("https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Puget_Sound_BG_Food/FeatureServer/...");
var query = new Query();
query.returnGeometry = false;
query.returnDistinctValues = true;
query.outFields = [
"COUNTY"
];
on(dom.byId("execute"), "click", execute);
function execute () {
query.where = '1=1';
queryTask.execute(query, showResults);
}
function showResults (results) {
var resultItems = [];
var resultCount = results.features.length;
for (var i = 0; i < resultCount; i++) {
var featureAttributes = results.features[i].attributes;
for (var attr in featureAttributes) {
resultItems.push("<b>" + attr + ":</b> " + featureAttributes[attr] + "<br>");
}
resultItems.push("<br>");
}
dom.byId("info").innerHTML = resultItems.join("");
}
});
</script>
</head>
<body>
<input id="execute" type="button" value="Get Details">
<br />
<br />
<div id="info" style="padding:5px; margin:5px; background-color:#eee;">
</div>
</body>
</html>
Christian,
You can use a Query on the layer using returnDistinctValues property of the Query class in the JS API.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Query distinct values without map</title>
<script src="https://js.arcgis.com/3.18/"></script>
<script>
require([
"dojo/dom", "dojo/on",
"esri/tasks/query", "esri/tasks/QueryTask", "dojo/domReady!"
], function (dom, on, Query, QueryTask) {
var queryTask = new QueryTask("https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Puget_Sound_BG_Food/FeatureServer/...");
var query = new Query();
query.returnGeometry = false;
query.returnDistinctValues = true;
query.outFields = [
"COUNTY"
];
on(dom.byId("execute"), "click", execute);
function execute () {
query.where = '1=1';
queryTask.execute(query, showResults);
}
function showResults (results) {
var resultItems = [];
var resultCount = results.features.length;
for (var i = 0; i < resultCount; i++) {
var featureAttributes = results.features[i].attributes;
for (var attr in featureAttributes) {
resultItems.push("<b>" + attr + ":</b> " + featureAttributes[attr] + "<br>");
}
resultItems.push("<br>");
}
dom.byId("info").innerHTML = resultItems.join("");
}
});
</script>
</head>
<body>
<input id="execute" type="button" value="Get Details">
<br />
<br />
<div id="info" style="padding:5px; margin:5px; background-color:#eee;">
</div>
</body>
</html>
returnDistinctValues property! Ok, didn't think about! Thank you!
Don't forget to mark this question as answered by clicking on the "Correct Answer" link on the reply that answered your question.