Select to view content in your preferred language

Only show selected records in Attribute Table

2383
0
12-11-2013 05:51 AM
ionarawilson1
Deactivated User
I am creating a web app that uses a map to select a feature and then a table to edit related records of that feature. I got it working with some help as I never worked with related tables before. However, the table is showing all the records and not only the related records for that selected feature. What is wrong my selection complete handler? Thank you!


<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
      xmlns:s="library://ns.adobe.com/flex/spark"
      xmlns:esri="http://www.esri.com/2008/ags"
      pageTitle="Attribute Table">    
 
 
 <s:layout>
  <s:VerticalLayout gap="0"/>
 </s:layout>
 
 <fx:Script>
  <![CDATA[
   import com.esri.ags.Graphic;
   import com.esri.ags.events.AttributeTableEvent;
   import com.esri.ags.events.FeatureLayerEvent;
   import com.esri.ags.layers.supportClasses.FeatureEditResult;
   import com.esri.ags.layers.supportClasses.FeatureEditResults;
   import com.esri.ags.layers.supportClasses.Field;
   import com.esri.ags.tasks.supportClasses.Query;
   
   import mx.controls.Alert;
   import mx.events.FlexEvent;
   import mx.rpc.AsyncResponder;
   import mx.rpc.Fault;
   
   
   protected function myMap_clickHandler(event:MouseEvent):void
   {
    if (event.target is Graphic || event.target.parent is Graphic)                
    {                    
     var graphic:Graphic = event.target is Graphic ? Graphic(event.target) : Graphic(event.target.parent);
     var query:Query = new Query;
     query.objectIds = [graphic.attributes[myFeatureLayer.layerDetails.objectIdField]];
     myFeatureLayer.selectFeatures(query, FeatureLayer.SELECTION_NEW);
     myFeatureLayer.addEventListener(FeatureLayerEvent.SELECTION_COMPLETE,myFeatureLayer_SelectionCompleteHandler);
    }
    else
    {
     myFeatureLayer.clearSelection();
     yourTable.clearSelection();
    }
   }
   
   protected function myFeatureLayer_SelectionCompleteHandler(event:FeatureLayerEvent):void
   {
    var query:Query = new Query;
    query.where = "Office = " + myFeatureLayer.selectedFeatures[0].attributes.NAME_SH;
    
    yourTable.selectFeatures(query, FeatureLayer.SELECTION_NEW);
     
    
    
   }
   
   protected function fdg_updateFeatureHandler(event:AttributeTableEvent):void
   {
    const attributes:Object = {};
    //the related table is a "Table" and not a "Layer" that's why 
    //in the updateFeatureHandler you can't find the objectIdField, because layerDetails = null.
    //  const objectIdField:String = event.featureLayer.layerDetails.objectIdField;
    const objectIdField:String = event.featureLayer.tableDetails.objectIdField;
    attributes[objectIdField] = event.feature.attributes[objectIdField];
    attributes[event.field.name] = event.newValue;
    
    // change the attributes on client right away
    event.feature.attributes[event.field.name] = event.newValue;
    
    const feature:Graphic = new Graphic(null, null, attributes);
    const updates:Array = [ feature ];
    event.featureLayer.applyEdits(null, updates, null, false,
     new AsyncResponder(featureLayer_editsCompleteHandler, featureLayer_faultHandler,
      { feature: event.feature, field: event.field, oldValue: event.oldValue }));
   }
   
   protected function fdg_deleteFeaturesHandler(event:AttributeTableEvent):void
   {
    const deletes:Array = event.features;
    event.featureLayer.applyEdits(null, null, deletes, false,
     new AsyncResponder(featureLayer_editsCompleteHandler, featureLayer_faultHandler));
   }
   
   private function featureLayer_editsCompleteHandler(featureEditResults:FeatureEditResults, token:Object = null):void
   {
    var doRefresh:Boolean = false;
    
    for each (var deleteResult:FeatureEditResult in featureEditResults.deleteResults)
    {
     if (deleteResult.success === false)
     {
      Alert.show("Could not delete feature");
      doRefresh = true;
     }
    }
    
    for each (var updateResult:FeatureEditResult in featureEditResults.updateResults)
    {
     const feature:Graphic = token.feature;
     if (updateResult.success === false)
     {
      Alert.show("Could not update feature, Restoring old value", "Error");
      
      const field:Field = token.field;
      feature.attributes[field.name] = token.oldValue;
      doRefresh = true;
     }
     else
     {
      feature.refresh();
     }
    }
    
    if (doRefresh)
    {
     myAttributeTable.refresh();
    }
   }
   
   private function featureLayer_faultHandler(fault:Fault, token:Object = null):void
   {
    Alert.show(fault.faultString, "Fault");
    myAttributeTable.refresh();
   }
   
   

   
  ]]>    
  

 </fx:Script>

 <fx:Declarations>
  
  <esri:FeatureLayer id="yourTable" outFields="*" url="http://tfsgis-iisd01:6080/arcgis/rest/services/SARS_WILSON/RELATED_TABLES/FeatureServer/1"/>    
 </fx:Declarations>            
 
 <s:controlBarContent>        
  <s:RichText width="100%">
   This sample demonstrates how to use the AttributeTable component which allows viewing and editing feature atttributes.
   
   
  </s:RichText>    
 </s:controlBarContent>
 <esri:Map id="myMap"
     width="100%" height="62%"
     click="myMap_clickHandler(event)">        
  <esri:extent>            
   <esri:Extent id="sheepfire" xmin="-12414729" ymin="2939999" xmax="-9838830" ymax="4415185">             
    <esri:SpatialReference wkid="102100"/>            
   </esri:Extent>        
  </esri:extent>        
  <esri:ArcGISTiledMapServiceLayer url="http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/>        
  <esri:FeatureLayer id="myFeatureLayer" mode="onDemand" outFields="*" url="http://tfsgis-iisd01:6080/arcgis/rest/services/SARS_WILSON/RELATED_TABLES/FeatureServer/0"/>    
  
 </esri:Map>
 <s:Line width="100%">        
  <s:stroke>            
   <s:SolidColorStroke color="0x000000" weight="1"/>        
  </s:stroke>    
 </s:Line>
 <s:BorderContainer width="100%" height="43%"
        backgroundColor="0xEEEEEE"
        borderVisible="false">        
  <s:layout>            
   <s:HorizontalLayout paddingLeft="5" paddingRight="5" paddingTop="2"/>        
  </s:layout>        
  <esri:AttributeTable 
   id="myAttributeTable"
   width="100%" height="55%"
   deleteFeatures="fdg_deleteFeaturesHandler(event)"
   featureLayer="{yourTable}"
   
   updateFeature="fdg_updateFeatureHandler(event)">            
  </esri:AttributeTable>    
 </s:BorderContainer>
 
</s:Application>


Tags (2)
0 Kudos
0 Replies