Select to view content in your preferred language

How to overwrite current where clause in subsequent Filter actions.

372
0
01-17-2024 12:15 PM
FranklinAlexander
Frequent Contributor

I have a custom Filter widget that was created by another developer and I noticed that on subsequent Filtering tasks, the results didn't make sense. After looking at the code, I think I found the issue, but I don't know how to fix it. From what I can tell the updateQueryParams function isn't working the way I would expect. Instead of overwriting the where clause when called, it seems to be appending the new where clause to the previously loaded where clause. Does any one know if this is the way this function should work? I need to be able to clear the previous clause completely before updating the params, but I am not familiar enough with the 4X api yet. Here is the code:

const executeAttributeQuery = async (props) => {
  let wc = "";
  if (filterItems.length > 0) {
    setMessage("")
    for (var i = 0; i < filterItems.length; i++) {
      if (i == 0) wc += filterItems[i].filter;
      else wc += " AND " + filterItems[i].filter;
    }
  } else {
    wc = "1=1";
    setMessage('No items to filter')
  }
  console.log("Before On 345 ");
  console.log("where ", wc)
  const ds0 = getFeatureLayerDataSource(props.useDataSources[0].dataSourceId);
  const ds1 = getFeatureLayerDataSource(props.useDataSources[1].dataSourceId);
  const ds2 = getFeatureLayerDataSource(props.useDataSources[2].dataSourceId);
  ds0.updateQueryParams(null, props.id);  //This seems to be the only
  ds1.updateQueryParams(null, props.id);  //way to clear the previous
  ds2.updateQueryParams(null, props.id);  //where clause, but it's not 
                                          //optimal

  const queryParams: FeatureLayerQueryParams = {
    outFields: ["OBJECTID"],
    where: wc,
    returnGeometry: false,
  };

  const records0 = await ds0.query(queryParams);
  let wc1 = getWcSpatial(records0.records) //returns array of object IDs
  console.log("query params ", queryParams) //where clause is correct here.

  if (wc1.length > 0) { 
    const qp1: SqlQueryParams = { where: wc1 }
    console.log("attrubute query params ", qp1)
    ds0.updateQueryParams(qp1, props.id); //does not update, appends to 
                                          //previously loaded where clause.
    ds1.updateQueryParams(qp1, props.id);
    ds2.updateQueryParams(qp1, props.id);
    publishDataFilterAction(props.id, props.useDataSources[0].dataSourceId);   
  } else {
    setMessage("No matches.")
    const qp1: SqlQueryParams = { where: "1=1" };
    console.log("attrubute query params no match ", qp1)
    ds0.updateQueryParams(qp1, props.id);
    ds1.updateQueryParams(qp1, props.id);
    ds2.updateQueryParams(qp1, props.id);
    publishDataFilterAction(props.id, props.useDataSources[0].dataSourceId);
  }
}

 

The above code is the source code from the widget.tsx file, but the app has already been exported to the server so any changes I make must be in the compiled code which is not ideal. 

where clause from 1st query: Horseback riding = 'y'

where clause from subsequent query: Horseback riding = 'y' and Bicycling = 'y'

The reason I don't want to run the updateQueryParams function to clear out the where clause is because as soon as it runs, all of the features display on the map briefly until the Filter executes.

 

0 Kudos
0 Replies