Trying to send query data to widget table

250
0
4 weeks ago
Labels (2)
Esprit3
New Contributor II

I made a custom widget that executes a query and then sends the response to this method in widget.tsx:

  const populateDataSource = async (features: any[]) => {
    const dataSourceManager = DataSourceManager.getInstance();
    const outputDsId = `${props.id}-output`;
  
    // Extract fields dynamically from features
    const feature = features[0];
    const attributes = feature.attributes;
    const fields = Object.keys(attributes).reduce((acc, fieldName) => {
      acc[fieldName] = {
        name: fieldName,
        alias: fieldName,
        type: typeof attributes[fieldName] === 'number' ? 'esriFieldTypeDouble' : 'esriFieldTypeString'
      };
      return acc;
    }, {});
  
    // Define data source JSON
    const dsJson: IMDataSourceJson = Immutable({
      id: outputDsId,
      type: DataSourceTypes.FeatureLayer,
      label: "Query Result Layer",
      originDataSources: [],
      isDataInDataSourceInstance: true,
      schema: {
        fields: fields
      }
    });
  
    // Create or get the data source
    let ods;
    try {
      ods = await dataSourceManager.createDataSource(dsJson);
    } catch (error) {
      console.error("Error creating data source:", error);
      return;
    }
  
    // Populate the data source with features
    const dataRecords = features.map(f => ods.buildRecord(f));
    ods.setSourceRecords(dataRecords);
    ods.setStatus(DataSourceStatus.Loaded);
    ods.setCountStatus(DataSourceStatus.Loaded);
  
    // Prepare the DataRecordSet array
    const dataRecordSet: DataRecordSet = {
      name: `${props.id}-output-record-set`,
      dataSource: ods,
      records: dataRecords
    };
  
    // Determine the data level, typically it is DataLevel.Records for multiple records
    const dataLevel = DataLevel.Records;
  
    // Use the widget ID of the table widget where the action will be applied
    const widgetId = "widget_5"; // Replace this with your actual table widget ID
  
    // Dispatch the action to open the results in a new tab in the table widget
    const instance = DataActionManager.getInstance();
    const action = instance.getActions().find(a => a.id === "widget_5-addToTable");
    if (action) {
      console.log("Executing data action");
      await action.onExecute([dataRecordSet], dataLevel, widgetId);
    } else {
      console.error("Data action not found");
    }
    console.log(DataSourceManager.getInstance());
    console.log(DataActionManager.getInstance());
  };
  

 I logged DataActionManager.getInstance() and got the table widget's ID. 

When this method is executed, a new tab with the proper name is created in the table widget, but it is empty.

When logging DataSourceManager I see this widget's data source:

Screenshot 2024-05-25 144352.png

Along with the errors when the new tab is created.

How can I make this work? I would appreciate any kind of help. Please let me know if you need more info/context.

0 Replies