Updating the results of a Query Statistic using Attribute Inspector

506
3
04-03-2018 01:26 PM
GiovanniMarrero3
New Contributor III

My web app allow users to edit existing polygon features using the attribute inspector. I have been trying to use a query to create a summary statistics to report back the count of a specific attribute name. The app currently display the count of a specific feature but it does not automatically update when a change is made to the feature using the attribute inspector. How can I get the query to automatically update the statistics after each change in the attribute. 

Here is part of my code:

 var blocks = new FeatureLayer("http://gis.dekalb.k12.ga.us/arcgis_webadapter/rest/services/Redistricting/ES_Areas_Redist_Test/FeatureServer/0", {
 mode: FeatureLayer.MODE_SNAPSHOT,
 outFields: ["ES_Name", "ES_City", "DDP_ES_Name"]
 });

 var selectionSymbol = new SimpleFillSymbol().setColor(new Color([255, 255, 255, 0.5]));
 blocks.setSelectionSymbol(selectionSymbol);

 blocks.setRenderer(renderer);
 map.addLayers([blocks]);

 map.on("layers-add-result", initSelectToolbar);

 //Code to store names for the query click
 map.infoWindow.on('show', function(evt) {
 setTimeout(function() {
 var node = dojoQuery('.atiField', evt.domNode);
 if (!node) {
 return;
 }
 var selectWid = registry.getEnclosingWidget(node[1]);
 //console.info(selectWid.get("store"));
 var oNames = [];
 array.map(uNames, function(fName) {
 oNames.push({
 id: fName,
 name: fName
 });
 });
 var data = {
 label: 'name',
 items: oNames
 };
 var store = new Memory({
 data: data
 });
 selectWid.setAttribute("store", store);
 }, 800);
 });


 function initSelectToolbar(evt) {
 var blocks = evt.layers[0].layer;
 var selectQuery = new Query();

 map.on("click", function(evt) {
 map.infoWindow.hide();
 selectQuery.outFields = ["*"];
 selectQuery.geometry = evt.mapPoint;
 selectQuery.returnGeometry = true;
 blocks.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW, function(features) {
 uNames = [];
 if (features.length > 0) {
 var attrQuery = new Query();
 attrQuery.geometry = features[0].geometry;
 attrQuery.outFields = ["ES_Name"];
 attrQuery.returnGeometry = false;
 attrQuery.spatialRelationship = Query.SPATIAL_REL_TOUCHES;
 blocks.queryFeatures(attrQuery, function(fset) {
 array.map(fset.features, function(feat) {
 var fname = feat.attributes.ES_Name;
 if (uNames.indexOf(fname) === -1) {
 uNames.push(fname);
 }
 });
 });
 //store the current feature
 updateFeature = features[0];
 title = "Study Area is " + features[0].attributes.DDP_ES_Name //InfoWindow Title with Attribute Name
 map.infoWindow.setTitle(title);
 map.infoWindow.show(evt.screenPoint, map.getInfoWindowAnchor(evt.screenPoint));
 } else {
 map.infoWindow.hide();
 }
 });
 });

 map.infoWindow.on("hide", function() {
 blocks.clearSelection();
 });

 var layerInfos = [{
 'featureLayer': blocks,
 'showAttachments': false,
 'isEditable': true,
 'fieldInfos': [{
 'fieldName': 'ES_City',
 'isEditable': false,
 'label': 'Current School:'
 }, {
 'fieldName': 'ES_Name',
 'isEditable': true,
 'label': 'Proposed School:'
 }]
 }];

 //Initialize Attribute Inspector
 var attInspector = new AttributeInspector({
 layerInfos: layerInfos
 }, domConstruct.create("div"));

 //add an apply button for any changes
 var saveButton = new Button({
 label: "Apply",
 "class": "saveButton"
 }, domConstruct.create("div"));
 domConstruct.place(saveButton.domNode, attInspector.deleteBtn.domNode, "after");

 saveButton.on("click", function() {
 updateFeature.getLayer().applyEdits(null, [updateFeature], null, function(result) {
 console.log(result);
 }, function(error) {
 console.log(error);
 });
 map.infoWindow.hide();
 blocks.clearSelection(selectionSymbol);
 });

 attInspector.on("attribute-change", function(evt) {
 //store the updates to apply when the save button is clicked
 updateFeature.attributes[evt.fieldName] = evt.fieldValue;
 });

 map.infoWindow.setContent(attInspector.domNode);
 map.infoWindow.resize(350, 240);
 }

 map.on("update-end", function() {

 var sqlExpression = "ES_Name";

 // Object used to request the count of all block groups

 var countStatDef = new StatisticDefinition();
 countStatDef.statisticType = "count";
 countStatDef.onStatisticField = sqlExpression;
 countStatDef.outStatisticFieldName = "numBlockGroups";

 var queryParams = new Query();
 queryParams.where = "ES_Name = 'North'";
 queryParams.outFields = ["*"];
 queryParams.returnGeometry = true;
 queryParams.outStatistics = [countStatDef];
 blocks.queryFeatures(queryParams, showResults);

 function showResults(results) {
 // The return object of the query containing the statistics requested
 var stats = results.features[0].attributes

 dom.byId("countResult").innerHTML = Math.round(stats.numBlockGroups);
 }
 })
0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor

I would run the query each time the attributes get updated (in the "attribute-change" event handler)

0 Kudos
GiovanniMarrero3
New Contributor III

Thanks for the reply. I tried using that method. The info populates as it should once the attribute changes but it does not update the total count, stays the same regardless of the changes. 

0 Kudos
GiovanniMarrero3
New Contributor III

Update.. the original script was working correctly. I had to change my layer privileges to update so that the query would recognize the new updated attributes. I did not realize that the query function is called back to the server, i though it could query on the client-side.  

Is there a way to query on the client side instead back to the server so that I can reset the layer back to select only? 

0 Kudos