Hide Table of Contents
View Show find task results in a DataGrid sample in sandbox
Show find task results in a DataGrid

Description

This sample demonstrates how you can display the results of a Find Task in a Dojo DataGrid control. This control is new at Dojo 1.2, and is therefore available starting with version 1.2 of the ArcGIS JavaScript API.

The DataGrid allows users to sort and resize columns, and provides many events that you can handle. You can use these events to tie the grid to the map. In this sample, when the user clicks a row, the map zooms to the associated tax lot.

The FindTask returns an array of results. Each result contains attributes that can be pushed to an array and associated with a Dojo ItemFileReadStore. The ItemFileReadStore is a valid type of data store for the DataGrid and is appropriate for displaying task results from the ArcGIS JavaScript API. For more information on working with the DataGrid, see theWorking with the Grid Dojo tutorial.

The logic that zooms the map to the clicked row is in the onRowClickHandler function. The tax lot ID is retrieved from the clicked row, and then existing graphics are searched for that tax lot ID. When the graphic with the correct tax lot ID is found, the map is set to the extent of that graphic.

Code

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Display Find Task results in Dojo DataGrid</title>

    <link rel="stylesheet" href="https://js.arcgis.com/3.46/dijit/themes/claro/claro.css">
    <link rel="stylesheet" href="https://js.arcgis.com/3.46/dgrid/css/dgrid.css">
    <link rel="stylesheet" href="https://js.arcgis.com/3.46/esri/css/esri.css">
    <style>
        html, body {
        height: 100%;
        width: 100%;
        margin: 0;
        padding: 0;
      }

      .claro .dijitContentPane {
        font-family: "Lucida Grande","Segoe UI","Arial",sans-serif;
        font-weight: normal;
        font-size: small;
      }
    </style>

    <script src="https://js.arcgis.com/3.46/"></script>
    <script>
      require([
       "esri/map",
        "esri/tasks/FindTask",
        "esri/tasks/FindParameters",
        "esri/symbols/SimpleMarkerSymbol",

        "dgrid/Grid",
        "dgrid/Selection",
        'dojo/_base/declare',
        "dojo/on",
        "dojo/dom",
        "dijit/registry",
        "dojo/_base/array",
        "dijit/form/Button",
        "dojo/parser",

        "dijit/layout/BorderContainer",
        "dijit/layout/ContentPane",
        "dojo/domReady!"
      ], function(
        Map, FindTask, FindParameters, SimpleMarkerSymbol,
        Grid, Selection, declare, on, dom, registry, arrayUtils, Button, parser
      ) {
        var findTask, findParams;
        var map, center, zoom;
        var grid;

        parser.parse();

        registry.byId("search").on("click", doFind);

        map = new esri.Map("map", {
          basemap: "streets-vector"
        });

        //Create Find Task using the URL of the map service to search
        findTask = new FindTask("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Earthquakes_Since1970/MapServer/");

        // Create a new constructor by mixing in the components
        // Allow row selection for the grid
        var myGrid = declare([Grid, Selection]);

        //columns for the grid
        var columns = [ // the alias fields
          {'field': 'Name', 'label': 'Location'},
          {'field': 'Date_', 'label': 'When did it happen?'},
          {'field': 'Magnitude', 'label': 'Magnitude'}
        ];

        // Create an instance of Grid
        grid = new myGrid({
          columns: columns,
          selectionMode: 'single',
        }, 'grid');

        grid.on("dgrid-select", onRowClickHandler);

        map.on("load", function () {
          //Create the find parameters
          findParams = new FindParameters();
          findParams.returnGeometry = true;
          findParams.layerIds = [0];
          findParams.searchFields = ["name"];
          findParams.outSpatialReference = map.spatialReference;
          console.log("find sr: ", findParams.outSpatialReference);
        });

        function doFind() {
          //Set the search text to the value in the box
          var nameBox = dom.byId("searchText");
          findParams.searchText = dom.byId("searchText").value;
          findTask.execute(findParams, showResults);
        }

        function showResults(results) {
          //This function works with an array of FindResult that the task returns
          map.graphics.clear();
          var symbol = new SimpleMarkerSymbol();

          //create array of attributes
          var items = arrayUtils.map(results, function (result) {
            var graphic = result.feature;
            graphic.setSymbol(symbol);
            map.graphics.add(graphic);
            return result.feature.attributes;
          });

          //display the results in the grid
          grid.refresh();
          grid.renderArray(items);

          //Zoom back to the initial map extent
          map.centerAndZoom(center, zoom);
        }

        //Zoom to the feature when the user clicks a row
        function onRowClickHandler(evt) {
          var clickedRowID = event.rows[0].data.OBJECTID;
          var selectedFeature = arrayUtils.filter(map.graphics.graphics, function (graphic) {
            return ((graphic.attributes) && graphic.attributes.OBJECTID === clickedRowID);
          });
          if ( selectedFeature.length ) {
            map.centerAndZoom(selectedFeature[0].geometry,5);
          }
        }
      });
    </script>
  </head>
 <body class="claro">
 <div data-dojo-type="dijit/layout/BorderContainer" data-dojo-props="design:'headline'"  style="width:100%;height:100%;margin:0;">
    <div data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'top'" style="height:40px;">
      Location of earthquake: <input type="text" id="searchText" size="60" value="Tokyo" />
          <button id="search" data-dojo-type="dijit.form.Button" type="button" data-dojo-attach-point="button" >Search
          </button>
    </div>
    <div id="map" data-dojo-props="region:'center'" data-dojo-type="dijit/layout/ContentPane"></div>
    <div data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'bottom'" style="height:280px;">
      <div id="grid" style="height:98%" ></div>
    </div>
  </body>
</html>
 
          
Show Modal