how to view/add client side feature layer into table widget ?

1415
14
Jump to solution
12-16-2024 04:17 AM
mohannainar1
Occasional Contributor

I created new feature layer from client side and added into map in experience builder developer edition.

Now I want to see the layer in table widget. 

In web app builder, once we add the layer programmatically the attribute information will be displayed automatically. Not sure how to implement in experience builder

 

 

  const activeViewChangeHandler = (jmv: JimuMapView) => {
    if (jmv) {
      setJimuMapView(jmv)
      jmv.view.when(() => {
        console.log("Map loaded");
        setIsMapLoaded(true);
        setLoading(false);  
        // Add sample graphic
        const ptGraphic = new Graphic({
          geometry: {
            type: "point",
            longitude: -118.805,
            latitude: 34.027
          },
          attributes: {
            Name: "Sample Point",
            Description: "This is a sample description"
          }
        });
        const layer = new FeatureLayer({
          source: [ptGraphic],
          fields: [{
            name: "ObjectID",
            alias: "ObjectID",
            type: "oid"
          }, {
            name: "place",
            alias: "Place",
            type: "string"
          },{
            name: "Description",
            alias: "Description",
            type: "string"
          }],
          objectIdField: "ObjectID",
          geometryType: "point"
        });
        layer.title = "Sample Layer";
        jmv.view.map.add(layer);
0 Kudos
14 Replies
Vara_PrasadM_S
Frequent Contributor

Hi @JeffreyThompson2 ,

Hope you are doing good. I request you to please guide me on creating datasource for grouplayer with featurelayers added as sub layers. I tried similar approach you suggested above for feature layers but facing errors something like below.

Vara_PrasadM_S_0-1742453209607.png

Any help would be highly appreciated. Thanks in advance

0 Kudos
JeffreyThompson2
MVP Frequent Contributor

I don't think the datasource support functions that were added in 1.16 are set up to handle GroupLayers. Back in 1.14, I tried creating datasources from FeatureLayers first and then turning them into GroupLayers, but it broke all the Datasource functionality, or at least the Select Widget. I don't know if that is still a limitation, so maybe try it that way and let us know how it goes.

GIS Developer
City of Arlington, Texas
0 Kudos
ericsamson_tract
Frequent Contributor

That is a custom function I created to transform geojsons users get from an API into a feature layer.

I was actually able to utilize @mohannainar1 code to create a function that adds a datasource to a created featurelayer:

interface AddDataSourceLayerOptions {
    layer: __esri.FeatureLayer;
    jimuMapView: JimuMapView;
    layerId: string;
    dataSourcePrefix?: string;
    zoomToLayer?: boolean;
    layerIndex?: number;
    label?: string;
  }

  async function addDataSourceLayerToMap({
    layer,
    jimuMapView,
    layerId,
    zoomToLayer = true,
    layerIndex = 0,
    label
  }: AddDataSourceLayerOptions): Promise<string> {
    const dataSourceManager = DataSourceManager.getInstance();
    
    const data: DataSourceJson = {
      id: layerId,
      layerId: layer.id,
      type: DataSourceTypes.FeatureLayer,
      label: label || layer.title
    };
    
    const dataJson = Immutable(data);
    const dataSourceOptions = {
      id: `${layerId}_client`,
      layer: layer,
      dataSourceJson: dataJson
    };
  
    try {
      const source = await dataSourceManager.createDataSource(dataSourceOptions);
  
      jimuMapView.view.map.add(layer);
      jimuMapView.createJimuLayerView(layer, jimuMapView.id, layerIndex, source, true);
  
      if (zoomToLayer) {
        await layer.when();
        if (layer.fullExtent) {
          await jimuMapView.view.goTo(layer.fullExtent);
        }
      }
  
      return source.id;
    } catch (error) {
      console.error('Error creating DataSource:', error);
      throw error;
    }
  }

// then to use it, I have a global variable called:
const layerIdCounters = new Map<string, number>();
 function getUniqueLayerId(baseId: string): string {
      const currentCount = layerIdCounters.get(baseId) || 0;
      const newCount = currentCount + 1;
      layerIdCounters.set(baseId, newCount);
      return `${baseId}_${newCount}`;
    }

    const uniqueLayerId = getUniqueLayerId('parcels_layer');
    console.log('uniqueLayerId:', uniqueLayerId)
    const sourceId = await addDataSourceLayerToMap({
        layer: api_feature_layer,
        jimuMapView: jimuMapView,
        layerId: uniqueLayerId,
        label: 'parcels_api'
    });

 

0 Kudos
ericsamson_tract
Frequent Contributor

@mohannainar1 @JeffreyThompson2

Any ideas on how to add this datasource to the attribute table widget automatically? I was able to get my attribute table to open when the functions are done by doing the following, but not getting any good documentation of sending it to the attribute table automatically:

getAppStore().dispatch(appActions.widgetStatePropChange(
        'widget_60',
        'collapse',
        true
));

 

0 Kudos
JeffreyThompson2
MVP Frequent Contributor

There is the Add To Table Action so there must be a way to trigger it programmatically, but simply don't know what it is. Maybe you can figure it out by digging around in the source code. It's probably somewhere in the Message interface. https://developers.arcgis.com/experience-builder/api-reference/jimu-core/Message/

If you want don't necessarily need the Experience Builder Table Widget. Take a look at the FeatureTable in the API. It should be easier to work with.

https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-FeatureTable.html 

GIS Developer
City of Arlington, Texas