<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Multiple actions on Layerlist in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/multiple-actions-on-layerlist/m-p/1221571#M79004</link>
    <description>&lt;P&gt;Hi, I was wondering if there is a way to add additional actions to the layer list. I currently have the ability to change opacity but I also want to implement the option to turn labels on/off but I don't know how to show both actions. I don't want to create another layer list&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;meta
      name="viewport"
      content="initial-scale=1, maximum-scale=1,user-scalable=no"
    /&amp;gt;

    &amp;lt;title&amp;gt;
      LayerList widget with actions | Sample | ArcGIS API for JavaScript 4.24
    &amp;lt;/title&amp;gt;

    &amp;lt;style&amp;gt;
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
        overflow: hidden;
      }
    &amp;lt;/style&amp;gt;

    &amp;lt;link
      rel="stylesheet"
      href="https://js.arcgis.com/4.24/esri/themes/light/main.css"
    /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.24/"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;script&amp;gt;
      require([
        "esri/Map",
        "esri/views/MapView",
        "esri/layers/GroupLayer",
        "esri/layers/MapImageLayer",
        "esri/widgets/LayerList",
        "esri/widgets/Slider",
        "esri/widgets/Expand",
        "esri/widgets/Legend",
        "esri/widgets/BasemapToggle"
      ], (Map, MapView, GroupLayer, MapImageLayer, LayerList, Slider, Expand, Legend, BasemapToggle) =&amp;gt; {
        const USALayer = new MapImageLayer({
          url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer",
          title: "US Sample Data"
        });


        const censusLayer = new MapImageLayer({
          url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer",
          title: "US Sample Census",
          visible: false
        });


        const layer1 = new GroupLayer({
          title: "USALayer",
          visible: true,
          visibilityMode: "exclusive",
          layers: [USALayer],
          opacity: 1
        });

        const layer2 = new GroupLayer({
          title: "CensusLayer",
          visible: true,
          visibilityMode: "exclusive",
          layers: [censusLayer],
          opacity: 0.75
        });

        const map = new Map({
          basemap: "gray-vector",
          layers: [layer1, layer2],
        });

        const view = new MapView({
          center: [-98.5795, 39.8282],
          zoom: 4,
          container: "viewDiv",
          map: map
        });
        
        const toggle = new BasemapToggle({
          view: view,
          nextBasemap: "hybrid"
        });
        

        function defineActions(event) {
      

          const item = event.item;
          if (item.children.length &amp;gt; 1 &amp;amp;&amp;amp; item.parent) {
            const slider = new Slider({
              min: 0,
              max: 1,
              precision: 2,
              values: [1],
              visibleElements: {
                labels: true,
                rangeLabels: true
              }
            });

            item.panel = {
              content: slider,
              className: "esri-icon-sliders-horizontal",
              title: "Change layer opacity"
            };

            slider.on("thumb-drag", (event) =&amp;gt; {
              const { value } = event;
              item.layer.opacity = value;
            });
          }
        }
        view.when(() =&amp;gt; {
          const layerList = new LayerList({
            view: view,
            listItemCreatedFunction: defineActions
          });

          layerList.on("trigger-action", (event) =&amp;gt; {
            const visibleLayer = USALayer.visible ? USALayer : censusLayer;

            const id = event.action.id;

            if (id === "increase-opacity") {

              if (demographicGroupLayer.opacity &amp;lt; 1) {
                demographicGroupLayer.opacity += 0.25;
              }
            } else if (id === "decrease-opacity") {

              if (demographicGroupLayer.opacity &amp;gt; 0) {
                demographicGroupLayer.opacity -= 0.25;
              }
            }
          });
          view.ui.add(layerList, "top-right");
          const legend = new Expand({
              content: new Legend({
                view: view,
                //style: "card"
              }),
              view: view
          });
          view.ui.add(legend, "bottom-left");
          view.ui.add(toggle, "top-left");

        });
      });
      
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body&amp;gt;
    &amp;lt;div id="viewDiv"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 13 Oct 2022 15:15:55 GMT</pubDate>
    <dc:creator>TheGamer</dc:creator>
    <dc:date>2022-10-13T15:15:55Z</dc:date>
    <item>
      <title>Multiple actions on Layerlist</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/multiple-actions-on-layerlist/m-p/1221571#M79004</link>
      <description>&lt;P&gt;Hi, I was wondering if there is a way to add additional actions to the layer list. I currently have the ability to change opacity but I also want to implement the option to turn labels on/off but I don't know how to show both actions. I don't want to create another layer list&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="utf-8" /&amp;gt;
    &amp;lt;meta
      name="viewport"
      content="initial-scale=1, maximum-scale=1,user-scalable=no"
    /&amp;gt;

    &amp;lt;title&amp;gt;
      LayerList widget with actions | Sample | ArcGIS API for JavaScript 4.24
    &amp;lt;/title&amp;gt;

    &amp;lt;style&amp;gt;
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
        overflow: hidden;
      }
    &amp;lt;/style&amp;gt;

    &amp;lt;link
      rel="stylesheet"
      href="https://js.arcgis.com/4.24/esri/themes/light/main.css"
    /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.24/"&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;script&amp;gt;
      require([
        "esri/Map",
        "esri/views/MapView",
        "esri/layers/GroupLayer",
        "esri/layers/MapImageLayer",
        "esri/widgets/LayerList",
        "esri/widgets/Slider",
        "esri/widgets/Expand",
        "esri/widgets/Legend",
        "esri/widgets/BasemapToggle"
      ], (Map, MapView, GroupLayer, MapImageLayer, LayerList, Slider, Expand, Legend, BasemapToggle) =&amp;gt; {
        const USALayer = new MapImageLayer({
          url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer",
          title: "US Sample Data"
        });


        const censusLayer = new MapImageLayer({
          url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer",
          title: "US Sample Census",
          visible: false
        });


        const layer1 = new GroupLayer({
          title: "USALayer",
          visible: true,
          visibilityMode: "exclusive",
          layers: [USALayer],
          opacity: 1
        });

        const layer2 = new GroupLayer({
          title: "CensusLayer",
          visible: true,
          visibilityMode: "exclusive",
          layers: [censusLayer],
          opacity: 0.75
        });

        const map = new Map({
          basemap: "gray-vector",
          layers: [layer1, layer2],
        });

        const view = new MapView({
          center: [-98.5795, 39.8282],
          zoom: 4,
          container: "viewDiv",
          map: map
        });
        
        const toggle = new BasemapToggle({
          view: view,
          nextBasemap: "hybrid"
        });
        

        function defineActions(event) {
      

          const item = event.item;
          if (item.children.length &amp;gt; 1 &amp;amp;&amp;amp; item.parent) {
            const slider = new Slider({
              min: 0,
              max: 1,
              precision: 2,
              values: [1],
              visibleElements: {
                labels: true,
                rangeLabels: true
              }
            });

            item.panel = {
              content: slider,
              className: "esri-icon-sliders-horizontal",
              title: "Change layer opacity"
            };

            slider.on("thumb-drag", (event) =&amp;gt; {
              const { value } = event;
              item.layer.opacity = value;
            });
          }
        }
        view.when(() =&amp;gt; {
          const layerList = new LayerList({
            view: view,
            listItemCreatedFunction: defineActions
          });

          layerList.on("trigger-action", (event) =&amp;gt; {
            const visibleLayer = USALayer.visible ? USALayer : censusLayer;

            const id = event.action.id;

            if (id === "increase-opacity") {

              if (demographicGroupLayer.opacity &amp;lt; 1) {
                demographicGroupLayer.opacity += 0.25;
              }
            } else if (id === "decrease-opacity") {

              if (demographicGroupLayer.opacity &amp;gt; 0) {
                demographicGroupLayer.opacity -= 0.25;
              }
            }
          });
          view.ui.add(layerList, "top-right");
          const legend = new Expand({
              content: new Legend({
                view: view,
                //style: "card"
              }),
              view: view
          });
          view.ui.add(legend, "bottom-left");
          view.ui.add(toggle, "top-left");

        });
      });
      
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body&amp;gt;
    &amp;lt;div id="viewDiv"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2022 15:15:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/multiple-actions-on-layerlist/m-p/1221571#M79004</guid>
      <dc:creator>TheGamer</dc:creator>
      <dc:date>2022-10-13T15:15:55Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple actions on Layerlist</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/multiple-actions-on-layerlist/m-p/1221659#M79009</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/595823"&gt;@TheGamer&lt;/a&gt;&amp;nbsp;, take a look at this sample.&amp;nbsp; It shows how to add multiple actions to the LayerList widget.&lt;/P&gt;&lt;P&gt;&lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/widgets-layerlist-actions/" target="_blank"&gt;https://developers.arcgis.com/javascript/latest/sample-code/widgets-layerlist-actions/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2022 17:01:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/multiple-actions-on-layerlist/m-p/1221659#M79009</guid>
      <dc:creator>Sage_Wall</dc:creator>
      <dc:date>2022-10-13T17:01:23Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple actions on Layerlist</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/multiple-actions-on-layerlist/m-p/1221687#M79012</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/507049"&gt;@Sage_Wall&lt;/a&gt;&amp;nbsp;I tried to implement that for labels, but does not work, here's my code:&amp;nbsp;&lt;A href="https://codepen.io/Charanb98/pen/bGMzvPX" target="_blank"&gt;https://codepen.io/Charanb98/pen/bGMzvPX&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2022 17:58:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/multiple-actions-on-layerlist/m-p/1221687#M79012</guid>
      <dc:creator>TheGamer</dc:creator>
      <dc:date>2022-10-13T17:58:37Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple actions on Layerlist</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/multiple-actions-on-layerlist/m-p/1221850#M79022</link>
      <description>&lt;P&gt;I wrote a codepen to demonstrate one way to add both an opacity slider and a button to toggle labels.&amp;nbsp; It's not a prefect implementation but hopefully will get you started in the right direction.&lt;/P&gt;&lt;P&gt;&lt;A href="https://codepen.io/sagewall/pen/zYjeJya?editors=1000" target="_blank"&gt;https://codepen.io/sagewall/pen/zYjeJya?editors=1000&lt;/A&gt;&lt;/P&gt;&lt;P&gt;This was made by combining parts of&amp;nbsp; these two samples:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;&lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/widgets-layerlist-actions/" target="_blank" rel="nofollow noopener noreferrer"&gt;https://developers.arcgis.com/javascript/latest/sample-code/widgets-layerlist-actions/&lt;/A&gt;&lt;/LI&gt;&lt;LI&gt;&lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/layers-mapimagelayer-dynamic-labels/" target="_blank"&gt;https://developers.arcgis.com/javascript/latest/sample-code/layers-mapimagelayer-dynamic-labels/&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;I hope this helps&lt;/P&gt;</description>
      <pubDate>Thu, 13 Oct 2022 22:40:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/multiple-actions-on-layerlist/m-p/1221850#M79022</guid>
      <dc:creator>Sage_Wall</dc:creator>
      <dc:date>2022-10-13T22:40:04Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple actions on Layerlist</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/multiple-actions-on-layerlist/m-p/1222198#M79032</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/507049"&gt;@Sage_Wall&lt;/a&gt;&amp;nbsp;Thank you so much, i just have a question how is your code able to change opacity even though you dont have a trigger&lt;/P&gt;</description>
      <pubDate>Fri, 14 Oct 2022 19:50:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/multiple-actions-on-layerlist/m-p/1222198#M79032</guid>
      <dc:creator>TheGamer</dc:creator>
      <dc:date>2022-10-14T19:50:00Z</dc:date>
    </item>
    <item>
      <title>Re: Multiple actions on Layerlist</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/multiple-actions-on-layerlist/m-p/1222233#M79035</link>
      <description>&lt;P&gt;Glad to help,&lt;/P&gt;&lt;P&gt;The defineActions() method is assigned to a property called &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-LayerList.html#listItemCreatedFunction" target="_blank" rel="noopener"&gt;listItemCreatedFunction&lt;/A&gt; which is function that executes each time a &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-LayerList-ListItem.html" target="_blank" rel="noopener"&gt;ListItem&lt;/A&gt; is created. So we first get a reference for the ListItem in our defineActions() method as a constant named item.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function defineActions(event) {
    // The event object contains an item property.
    // This is a ListItem referencing the associated layer
    // and other properties.
    const item = event.item;

    .....&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then later in the same defineActions() method (around line 238) we use that ListItem to change the associated layer's opacity to the current value of the slider.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;slider.on("thumb-drag", (event) =&amp;gt; {
    const { value } = event;
    item.layer.opacity = value;
})&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Oct 2022 22:16:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/multiple-actions-on-layerlist/m-p/1222233#M79035</guid>
      <dc:creator>Sage_Wall</dc:creator>
      <dc:date>2022-10-14T22:16:52Z</dc:date>
    </item>
  </channel>
</rss>

