Custom Widget Filter Not Persistent

183
2
4 weeks ago
Labels (2)
TristanJonas
Occasional Contributor

Hello,
I made a custom widget to simplify the filtering process, it applies the filter just fine (for example, filtering by state will filter out all but the selected states in a drop down menu) but the moment I click any of the feature polygons in the map to see the attributes of that feature, the filter disappears and I can see all the states again.

Here are the relevant parts of my code:

// Apply filter function from BlocksBeadFilter.tsx
const addFilter = (): void => {
  if (!layerInfo) return
  const lyr = layerInfo.lyr

  // Build age clause
  const ageClause = (() => {
    if (!ageSelected.length || ageMin.value <= 0) return ''
    const flds = ageSelected.flatMap(l => ageMap[l] || [])
    return flds.length ? `(${flds.map(f => `${f} > ${ageMin.value}`).join(' OR ')})` : ''
  })()

  // Build all clauses
  const clauses = [
    buildInClause('ProviderName', provider.selected),
    buildInClause('StateName', state.selected),
    buildInClause('CountyName', county.selected),
    ageClause,
    buildGroupClause(bslSelected, bslMin.value, f => f),
    buildGroupClause(raceSelected, raceMin.value, lbl => raceMap[lbl]),
    buildIncomeClause(incMin.value, incMax.value)
  ].filter(Boolean)

  lyr.definitionExpression = clauses.join(' AND ')
  
  // Clear all widget selections after applying the filter
  clearWidgetSelections()
}

 

// From filterUtils.ts
export const buildInClause = (field: string, values: string[]): string => {
  if (!values.length) return ''
  return `${field} IN (${values.map(val => `'${val.replace(/'/g, "''")}'`).join(', ')})`
}

export const buildGroupClause = (
  arr: string[], 
  min: number, 
  getField: (s: string) => string
): string => {
  return (arr.length && min > 0) 
    ? `(${arr.map(a => `${getField(a)} > ${min}`).join(' OR ')})` 
    : ''
}

export const buildIncomeClause = (incMin: number, incMax: number): string => {
  const parts: string[] = []
  if (incMin > 0) parts.push(`ASQPE001 >= ${incMin}`)
  if (incMax > 0) parts.push(`ASQPE001 <= ${incMax}`)
  return parts.length ? `(${parts.join(' AND ')})` : ''
}


 Thank you for your advice.

0 Kudos
2 Replies
TimWestern
MVP

So if you are making changes in a custom widget, does the custom widget communicate to any other widgets in the experience? (This likely would be an 'action' of some kind) Depending on the behavior you want it might be an selection update that needs to be fired from the custom widget and subscribed to by the other widget (map perhaps in this case)

You may want to check out one of these:

https://doc.arcgis.com/en/experience-builder/latest/configure-widgets/action-triggers.htm
https://community.esri.com/t5/arcgis-experience-builder-questions/add-a-custom-action-in-built-in-es...
https://community.esri.com/t5/arcgis-experience-builder-questions/triggering-data-actions-from-a-cus...





 

0 Kudos