Hi all,
I am using a feature layer in my web up and when I make a selection, I would like to sum up the numeric values stored in one field for the selected values. Is that possible?
Thanks,
Alex
Solved! Go to Solution.
If I understand your question correctly, when you make a selection from your feature layer, you will have an array of features. You can cycle through this array to get the value from each feature and add it to the sum
var sum = 0;
array.forEach(features, function (feature) {
sum += feature.attributes.yourField;
}
If I understand your question correctly, when you make a selection from your feature layer, you will have an array of features. You can cycle through this array to get the value from each feature and add it to the sum
var sum = 0;
array.forEach(features, function (feature) {
sum += feature.attributes.yourField;
}
Thanks ken. That is just what I need. It seems however like the new selection did not pertain the attributes. Any idea how to pass the attributes along?
function initSelectToolbar(event) {
console.log("start edits");
selectionToolbar = new Draw(event.map);
var selectQuery = new Query();
selectQuery.outFields = ["*"];
var targetGeometry;
on(selectionToolbar, "DrawEnd", function (geometry) {
var geometryService = new GeometryService("http://itas46:6080/arcgis/rest/services/Utilities/Geometry/GeometryServer");
selectQuery.geometry = geometry;
featureLayer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW, function (targetGeometry) {
var sum = 0;
var selectedfeatures = featureLayer.getSelectedFeatures();
$.each(selectedfeatures, function (feature) {
sum += feature.attributes.Sum_TREECO;
});
console.log(sum);
Why not just use targetGeometry in your array
featureLayer.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW, function (targetGeometry) {
var sum = 0;
$.each(targetGeometry, function (feature) {
sum += feature.attributes.Sum_TREECO;
});
});
I tried. I still get this error. I am not sure if featureLayer.selectFeatures keeps all the attributes or discards them.
array.forEach
it seems like it is not taking the JQuery part. array.foreach or arrayUtil.forEach was the way to go. Thanks Ken!