Select to view content in your preferred language

Feature layer selection sum up numeric values in field

1165
5
Jump to solution
11-17-2016 01:36 PM
by Anonymous User
Not applicable

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

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

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;
}

View solution in original post

5 Replies
KenBuja
MVP Esteemed Contributor

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;
}
by Anonymous User
Not applicable

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);

0 Kudos
KenBuja
MVP Esteemed Contributor

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;
    });
});
by Anonymous User
Not applicable

I  tried. I still get this error. I am not sure if featureLayer.selectFeatures keeps all the attributes or discards them. 

0 Kudos
by Anonymous User
Not applicable
array.forEach

it seems like it is not taking the JQuery part. array.foreach or arrayUtil.forEach was the way to go. Thanks Ken!

0 Kudos