<?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 Filter map layers using default Select component option in ArcGIS Experience Builder Questions</title>
    <link>https://community.esri.com/t5/arcgis-experience-builder-questions/filter-map-layers-using-default-select-component/m-p/1523081#M14351</link>
    <description>&lt;P&gt;Using the Select component, I would like to pre-filter (right after the map loads) map layers based on the default value of the select component. Based on my code below, by the time the default value (&lt;STRONG&gt;serviceType&lt;/STRONG&gt;) is retrieved inside &lt;STRONG&gt;UseEffect&lt;/STRONG&gt;,&amp;nbsp;&lt;STRONG&gt;await jmv.whenAllJimuLayerViewLoaded&lt;/STRONG&gt;&lt;SPAN&gt;&lt;STRONG&gt;()&lt;/STRONG&gt; is not ready so the code that filters the layers is not executed. How can I filter maps layers before any selection is made on the Select component, just using the default value?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;import { React, type AllWidgetProps } from 'jimu-core'
import { type JimuLayerView, JimuMapViewComponent, type JimuMapView } from 'jimu-arcgis'
import { Select, Option } from 'jimu-ui'
import { useEffect } from 'react'
const { useState } = React //{} allow destructuring of this constant

const Widget = (props: AllWidgetProps&amp;lt;any&amp;gt;) =&amp;gt; {
  const [jmv, setJmv] = useState&amp;lt;JimuMapView&amp;gt;()
  const [serviceType, setServiceType] = useState('Residential Trash')
  const activeViewChangeHandler = (jimumapview: JimuMapView) =&amp;gt; {
    if (jimumapview) {
      setJmv(jimumapview)
    }
  }
  useEffect(() =&amp;gt; { //runs when service type is updated by useState
    FilterMapLayers(serviceType)
  }, [serviceType])
  const UpdateServiceType = (evt) =&amp;gt; {
    setServiceType(evt.target.value)
  }
  const FilterMapLayers = async (serviceTypeString) =&amp;gt; {
    await jmv.whenAllJimuLayerViewLoaded()
    const jimuLayerViews = jmv.getAllJimuLayerViews()
    jimuLayerViews.forEach(async (jimuLayerView) =&amp;gt; {
      let whereClause = null
      const layer = jimuLayerView.layer
      if (layer.type === 'feature') {
        if (layer.title === 'OpenMarketSm' || layer.title === 'ExclusionZoneSm' || layer.title === 'LocalContentAreaSm') {
          whereClause = `ServiceAreaType = '${GetServiceTypeText(serviceTypeString)}'`
          await FilterLayerView(jimuLayerView, whereClause, props)
        } else if (layer.title === 'HOAsm' || layer.title === 'MunicipalitySm') {
          whereClause = GetWhereClauseMuniHOA(serviceTypeString)
          await FilterLayerView(jimuLayerView, whereClause, props)
        }
      }
    })
  }
  const GetServiceTypeText = (FilterString) =&amp;gt; {
    switch (FilterString) {
      case 'Residential Trash':
        return 'Resi Trash'
      case 'Residential Recycle':
        return 'Resi Recycling'
      case 'Residential Yard Waste':
        return 'Resi Yard Waste'
      case 'Commercial Trash':
        return 'Comm. Trash'
      case 'Commercial Recycle':
        return 'Comm. Recycling'
      case 'Industrial Temp':
        return 'Industrial Temp'
      case 'Sanitation Services':
        return 'Sanitation Services'
    }
  }
  const GetWhereClauseMuniHOA = (FilterString) =&amp;gt; {
    switch (FilterString) {
      case 'Residential Trash':
        return "SvcBy_ResiTrsh = 'C' or SvcBy_ResiTrsh = 'R'"
      case 'Residential Recycle':
        return "SvcBy_ResiRec = 'C' or SvcBy_ResiRec = 'R'"
      case 'Residential Yard Waste':
        return "SvcBy_ResiYW = 'C' or SvcBy_ResiYW = 'R'"
      case 'Commercial Trash':
        return "SvcBy_CommTrsh = 'C' or SvcBy_CommTrsh = 'R'"
      case 'Commercial Recycle':
        return "SvcBy_CommRec = 'C' or SvcBy_CommRec = 'R'"
      case 'Industrial Temp':
        return "SvcBy_IndsTemp = 'C' or SvcBy_IndsTemp = 'R'"
      default:
        return ''
    }
  }

  async function FilterLayerView (jimuLayerView: JimuLayerView, whereClause: any, props: AllWidgetProps&amp;lt;any&amp;gt;) {
    let datasource = null
    if (jimuLayerView.layerDataSourceId) {
      datasource = await jimuLayerView.createLayerDataSource()
    }
    if (datasource) {
      datasource.updateQueryParams({
        where: whereClause
      }, props.widgetId)
    }
  }
  return (
      &amp;lt;div&amp;gt;
      &amp;lt;Select aria-label="Name" value = {serviceType} onChange={UpdateServiceType}&amp;gt;
        &amp;lt;Option value = "Residential Trash"&amp;gt;Residential Trash &amp;lt;/Option&amp;gt;
        &amp;lt;Option value = "Residential Recycle"&amp;gt;Residential Recycle &amp;lt;/Option&amp;gt;
        &amp;lt;Option value = "Residential Yard Waste"&amp;gt;Residential Yard Waste &amp;lt;/Option&amp;gt;
        &amp;lt;Option value = "Commercial Trash"&amp;gt;Commercial Trash &amp;lt;/Option&amp;gt;
        &amp;lt;Option value = "Commercial Recycle"&amp;gt;Commercial Recycle &amp;lt;/Option&amp;gt;
        &amp;lt;Option value = "Industrial Temp"&amp;gt;Industrial Temp &amp;lt;/Option&amp;gt;
        &amp;lt;Option value = "Sanitation Services"&amp;gt;Sanitation Services &amp;lt;/Option&amp;gt;
      &amp;lt;/Select&amp;gt;
      &amp;lt;div&amp;gt;
  {props.useMapWidgetIds &amp;amp;&amp;amp; props.useMapWidgetIds.length === 1 &amp;amp;&amp;amp;
        (&amp;lt;JimuMapViewComponent useMapWidgetId={props.useMapWidgetIds?.[0]} onActiveViewChange={activeViewChangeHandler}/&amp;gt;)}
      &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
  )
}

export default Widget

//activeViewChangeHandler is called once when the map is ready
// {/* ?. checks if object is null  */}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/72016"&gt;@QunSun&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 16 Aug 2024 19:42:30 GMT</pubDate>
    <dc:creator>MK13</dc:creator>
    <dc:date>2024-08-16T19:42:30Z</dc:date>
    <item>
      <title>Filter map layers using default Select component option</title>
      <link>https://community.esri.com/t5/arcgis-experience-builder-questions/filter-map-layers-using-default-select-component/m-p/1523081#M14351</link>
      <description>&lt;P&gt;Using the Select component, I would like to pre-filter (right after the map loads) map layers based on the default value of the select component. Based on my code below, by the time the default value (&lt;STRONG&gt;serviceType&lt;/STRONG&gt;) is retrieved inside &lt;STRONG&gt;UseEffect&lt;/STRONG&gt;,&amp;nbsp;&lt;STRONG&gt;await jmv.whenAllJimuLayerViewLoaded&lt;/STRONG&gt;&lt;SPAN&gt;&lt;STRONG&gt;()&lt;/STRONG&gt; is not ready so the code that filters the layers is not executed. How can I filter maps layers before any selection is made on the Select component, just using the default value?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;import { React, type AllWidgetProps } from 'jimu-core'
import { type JimuLayerView, JimuMapViewComponent, type JimuMapView } from 'jimu-arcgis'
import { Select, Option } from 'jimu-ui'
import { useEffect } from 'react'
const { useState } = React //{} allow destructuring of this constant

const Widget = (props: AllWidgetProps&amp;lt;any&amp;gt;) =&amp;gt; {
  const [jmv, setJmv] = useState&amp;lt;JimuMapView&amp;gt;()
  const [serviceType, setServiceType] = useState('Residential Trash')
  const activeViewChangeHandler = (jimumapview: JimuMapView) =&amp;gt; {
    if (jimumapview) {
      setJmv(jimumapview)
    }
  }
  useEffect(() =&amp;gt; { //runs when service type is updated by useState
    FilterMapLayers(serviceType)
  }, [serviceType])
  const UpdateServiceType = (evt) =&amp;gt; {
    setServiceType(evt.target.value)
  }
  const FilterMapLayers = async (serviceTypeString) =&amp;gt; {
    await jmv.whenAllJimuLayerViewLoaded()
    const jimuLayerViews = jmv.getAllJimuLayerViews()
    jimuLayerViews.forEach(async (jimuLayerView) =&amp;gt; {
      let whereClause = null
      const layer = jimuLayerView.layer
      if (layer.type === 'feature') {
        if (layer.title === 'OpenMarketSm' || layer.title === 'ExclusionZoneSm' || layer.title === 'LocalContentAreaSm') {
          whereClause = `ServiceAreaType = '${GetServiceTypeText(serviceTypeString)}'`
          await FilterLayerView(jimuLayerView, whereClause, props)
        } else if (layer.title === 'HOAsm' || layer.title === 'MunicipalitySm') {
          whereClause = GetWhereClauseMuniHOA(serviceTypeString)
          await FilterLayerView(jimuLayerView, whereClause, props)
        }
      }
    })
  }
  const GetServiceTypeText = (FilterString) =&amp;gt; {
    switch (FilterString) {
      case 'Residential Trash':
        return 'Resi Trash'
      case 'Residential Recycle':
        return 'Resi Recycling'
      case 'Residential Yard Waste':
        return 'Resi Yard Waste'
      case 'Commercial Trash':
        return 'Comm. Trash'
      case 'Commercial Recycle':
        return 'Comm. Recycling'
      case 'Industrial Temp':
        return 'Industrial Temp'
      case 'Sanitation Services':
        return 'Sanitation Services'
    }
  }
  const GetWhereClauseMuniHOA = (FilterString) =&amp;gt; {
    switch (FilterString) {
      case 'Residential Trash':
        return "SvcBy_ResiTrsh = 'C' or SvcBy_ResiTrsh = 'R'"
      case 'Residential Recycle':
        return "SvcBy_ResiRec = 'C' or SvcBy_ResiRec = 'R'"
      case 'Residential Yard Waste':
        return "SvcBy_ResiYW = 'C' or SvcBy_ResiYW = 'R'"
      case 'Commercial Trash':
        return "SvcBy_CommTrsh = 'C' or SvcBy_CommTrsh = 'R'"
      case 'Commercial Recycle':
        return "SvcBy_CommRec = 'C' or SvcBy_CommRec = 'R'"
      case 'Industrial Temp':
        return "SvcBy_IndsTemp = 'C' or SvcBy_IndsTemp = 'R'"
      default:
        return ''
    }
  }

  async function FilterLayerView (jimuLayerView: JimuLayerView, whereClause: any, props: AllWidgetProps&amp;lt;any&amp;gt;) {
    let datasource = null
    if (jimuLayerView.layerDataSourceId) {
      datasource = await jimuLayerView.createLayerDataSource()
    }
    if (datasource) {
      datasource.updateQueryParams({
        where: whereClause
      }, props.widgetId)
    }
  }
  return (
      &amp;lt;div&amp;gt;
      &amp;lt;Select aria-label="Name" value = {serviceType} onChange={UpdateServiceType}&amp;gt;
        &amp;lt;Option value = "Residential Trash"&amp;gt;Residential Trash &amp;lt;/Option&amp;gt;
        &amp;lt;Option value = "Residential Recycle"&amp;gt;Residential Recycle &amp;lt;/Option&amp;gt;
        &amp;lt;Option value = "Residential Yard Waste"&amp;gt;Residential Yard Waste &amp;lt;/Option&amp;gt;
        &amp;lt;Option value = "Commercial Trash"&amp;gt;Commercial Trash &amp;lt;/Option&amp;gt;
        &amp;lt;Option value = "Commercial Recycle"&amp;gt;Commercial Recycle &amp;lt;/Option&amp;gt;
        &amp;lt;Option value = "Industrial Temp"&amp;gt;Industrial Temp &amp;lt;/Option&amp;gt;
        &amp;lt;Option value = "Sanitation Services"&amp;gt;Sanitation Services &amp;lt;/Option&amp;gt;
      &amp;lt;/Select&amp;gt;
      &amp;lt;div&amp;gt;
  {props.useMapWidgetIds &amp;amp;&amp;amp; props.useMapWidgetIds.length === 1 &amp;amp;&amp;amp;
        (&amp;lt;JimuMapViewComponent useMapWidgetId={props.useMapWidgetIds?.[0]} onActiveViewChange={activeViewChangeHandler}/&amp;gt;)}
      &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
  )
}

export default Widget

//activeViewChangeHandler is called once when the map is ready
// {/* ?. checks if object is null  */}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/72016"&gt;@QunSun&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Aug 2024 19:42:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-experience-builder-questions/filter-map-layers-using-default-select-component/m-p/1523081#M14351</guid>
      <dc:creator>MK13</dc:creator>
      <dc:date>2024-08-16T19:42:30Z</dc:date>
    </item>
    <item>
      <title>Re: Filter map layers using default Select component option</title>
      <link>https://community.esri.com/t5/arcgis-experience-builder-questions/filter-map-layers-using-default-select-component/m-p/1525564#M14430</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/72016"&gt;@QunSun&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Aug 2024 02:43:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-experience-builder-questions/filter-map-layers-using-default-select-component/m-p/1525564#M14430</guid>
      <dc:creator>MK13</dc:creator>
      <dc:date>2024-08-22T02:43:05Z</dc:date>
    </item>
  </channel>
</rss>

