Programmatically open node on layerlist 4.x?

472
1
01-07-2021 10:55 AM
MichaelSnook
Occasional Contributor III

Hi All,

Wondering if anyone has an example on how to programmatically open a layer list node?  I need to be able to open a group layer (in a dynamic map service -- not sure if that matters) when I make that node visible to show the sublayers within that node.

Thanks!

Mike

Tags (1)
1 Reply
jfeliciani
New Contributor

Hey Mike,

I know this is a bit late, but I wanted to add a response here since I was looking to do the same thing and finally came across a solution.  Hopefully this helps anyone in the future with similar issues...

This fn recurses over all of the operational items and checks if the layer id matches the passed in id.  If it matches, we expand that node and then return back up through the call stack and expand any parents of that node.  The optional part I have commented out was a use case of mine which was to hide other results that didn't match the passed in id. (ie. a search input)

 

 

function expand(items: __esri.Collection<__esri.ListItem>, id: string | null) {
  for (const item of items.toArray()) {
    if (item.layer.id === id) {
      // Found the searched for node
      item.open = true;
      return true;
    } else if (expand(item.children, id)) {
      // Open the item parent
      item.open = true;
      return true;
    } else {
      /**
       * Optionally hide other layer items that don't fall in the chain
       * Requires: ArcGIS API for JavaScript 4.24
       * @see https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-LayerList-ListItem.html#hidden
       */
      // item.hidden = id ? true : false;
    }
  }
}

// Use the operational items on the layerList class instance
expand(layerList.operationalItems, "some layer id");

 

 

 

Tags (1)