Add/Remove Layers 2.0

1386
6
09-18-2024 12:48 PM

Add/Remove Layers 2.0

This is a major update to my previous add/remove layers widget. In the old version, map layers would not be recognized by Experience Builder as valid datasources. In this version, FeatureLayers added to this widget will be converted to datasources and should operate within the Experience Builder framework as if they were added from the official Add Data Widget. This means you will have access to the Add To Table and Export Actions, the Select Widget and the Edit Widget. (I have not tested with the Edit Widget, but it should work.) Note: Any widget which requires setup in their Settings Panel to operate will not be able to pick up these layers.

How it works:

Layers will be added to the map starting at the highest available index of your map layer. FeatureLayers will be converted to a datasource and re-added to the map with the prefix 'custom_' added to their ids. (If you need to access these layers for other custom widgets, their permanent id will be `custom_${layer.mapId}`. MapImageLayers will not have their ids changed.) The originally added FeatureLayer will then be removed from the map. This adding and removing layers and the dependency on the order promises are resolved will result in a pseudo-random layer order. After the amount of delay designated in the Settings Panel, the layers will sort themselves based on the order designated in the groups starting at the index number also designated in the Settings Panel. If the layers are loaded as single layers, they will not re-sort.

If your user has loaded their layers with the layer groups and they use the drag-and-drop reordering in the Map Layers Widget, the Default Layer Order button will instantly restore the order designated by your groups.

How to use:

Open /src/components/layers.ts and replace the dummy data with the layers and groups you want to use. I recommend setting your Map Widget to a WebMap with only a basemap and no layer data and loading all of your data from this widget. By default, groups[0] will appear selected on load, you may wish to set this to null or a different group. If you chose to have a group selected on load, each layer in that group should be set to activeOnLoad: true. Every layer must be designated as a FeatureLayer or a MapImageLayer by using the layerType property and setting it to layerType.feature or layerType.mapImage. In the Settings Panel, use the dropdown menu to connect this widget to a Map Widget.

If you are loading all of your operational layers from this widget or you want your layers added below any WebMap layers, set the Start Adding Layers At Index value to 0. Higher numbers will cause your layers to paint on top of any WebMap layers with a lower index. Out of range values could cause unexpected results. Set the Reordering Delay to slightly longer than you expect your most complicated group change (largest amount of layers and/or data being added/removed at once) to take. The Default Layer Order button can be switched off if you do not want to show this option to your users.

JeffreyThompson2_0-1726688669452.png

 

Attachments
Comments
JeffreyThompson2
MVP Frequent Contributor
BrianLeroux
Frequent Contributor

@JeffreyThompson2 Did you take a look at the solution @jwilner_amica posted here? https://community.esri.com/t5/arcgis-experience-builder-questions/loss-of-renderer-with-runtime-crea... 

Seems like it might be a better solution and avoids adding/removing layers and using timeouts. 

JeffreyThompson2
MVP Frequent Contributor
  1. Time and Timing: This is a widget developed specifically for sharing to the Community. It is a slimed down version of a widget developed for a single specific application. My employer is very generous allowing and encouraging me to be very active on this site and it has paid off for us on several occasions, but I try not to take advantage of that generosity. By the time @jwilner_amica posted their solution, I was putting the finishing touches on this widget. I did not want to go back and re-write and re-debug again for a third time for a marginal improvement.
  2. Ease-of-use: There are a number of required attributes in the DataSourceJson necessary for this widget to function. By adding the data to the map first as a FeatureLayer, I can access the required data from the layer with just a url. Otherwise, it would require many more required inputs in the layers.ts.
  3. Re-ordering: The pseudo-random initial ordering would still be a problem even without the initial FeatureLayer load. Eliminating the re-ordering timeout would require a major redesign.
BrianLeroux
Frequent Contributor

Makes sense. Your time and info on this community is definitely appreciated as we all wade through the murky waters with ExB Dev. 

Also here is a bit of feedback. I grabbed your widget to give it a try and see an issue that I recently found with a custom widget I built. It seems like re-ordering the layers breaks the extended functionality gained by utilizing the data manager. If I add the bigfoot layer individually, it stays at the top and the options to add to table, set filter, etc are available. However, when that layer is added through the Animal Sightings group, the re-order happens and the layer context menu gets stripped down to the 4 basic options. I work with Jwilner_Amica so we are going to try to figure that issue out next.

Update: I confirmed the same behavior with the Add Data Widget. Possible bug with the Layer List Widget.

BrianLeroux_0-1726837710251.png

 

BrianLeroux_1-1726837737945.png

 

JeffreyThompson2
MVP Frequent Contributor

@BrianLeroux https://community.esri.com/t5/arcgis-experience-builder-questions/loss-of-runtime-created-datasource...

I tested the Add Data Widget in ArcGIS Online and found it also loses the data actions in the Map Layer Widget after re-ordering. The Online version should be the same as 1.15. So, it appears that this widget is currently only compatible with Experience Builder 1.14. If you want to use this widget in 1.15, comment out all the calls to the defaultOrder() function. Data functions should be available, but layers may load out of order.

BrianLeroux
Frequent Contributor

Thank you for the additional testing. This is a bigger issue for my team outside of the widget you created. I have created a support ticket to have a bug created and hopefully will be resolved by next release.

Version history
Last update:
‎09-18-2024 12:48 PM
Updated by:
Contributors