Adding a taskE-mail This Topic Printable Version Give Us Feedback

This example provides details on how to implement a task. The query task is used, which shows tabular results but does not include a map. [Show me]

The following is the HTML page in its entirety:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <title>State Info</title>
    <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/1.6/js/dojo/dijit/themes/tundra/tundra.css">
    <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script>
    <script type="text/javascript">
      dojo.require("esri.tasks.query");

      var myQueryTask, myQuery;

      function init() {
        //build query
        myQueryTask = new
esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5");

        //build query filter
        myQuery = new esri.tasks.Query();
        myQuery.returnGeometry = false;
        myQuery.outFields = ["STATE_NAME","POP2007","MALES","FEMALES"];
      }

      function execute(stateName) {
        myQuery.text = stateName;
        //execute query
        myQueryTask.execute(myQuery,showResults);
      }

      function showResults(myFeatureSet) {
        var s = "";
        for (var i=0, il=myFeatureSet.features.length; i<il; i++) {
          var featureAttributes = myFeatureSet.features[i].attributes;
          for (att in featureAttributes) {
            s = s + "<strong>" + att + ": </strong>" +
			featureAttributes[att] + "<br />";
          }
        }
        dojo.byId("info").innerHTML = s;
      }

      dojo.addOnLoad(init);
    </script>
  </head>
  <body>
    <form action="">
      US state name :
      <input type="text" id="stateName" value="California" />
      <input type="button" value="Get Details"
onclick="execute(dojo.byId('stateName').value);" />
    </form>
    <br />
    <br />
    <div id="info" class="tundra" style="padding:5px; margin:5px; background-color:#eee;">
    </div>
  </body>
</html>

The following discussion assumes you are already familiar with the basic concepts of the JavaScript API and Dojo, and you understand the basic patterns of how the JavaScript API works within an HTML page. For more information, see Adding a map.

Scripts inside HTML HEAD

The scripts section inside HTML HEAD is where you write your functions that call the JavaScript API.

    <script type="text/javascript" language="Javascript">

Reference the packages.

Since you are implementing a task, you need to reference the tasks package using dojo.require(). In this example, the "esri.tasks.query" package is referenced. For more information on what packages can be referenced, see About Dojo.

      dojo.require("esri.tasks.query");

Create the init() function.

The init() function is where the query is built. First, a QueryTask named myQueryTask is initialized using the QueryTask constructor.

      function init() {
        myQueryTask = new
esri.tasks.QueryTask("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5");

Next, you need to build the query filter.

        //build query filter
        myQuery = new esri.tasks.Query();
        myQuery.returnGeometry = false;
        myQuery.outFields = ["STATE_NAME","POP2007","MALES","FEMALES"];
      }

Create a callback function.

The callback function is the function referenced from the input in the HTML page. In this example, a function named "execute" is called after a user types in a state name and clicks the "Get Results" button.

      function execute(stateName) {
        myQuery.text = stateName;
        //execute query
        myQueryTask.execute(myQuery,showResults);
      }

Create a function to show the results.

The showResults() function first parses the results based on the information received when the QueryTask was executed in the callback function. The results are passed into the showResults function in "myFeatureSet", which is a FeatureSet object. A FeatureSet contains an array of Graphic features.

      function showResults(myFeatureSet) {

In the next section of this function, the variable "s" is populated with the field names and the field values contained in "myFeatureSet".

        var s = "";
        for (var i=0, il=myFeatureSet.features.length; i<il; i++) {
          var featureAttributes = myFeatureSet.features[i].attributes;
          for (att in featureAttributes) {
            s = s + "<strong>" + att + ": </strong>" +
			featureAttributes[att] + "<br />";
          }
        }

The next step in this function is to assign the value of "s" as innerHTML, which allows text on an HTML page to be replaced with new text. In this example, dojo.byId is used, which is similar to the document.getElementById(id) JavaScript function. This function searches and returns the first HTML element with the argument ID of "info". In this case, the text is written inside the HTML DIV with id="info".

        dojo.byId("info").innerHTML = s;
      }

Specify the addOnLoad event.

As with all HTML pages using the JavaScript API, you must specify the Dojo addOnLoad event. The parameter it takes is "init", which represents what takes place in the init() function. The final step is to include the closing script tag.

      dojo.addOnLoad(init);
    </script>

HTML BODY

The next section is the BODY section of the HTML page.

  <body>
    <form action="">
      US state name :
      <input type="text" id="stateName" value="California" />
      <input type="button" value="Get Details"
onclick="execute(dojo.byId('stateName').value);" />
    </form>
    <br />
    <br />
    <div id="info" class="tundra" style="padding:5px; margin:5px; background-color:#eee;">
    </div>
  </body>