function extractData() { //get clip layers var clipLayers = []; if (registry.byId("layer1").get("checked")) { clipLayers.push("SRA"); } if (registry.byId("layer2").get("checked")) { clipLayers.push("Burn"); } if (clipLayers.length === 0|| map.graphics.graphics.length === 0) { alert("Select layers to extract and draw an area of interest."); return; } var featureSet = new FeatureSet(); var features = []; features.push(map.graphics.graphics[0]); featureSet.features = features; var params3 = { "Layers_to_Clip": clipLayers, "Area_of_Interest": featureSet, "Feature_Format": registry.byId("formatBox").get("value") }; domStyle.set(loading2, "display", "inline-block"); gp.submitJob(params3, completeCallback, statusCallback2, function (error) { alert(error); domStyle.set(loading2, "display", "none"); }); }
function executeQueryTask(county) { var clipLayers = []; if (registry.byId("layer1").get("checked")) { clipLayers.push("SRA"); } if (registry.byId("layer2").get("checked")) { clipLayers.push("Burn"); } if (clipLayers.length === 0) { alert("No layers to clip, please select a layer you would like to clip..."); return; } var county = document.getElementById("sel_county").value var queryTask = new QueryTask("http://webgisdevint1/arcgis/rest/services/Alex_Try/Counties/MapServer/0"); queryTask.on("complete", addToMap) var query = new Query(); query.returnGeometry = true; query.outFields = ["NAME_PCASE"]; query.where = "NAME_PCASE = '" + county + "'"; query.outSpatialReference = map.spatialReference; queryTask.execute(query, function (featureSet) { var AOI = featureSet.features[0].geometry; var graphic = new Graphic(AOI, symbol); var features = []; features.push(graphic); var fSet = new FeatureSet(); fSet.features = features; var params = { "Layers_to_Clip": clipLayers, "Area_of_Interest": fSet, "Feature_Format": registry.byId("formatBox").get("value") }; domStyle.set(loading, "display", "inline-block"); gp.submitJob(params, completeCallback, statusCallback, function (error) { alert(error); domStyle.set(loading, "display", "none"); }); }); }
registry.byId("extract1").on("click", executeQueryTask);
<button id="extract1" data-dojo-type="dijit/form/Button" data-dojo-props="iconClass:'Query', showLabel:false"> Extract Data </button>
Solved! Go to Solution.
case "extractByUnit": executeQueryTask1(unit); function executeQueryTask1(unit) { var clipLayers = []; if (registry.byId("layer1").get("checked")) { clipLayers.push("SRA"); } etc.
"addtomap" is a function that i call outside the switch function.
extractMethod = "extractByPoly";
extractMethod = "extractByCounty";
extractMethod = "extractByUnit";
switch (extractMethod ) { case "extractByPoly": someSubRoutine(); break; case "extractByCounty": executeQueryTask(countyName); break; default: executeQueryTask(unitName); break; }
I think one way would be using a global variable (something like "var extractMethod;") that gets populated at the conclusion of all three "methods". So, for example, at the conclusion of the user's draw event, code something like:extractMethod = "extractByPoly";
In the dropdown's update method for your county list, code something like:extractMethod = "extractByCounty";
...and finally, at the end of the dropdown's update method for your unit's info, code something like:extractMethod = "extractByUnit";
Now, in the click event for your "Extract" button, use a switch conditional to then determine what to do:switch (extractMethod ) { case "extractByPoly": someSubRoutine(); break; case "extractByCounty": executeQueryTask(countyName); break; default: executeQueryTask(unitName); break; }
Steve
function myFunction() { var extractMethod = "extractByPoly"; var extractMethod = "extractByCounty"; var extractMethod = "extractByUnit"; switch (extractMethod) { default: function extractData() { //get clip layers var clipLayers = []; if (registry.byId("layer1").get("checked")) { clipLayers.push("SRA"); } if (registry.byId("layer2").get("checked")) { clipLayers.push("Burn"); } if (clipLayers.length === 0|| map.graphics.graphics.length === 0) { alert("Select layers to extract and draw an area of interest."); return; } var featureSet = new FeatureSet(); var features = []; features.push(map.graphics.graphics[0]); featureSet.features = features; var params3 = { "Layers_to_Clip": clipLayers, "Area_of_Interest": featureSet, "Feature_Format": registry.byId("formatBox").get("value") }; domStyle.set(loading2, "display", "inline-block"); gp.submitJob(params3, completeCallback, statusCallback2, function (error) { alert(error); domStyle.set(loading2, "display", "none"); }); } break; case "extractByCounty": function executeQueryTask(county) { var clipLayers = []; if (registry.byId("layer1").get("checked")) { clipLayers.push("SRA"); } if (registry.byId("layer2").get("checked")) { clipLayers.push("Burn"); } if (clipLayers.length === 0) { alert("No layers to clip, please select a layer you would like to clip..."); return; } var county = document.getElementById("sel_county").value var unit = document.getElementById("sel_unit").value var queryTask = new QueryTask("http://webgisdevint1/arcgis/rest/services/Alex_Try/Counties/MapServer/0"); var queryTask1 = new QueryTask("http://webgisdevint1/arcgis/rest/services/Alex_Try/Layers/MapServer/1"); queryTask.on("complete", addToMap) var query = new Query(); query.returnGeometry = true; query.outFields = ["NAME_PCASE"]; query.where = "NAME_PCASE = '" + county + "'"; query.outSpatialReference = map.spatialReference; queryTask.execute(query, function (featureSet) { var AOI = featureSet.features[0].geometry; var graphic = new Graphic(AOI, symbol); var features = []; features.push(graphic); var fSet = new FeatureSet(); fSet.features = features; var params = { "Layers_to_Clip": clipLayers, "Area_of_Interest": fSet, "Feature_Format": registry.byId("formatBox").get("value") }; domStyle.set(loading, "display", "inline-block"); gp.submitJob(params, completeCallback, statusCallback, function (error) { alert(error); domStyle.set(loading, "display", "none"); }); }); } break; case "extractByUnit": function executeQueryTask1(unit) { var clipLayers = []; if (registry.byId("layer1").get("checked")) { clipLayers.push("SRA"); } if (registry.byId("layer2").get("checked")) { clipLayers.push("Burn"); } if (clipLayers.length === 0) { alert("No layers to clip, please select a layer you would like to clip..."); return; } var unit = document.getElementById("sel_unit").value var queryTask = new QueryTask("http://webgisdevint1/arcgis/rest/services/Alex_Try/Layers/MapServer/1"); queryTask.on("complete", addToMap2) var query = new Query(); query.returnGeometry = true; query.outFields = ["UNITCODE"]; query.where = "UNITCODE = '" + unit + "'"; query.outSpatialReference = map.spatialReference; queryTask.execute(query, function (featureSet) { var AOI = featureSet.features[0].geometry; var graphic = new Graphic(AOI, symbol); var features = []; features.push(graphic); var fSet = new FeatureSet(); fSet.features = features; var params2 = { "Layers_to_Clip": clipLayers, "Area_of_Interest": fSet, "Feature_Format": registry.byId("formatBox").get("value") }; domStyle.set(loading1, "display", "inline-block"); gp.submitJob(params2, completeCallback, statusCallback1, function (error) { alert(error); domStyle.set(loading1, "display", "none"); }); }); } } }
<button onclick="myFunction()" data-dojo-type="dijit/form/Button" data-dojo-props="iconClass:'DownIcon', showLabel:false">
HTML <button id="myButton" data-dojo-type="dijit/form/Button" data-dojo-props="iconClass:'DownIcon', showLabel:false"> JavaScript require(["dijit/registry"], function(registry){ registry.byId("myButton").on("click", myFunction); });
If your code is in AMD, then this is a problem of scoping. The "myFunction" function is not available in the HTML portion of your code. Instead, you should create a listener for the button in your script. See the documention for dojo events.
You'd use something like thisHTML <button id="myButton" data-dojo-type="dijit/form/Button" data-dojo-props="iconClass:'DownIcon', showLabel:false"> JavaScript require(["dijit/registry"], function(registry){ registry.byId("myButton").on("click", myFunction); });
set the extractMethod somewhere outside this function function myFunction() { //var extractMethod = "extractByPoly"; //var extractMethod = "extractByCounty"; //var extractMethod = "extractByUnit"; switch (extractMethod) { default: //function extractData() { //get clip layers var clipLayers = []; if (registry.byId("layer1").get("checked")) { clipLayers.push("SRA"); } if (registry.byId("layer2").get("checked")) { clipLayers.push("Burn"); } if (clipLayers.length === 0 || map.graphics.graphics.length === 0) { alert("Select layers to extract and draw an area of interest."); return; } var featureSet = new FeatureSet(); var features = []; features.push(map.graphics.graphics[0]); featureSet.features = features; var params3 = { "Layers_to_Clip": clipLayers, "Area_of_Interest": featureSet, "Feature_Format": registry.byId("formatBox").get("value") }; domStyle.set(loading2, "display", "inline-block"); gp.submitJob(params3, completeCallback, statusCallback2, function (error) { alert(error); domStyle.set(loading2, "display", "none"); }); //} break; case "extractByCounty": //function executeQueryTask(county) { var clipLayers = []; if (registry.byId("layer1").get("checked")) { clipLayers.push("SRA"); } if (registry.byId("layer2").get("checked")) { clipLayers.push("Burn"); } if (clipLayers.length === 0) { alert("No layers to clip, please select a layer you would like to clip..."); return; } var county = document.getElementById("sel_county").value var unit = document.getElementById("sel_unit").value var queryTask = new QueryTask("http://webgisdevint1/arcgis/rest/services/Alex_Try/Counties/MapServer/0"); var queryTask1 = new QueryTask("http://webgisdevint1/arcgis/rest/services/Alex_Try/Layers/MapServer/1"); queryTask.on("complete", addToMap) var query = new Query(); query.returnGeometry = true; query.outFields = ["NAME_PCASE"]; query.where = "NAME_PCASE = '" + county + "'"; query.outSpatialReference = map.spatialReference; queryTask.execute(query, function (featureSet) { var AOI = featureSet.features[0].geometry; var graphic = new Graphic(AOI, symbol); var features = []; features.push(graphic); var fSet = new FeatureSet(); fSet.features = features; var params = { "Layers_to_Clip": clipLayers, "Area_of_Interest": fSet, "Feature_Format": registry.byId("formatBox").get("value") }; domStyle.set(loading, "display", "inline-block"); gp.submitJob(params, completeCallback, statusCallback, function (error) { alert(error); domStyle.set(loading, "display", "none"); }); }); //} break; case "extractByUnit": //function executeQueryTask1(unit) { var clipLayers = []; if (registry.byId("layer1").get("checked")) { clipLayers.push("SRA"); } if (registry.byId("layer2").get("checked")) { clipLayers.push("Burn"); } if (clipLayers.length === 0) { alert("No layers to clip, please select a layer you would like to clip..."); return; } var unit = document.getElementById("sel_unit").value var queryTask = new QueryTask("http://webgisdevint1/arcgis/rest/services/Alex_Try/Layers/MapServer/1"); queryTask.on("complete", addToMap2) var query = new Query(); query.returnGeometry = true; query.outFields = ["UNITCODE"]; query.where = "UNITCODE = '" + unit + "'"; query.outSpatialReference = map.spatialReference; queryTask.execute(query, function (featureSet) { var AOI = featureSet.features[0].geometry; var graphic = new Graphic(AOI, symbol); var features = []; features.push(graphic); var fSet = new FeatureSet(); fSet.features = features; var params2 = { "Layers_to_Clip": clipLayers, "Area_of_Interest": fSet, "Feature_Format": registry.byId("formatBox").get("value") }; domStyle.set(loading1, "display", "inline-block"); gp.submitJob(params2, completeCallback, statusCallback1, function (error) { alert(error); domStyle.set(loading1, "display", "none"); }); }); //} } }
The way you have it written, you're defining functions within each case, but never calling them. And be aware that the way it's currently written where you keep defining the variable extractMethod, it will only run the "extractByUnit" method. This value should be set outside the function. Try something like this
case "extractByUnit": //function executeQueryTask1(unit) { var clipLayers = []; if (registry.byId("layer1").get("checked")) { clipLayers.push("SRA"); } if (registry.byId("layer2").get("checked")) { clipLayers.push("Burn"); } if (clipLayers.length === 0) { alert("No layers to clip, please select a layer you would like to clip..."); return; } var unit = document.getElementById("sel_unit").value var queryTask = new QueryTask("http://webgisdevint1/arcgis/rest/services/Alex_Try/Layers/MapServer/1"); queryTask.on("complete", addToMap2) var query = new Query(); query.returnGeometry = true; query.outFields = ["UNITCODE"]; query.where = "UNITCODE = '" + unit + "'"; query.outSpatialReference = map.spatialReference; queryTask.execute(query, function (featureSet) { var AOI = featureSet.features[0].geometry; var graphic = new Graphic(AOI, symbol); var features = []; features.push(graphic); var fSet = new FeatureSet(); fSet.features = features; var params2 = { "Layers_to_Clip": clipLayers, "Area_of_Interest": fSet, "Feature_Format": registry.byId("formatBox").get("value") }; domStyle.set(loading1, "display", "inline-block"); gp.submitJob(params2, completeCallback, statusCallback1, function (error) { alert(error); domStyle.set(loading1, "display", "none"); }); }); //} break; } } function addToMap(results){ map.graphics.clear(); var featureArray = results.featureSet.features; var feature = featureArray[0]; var graph = map.graphics.add(feature.setSymbol(symbol).setInfoTemplate(infoTemplate)); var extent = esri.graphicsExtent(map.graphics.graphics); map.setExtent(extent, true); } function addToMap2(results){ map.graphics.clear(); var featureArray = results.featureSet.features; var feature = featureArray[0]; var graph = map.graphics.add(feature.setSymbol(symbol).setInfoTemplate(infoTemplate)); var extent = esri.graphicsExtent(map.graphics.graphics); map.setExtent(extent, true); } function completeCallback(jobInfo) { if (jobInfo.jobStatus !== "esriJobFailed") { gp.getResultData(jobInfo.jobId, "output_zip", downloadFile); } } function statusCallback(jobInfo) { var status = jobInfo.jobStatus; if ( status === "esriJobFailed" ) { alert(status); domStyle.set("loading", "display", "none"); } else if (status === "esriJobSucceeded"){ domStyle.set("loading", "display", "none"); } } function statusCallback1(jobInfo) { var status = jobInfo.jobStatus; if ( status === "esriJobFailed" ) { alert(status); domStyle.set("loading1", "display", "none"); } else if (status === "esriJobSucceeded"){ domStyle.set("loading1", "display", "none"); } } function statusCallback2(jobInfo) { var status = jobInfo.jobStatus; if ( status === "esriJobFailed" ) { alert(status); domStyle.set("loading2", "display", "none"); } else if (status === "esriJobSucceeded"){ domStyle.set("loading2", "display", "none"); } } function downloadFile(outputFile) { map.graphics.clear(); var theurl = outputFile.value.url; window.location = theurl; }
case "extractByUnit": executeQueryTask1(unit); function executeQueryTask1(unit) { var clipLayers = []; if (registry.byId("layer1").get("checked")) { clipLayers.push("SRA"); } etc.
"addtomap" is a function that i call outside the switch function.