DuncanRager

Multiple Layer Info Query to Datagrid

Discussion created by DuncanRager on Jul 29, 2011
Greetings,

I just figured out a technique to output the fields of many different layers (with different fields) into a datagrid from an info query... it seems to have decent performance, and so I thought I'd share it here, since I couldn't find a similar sample around. Also, it could be that this isn't the best way to go about this, and someone might have some critique.

I set most of the identity parameters up when I construct my map service, as well setup an event handler for when a user clicks the map. When constructing the structure for the datagrid, I specify certain fields I want to hide from the user, as well as ensure a wide column for the "Notes" field, which I know typically contains large strings (Dojo does not recommend width="auto", as it isn't fully supported and can cause some funky errors). Further down the line, I check to see if the datagrid exists, and if it does, can just update the structure (to accommodate new fields) and store.

Importantly, this is setup for identifying a single feature... to alter it for multiple features, I might just remove the allItems.slice(0,1) and tweak the graphics section to handle multiple geometries. With the single feature in mind, I added the grid.canSort function at the end to disable the column sorting... without this little bit, the column headers add an annoying little arrow on top of the label when clicked that throws the layout off a bit.

Hope this helps some folks. 

function constructLayers(){
  fLayers = new esri.layers.ArcGISDynamicMapServiceLayer("http://server/ArcGIS/rest/services/features/MapServer");
  allLayers.push({layer:fLayers,title:'Features'});
  gsvc = new esri.tasks.GeometryService("http://server/ArcGIS/rest/services/Geometry/GeometryServer");

  dojo.connect(map, "onClick", executeIdentityTask);
  
  identityTask = new esri.tasks.IdentifyTask("http://server/ArcGIS/rest/services/features/MapServer");
  identityParams = new esri.tasks.IdentifyParameters();
  identityParams.layerOption = esri.tasks.IdentifyParameters.LAYER_OPTION_TOP;
  identityParams.tolerance = 6;
  identityParams.width = map.width;
  identityParams.height = map.height;
  identityParams.returnGeometry = true;
 }

function executeIdentityTask(evt){
  map.graphics.clear();
  
  identityParams.mapExtent = map.extent;
  identityParams.geometry = evt.mapPoint;
  identityTask.execute(identityParams, function(layerInfo){
   displayInfo(layerInfo);
  });
 }

function displayInfo(layerInfo){
  /* Graphics */
  var symbol = new esri.symbol.SimpleMarkerSymbol();
  var graphic = layerInfo[0].feature;
  graphic.setSymbol(symbol);
  map.graphics.add(graphic);
  
  /* Datagrid */
  var allItems = dojo.map(layerInfo, function(layerInfo){
   return layerInfo.feature.attributes;
  });
  
  var items = allItems.slice(0, 1);
  
  var data = {
   items: items
  };
  
  var currentStore = new dojo.data.ItemFileReadStore({
   data: data
  });
  
  var currentLayout = []
  for (fieldName in items[0]) {
   if (fieldName == "OBJECTID"||fieldName == "ROWGUID"|| fieldName == "Shape"){
    var addField = {
    field: fieldName, hidden: true
    };
    currentLayout.push(addField);
   }
   else if (fieldName == "NOTES"){
    var addField = {
    field: fieldName, width: "500px"
    };
    currentLayout.push(addField);
   }
   else{
    var addField = {
    field: fieldName
    };
    currentLayout.push(addField);
   }
  }
  
  if (grid === null || grid === undefined) {
   /* Create Grid */
   grid = new dojox.grid.DataGrid({
    store: currentStore,
    structure: currentLayout
   }, document.createElement('div'));
   
   dojo.byId("gridPane").appendChild(grid.domNode);
   grid.startup();
  }
  else {
   /* Update Grid */
   grid.setStructure(currentLayout);
   grid.setStore(currentStore);
  }
  
  grid.canSort = function(){
   return false;
  }
 }


And to avoid identifying features in layers that are "visible" but turned off, I include a...

identityParams.layerIds = visible;


...line in both the portion of my code that produces a layer checklist ("visible" being an array of those layers that are turned on), and the portion that updates the visibility. See examples of returning a visible layers array in the sample here...

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples/map_dynamiclayerlist.html

Outcomes