Welcome to ESRI Blogs

Sychronizing map and datagrid interaction with the ArcGIS API for Flex

Example application with map and data grid synchronizationThere are many samples in the Flex API resource center that show how to render the results of queries on to a graphics layer and also how to populate a component such as datagrid with attributes. The example application discussed in this post shows how mouse events can be synchronized between the graphics layer and the datagrid.

As you hover your mouse over a state, both the state and its corresponding information in the datagrid are highlighted. Also when you hover your mouse pointer over a datagrid row, the corresponding state gets highlighted. This approach adds a greater degree of interactivity to your Web application.

Let’s see how the mouse events are synchronized in this example. Events are an important concept in the Flex framework that enable an application to respond to user interactions. Flex components have built in events that generate and dispatch events, and components can also listen for other events. Refer to Adobe’s Flex framework documentation to learn more about this important concept.

In this example we will add event listeners that will react to the mouse-over and mouse-out events on the graphic and the roll-out and roll-over events on datagrid.

  1. Add listeners to each graphic that can respond to mouse-over and mouse-out events. Before adding each graphic to the graphics layer, add event listeners like this:

    for each( var graphic : Graphic in featureSet.features )
    {
      graphic.addEventListener( MouseEvent.MOUSE_OVER, onMouseOver );
      graphic.addEventListener( MouseEvent.MOUSE_OUT, onMouseOut );
      myGraphicsLayer.add( graphic );    			
    }    
    

    When the mouse cursor enters the boundary of a graphic, the onMouseOver function is called and when the cursor leaves the boundary of a graphic, the onMouseOut function is called. Here’s some more detail on what these two functions do.

    onMouseOver: Obtains the graphic from the event and changes its symbol. Loops through all the rows in the datagrid and finds the index of the object that matches the attributes object of the graphic. Assigns this index as the selectedIndex of the datagrid. Optionally you can also use this index such that the datagrid automatically scrolls to that row.

    private function onMouseOver( event : MouseEvent ) : void
    {
      var graphic : Graphic = Graphic( event.target );
      graphic.symbol = highlightSymbol;
        			
      for each( var attributes : Object in resultsGrid.dataProvider )
      {
        if (attributes === graphic.attributes)
        {
          resultsGrid.selectedIndex = (resultsGrid.dataProvider as ArrayCollection).getItemIndex(attributes)						
        }
      }	    	    
      resultsGrid.scrollToIndex(resultsGrid.selectedIndex);
    }
    

    onMouseOut: Resets the symbol of the graphic to its original symbol and resets the selectedIndex of the datagrid.

    private function onMouseOut( event : MouseEvent ) : void
    {   			   			    		
      Graphic( event.target ).symbol = resultsSymbol;	    		  			
      resultsGrid.selectedIndex = -1;  			
    }
    
  2. Add functions that can respond to the roll-over and roll-out event of the datagrid. When a user hovers the mouse cursor over a row in the datagrid , the onItemRollOver function is called and when the cursor leaves the row, the onItemRollOut function is called. In both these functions, the symbol associated with graphic is changed accordingly. The implementation of the functions looks like this:

    private function onItemRollOver( event : ListEvent ) : void
    {
      findGraphicByAttribute(event.itemRenderer.data).symbol = highlightSymbol;    			    			
    }
        		
    private function onItemRollOut( event : ListEvent ) : void
    {
      findGraphicByAttribute(event.itemRenderer.data).symbol = resultsSymbol;
    }
    	
    public function findGraphicByAttribute( attributes : Object ) : Graphic
    {
      for each( var graphic : Graphic in myGraphicsLayer.graphicProvider)
      {
        if ( graphic.attributes === attributes)
        {
          return graphic;
        }
      }			
      return null;
    }
    

In this article you have seen how you can take advantage of the Flex eventing mechanism to add more interactivity to your application. To view and download the source for the example application, right click anywhere outside the map and click “View Source”.

Contributed by Antony Jayaprakash of the ArcGIS API for Flex development team

Published Thursday, February 19, 2009 3:42 PM by sterlingdq
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Sychronizing map and datagrid interaction with the ArcGIS API for Flex

Fantastic example Antony - thank you. Its amazing how little code that takes.
Friday, February 20, 2009 5:29 PM by Greg

# re: Sychronizing map and datagrid interaction with the ArcGIS API for Flex

Thanks for posting this. Could you show us how to do this with the Javascript API? That's the language I'm working with. I'm a rank novice and would appreciate the guidance. Others I met this week at the Fed UC would likewise.
Monday, February 23, 2009 4:04 AM by Don

# re: Sychronizing map and datagrid interaction with the ArcGIS API for Flex

@Don- There are a couple of samples available that show map/grid interaction with the JavaScript API.

This example shows how you can highlight features on the map while mousing over rows. This uses the DojoX 1.1 grid (If you try the live sample, zoom out once and click on a parcel to fill the grid):

http://resources.esri.com/arcgisserver/apis/javascript/arcgis/index.cfm?fa=codeGalleryDetails&scriptID=15733

This sample shows interaction with the Dojo 1.2 data grid through the click event, but you could do similar things with the mouse over and mouse out events:

http://resources.esri.com/help/9.3/arcgisserver/apis/javascript/arcgis/help/jssamples/find_map_datagrid.html

Monday, February 23, 2009 9:17 AM by sterlingdq

# re: Sychronizing map and datagrid interaction with the ArcGIS API for Flex

I'm attempting to follow your article above and am stumped on something. I added the "OnItemRollOver(event);" function to my datagrid but when I compile I get a "type coersion" error. The OnItemrollOver function wants to reference a ListEvent type but the datagrid "rollOver" event passes a MouseEvent. What am I overlooking? Thanks, -Royce (royce.simpson@greeleygov.com)
Monday, April 27, 2009 12:44 PM by Royce Simpson

# re: Sychronizing map and datagrid interaction with the ArcGIS API for Flex

is there anyway to make sure that the mouse is hovering over the graphic for say, more than 3 secs, before highlighting in the grid?
Wednesday, April 29, 2009 3:04 AM by mei

# re: Sychronizing map and datagrid interaction with the ArcGIS API for Flex

is it possible to determine whether the mouse has actually been placed over the polygon for, say, 5 secs, before the pop-up information is displayed?
Wednesday, April 29, 2009 3:46 AM by mei

# re: Sychronizing map and datagrid interaction with the ArcGIS API for Flex

Is it possible to do multiple selection and hilights on the datagrid?
Wednesday, June 17, 2009 4:15 AM by Nipa Parikh

Leave a Comment

(required) 
required 
(required)