<?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 Re: React to change in selection on data source from custom widget in ArcGIS Experience Builder Questions</title>
    <link>https://community.esri.com/t5/arcgis-experience-builder-questions/react-to-change-in-selection-on-data-source-from/m-p/1092732#M2672</link>
    <description>&lt;P&gt;Thanks&amp;nbsp;@Anonymous User&amp;nbsp;for your detailed response. Much appreciated.&lt;/P&gt;&lt;P&gt;That works, in my code the&amp;nbsp;&lt;STRONG&gt;onDataSourceInfoChange&lt;/STRONG&gt; also gets called now whenever the data selection changes (I don't know why it wasn't before, something must have been wrong in my code).&lt;/P&gt;&lt;P&gt;I've also noticed is that the child component of the&amp;nbsp;&lt;STRONG&gt;&amp;lt;DataSourceComponent&amp;gt;&lt;/STRONG&gt; also gets triggered every time the selection changes.&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;updateChild = (ds?: QueriableDataSource, dsInfo?: IMDataSourceInfo) =&amp;gt; {
    const selectedProjects = ds.getSelectedRecords(); // Will contain the selected records.
}

render() { 
	return (&amp;lt;div&amp;gt;
		{&amp;lt;DataSourceComponent widgetId={this.props.id} useDataSource={this.props.useDataSources[0]}
          onDataSourceCreated={this.onDs} 
          onDataSourceInfoChange={this.dataChanged}
          queryCount&amp;gt;
			{this.updateChild}
		}
      &amp;lt;/DataSourceComponent&amp;gt;
	&amp;lt;/div&amp;gt;)
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 26 Aug 2021 15:32:02 GMT</pubDate>
    <dc:creator>AlejandroMari1</dc:creator>
    <dc:date>2021-08-26T15:32:02Z</dc:date>
    <item>
      <title>React to change in selection on data source from custom widget</title>
      <link>https://community.esri.com/t5/arcgis-experience-builder-questions/react-to-change-in-selection-on-data-source-from/m-p/1089470#M2607</link>
      <description>&lt;P&gt;Hello! I'm trying to understand how can I "listen" for changes on a data source selection on a custom widget that were made on a different widget.&lt;/P&gt;&lt;P&gt;I have a feature layer data source. From one custom widget, I get hold of that that source I select some features.&amp;nbsp;&lt;/P&gt;&lt;P&gt;From a different custom widget, I want to listen on that change so I can render information appropriately.&lt;/P&gt;&lt;P&gt;This is my render function:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;render() {
    const { dataSource } = this.state;
    const useDataSource = this.props.useDataSources &amp;amp;&amp;amp; this.props.useDataSources[0]
    let query: FeatureLayerQueryParams = {
        where: "1=1",
        returnGeometry: false,
        outFields: ["*"]
    };
    return (
        &amp;lt;div&amp;gt;
            &amp;lt;DataSourceComponent
                useDataSource={useDataSource}
                query={dataSource ? query : null}
                widgetId={this.props.widgetId}
                onDataSourceCreated={this.onDataSourceCreated}
                // onQueryStatusChange={this.onQueryStatusChange}
                onDataSourceInfoChange={this.onDataSourceInfoChange}
                onCreateDataSourceFailed={this.onCreateDataSourceFailed}
            &amp;gt;
                {this.renderTheSubComponent}
            &amp;lt;/DataSourceComponent&amp;gt;
        &amp;lt;/div&amp;gt;
    )
}
onDataSourceCreated = (dataSource: QueriableDataSource): void =&amp;gt; {
    this.setState({ dataSource: dataSource });
}&lt;/LI-CODE&gt;&lt;P&gt;None of the callbacks there are called when the selection is changed. ComponentDidUpdate also doesn't get called, so my component never receives the selection.&lt;/P&gt;&lt;P&gt;Other OOTB widgets (like the List widget) are properly reacting to selection changes, so I'm positive the selection is properly done.&lt;/P&gt;&lt;P&gt;Any help will be appreciated.&lt;/P&gt;&lt;P&gt;Alejandro&lt;/P&gt;</description>
      <pubDate>Mon, 16 Aug 2021 22:53:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-experience-builder-questions/react-to-change-in-selection-on-data-source-from/m-p/1089470#M2607</guid>
      <dc:creator>AlejandroMari1</dc:creator>
      <dc:date>2021-08-16T22:53:13Z</dc:date>
    </item>
    <item>
      <title>Re: React to change in selection on data source from custom widget</title>
      <link>https://community.esri.com/t5/arcgis-experience-builder-questions/react-to-change-in-selection-on-data-source-from/m-p/1092260#M2654</link>
      <description>&lt;P&gt;I can share how I've solved this, although I'll caution I'm not sure it's&amp;nbsp;&lt;EM&gt;the best way&lt;/EM&gt;.&lt;/P&gt;&lt;P&gt;I'm using a similar pattern to how you started with the&amp;nbsp;&lt;STRONG&gt;DataSource&lt;/STRONG&gt;:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;      {&amp;lt;DataSourceComponent widgetId={this.props.id} useDataSource={this.props.useDataSources[0]}
          onDataSourceCreated={this.onDs} 
          onDataSourceInfoChange={this.dataChanged}
          queryCount&amp;gt;
        {this.layerHandler}
      &amp;lt;/DataSourceComponent&amp;gt;}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;onDataSourceInfoChange&lt;/EM&gt; will be triggered when interacting with the data. I believe any widget you've setup within the Builder that listens for Selection change within the framework should trigger this. So a selection drawn in the map widget, or you could manually perform the selection on the datasource in your first widget code. Eg.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;this.state.datasource.selectRecordById(oid)  //from the selecting widget, not the recieving widget&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From the widget you want to react to the selection change....(the widget with the 1st datasource code above) ..... in my case I'm calling into &lt;STRONG&gt;dataChanged -&amp;nbsp;&lt;/STRONG&gt;See the 2 test cases I have here:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;dataChanged = (info: IMDataSourceInfo) =&amp;gt;{
  console.log("imdatasource status", info)
  if (this.state.datasource){
    if (this.state.datasource.getStatus() == "LOADED"){
      let r = this.state.datasource.getSelectedRecords()
      console.log("SELECTED RECORDS:", r)
    }
  }
  if (info.selectedIds){
    console.log("info ids: ", info.selectedIds)
    this.selectedSite([info.selectedIds[0]])
  }
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My selected records in the first branch always comes up with an empty array. (I point this out as I would have expected the datasource to have the selection here, but it doesn't. Thus why I'm cautioning this might not be the best way). However, within the second branch&amp;nbsp;&lt;EM&gt;info.selectedIds&lt;/EM&gt; - when there is a selection, I can get the OID of the feature from that.&lt;/P&gt;&lt;P&gt;Once you have the OID, simply carry on with what you're doing. In my case, I need to get more attributes from the selected feature:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;selectedSite = (oid_facname) =&amp;gt;{

  const oid = oid_facname[0];
  //const fac_name = oid_facname[1];
  //const fac_id = oid_facname[2];

    if (this.state.datasource &amp;amp;&amp;amp; this.state.datasource.getStatus() == "LOADED"){
      if (oid){
        this.state.datasource.selectRecordById(oid)      
        const fac_name = this.state.datasource.getRecords()[0].feature.attributes['facility_name_location'] &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Alternatively, you could broadcast the OID or attributes of your selected feature and have the 2nd widget listen for that. Check the &lt;A href="https://developers.arcgis.com/experience-builder/guide/widget-communication/" target="_self"&gt;widget communication help&lt;/A&gt; to see this pattern. I only offer this up as another idea, I don't know if it'd be suitable for your widgets....&lt;/P&gt;</description>
      <pubDate>Wed, 25 Aug 2021 15:29:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-experience-builder-questions/react-to-change-in-selection-on-data-source-from/m-p/1092260#M2654</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-08-25T15:29:10Z</dc:date>
    </item>
    <item>
      <title>Re: React to change in selection on data source from custom widget</title>
      <link>https://community.esri.com/t5/arcgis-experience-builder-questions/react-to-change-in-selection-on-data-source-from/m-p/1092732#M2672</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;@Anonymous User&amp;nbsp;for your detailed response. Much appreciated.&lt;/P&gt;&lt;P&gt;That works, in my code the&amp;nbsp;&lt;STRONG&gt;onDataSourceInfoChange&lt;/STRONG&gt; also gets called now whenever the data selection changes (I don't know why it wasn't before, something must have been wrong in my code).&lt;/P&gt;&lt;P&gt;I've also noticed is that the child component of the&amp;nbsp;&lt;STRONG&gt;&amp;lt;DataSourceComponent&amp;gt;&lt;/STRONG&gt; also gets triggered every time the selection changes.&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;updateChild = (ds?: QueriableDataSource, dsInfo?: IMDataSourceInfo) =&amp;gt; {
    const selectedProjects = ds.getSelectedRecords(); // Will contain the selected records.
}

render() { 
	return (&amp;lt;div&amp;gt;
		{&amp;lt;DataSourceComponent widgetId={this.props.id} useDataSource={this.props.useDataSources[0]}
          onDataSourceCreated={this.onDs} 
          onDataSourceInfoChange={this.dataChanged}
          queryCount&amp;gt;
			{this.updateChild}
		}
      &amp;lt;/DataSourceComponent&amp;gt;
	&amp;lt;/div&amp;gt;)
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Aug 2021 15:32:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-experience-builder-questions/react-to-change-in-selection-on-data-source-from/m-p/1092732#M2672</guid>
      <dc:creator>AlejandroMari1</dc:creator>
      <dc:date>2021-08-26T15:32:02Z</dc:date>
    </item>
  </channel>
</rss>

