FeatureLayer.definitionExpression on 4.22 not refreshing

1529
3
Jump to solution
01-14-2022 04:32 PM
RyderRoss
New Contributor II
We're having an issue which I think is similar to https://community.esri.com/t5/arcgis-api-for-javascript-questions/feature-effect-and-labeling-issue/...but it's a little different so I wanted to point it out so that it can get fixed in the next release.
 
We have a FeatureLayer that displays a number of points across the US. We have a default definitionExpression of "acres > 0 and price/acres > 500" which can be seen in the first attachment.
 
We allow a user to select a state and filter down their search. Doing so results in a definitionExpression like "state = 'AL' AND acres > 0 and price/acres > 500" This can be seen in the second attachment. When we change the definitionExpression to be more restrictive, the points that do not match are not removed from the map. We've confirmed this behavior was introduced after 4.20. 
 
 
 
0 Kudos
1 Solution

Accepted Solutions
RyderRoss
New Contributor II

After disabling snapshot mode (https://community.esri.com/t5/arcgis-api-for-javascript-questions/enterprise-point-featurelayers-in-...), this problem went away. I'm wondering if it was due to the layer still trying to load all of the >350k features into memory. 

View solution in original post

0 Kudos
3 Replies
JeffreyWilkerson
Occasional Contributor III

This works for me in both 4.21 and 4.22.  Not sure of your particular feature service URL, but this one using stream data on top of an Esri simple example will correctly apply the definition expression, and then remove it based on the button chosen.  If you are still experiencing issues, maybe adding some actual code would help decipher it.

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <meta name="viewport"
          content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <title>
        Example resetting a FeatureLayer's Definition Expression
    </title>
    <link rel="stylesheet"
          href="https://js.arcgis.com/4.22/esri/themes/dark/main.css" />
    <script src="https://js.arcgis.com/4.22/"></script>

    <style>
        html,
        body,
        #viewDiv {
            height: 100%;
            width: 100%;
            margin: 0;
            padding: 0;
        }

        #infoDiv {
            padding: 6px;
            width: 370px;
            height: 97%;
            position: absolute;
            top: 10px;
            right: 10px;
            --calcite-ui-brand: #71C96E;
            --calcite-ui-brand-hover: #67B564;
        }

        #resultsDiv {
            overflow: auto;
            display: none;
        }
    </style>

    <script>
        require([
            "esri/Map",
            "esri/views/MapView",
            "esri/layers/FeatureLayer",
            "esri/Basemap"
        ], (
            Map,
            MapView,
            FeatureLayer,
            Basemap
        ) =>
            (async () => {
                // dark human geography basemap
                const basemap = new Basemap({
                    portalItem: {
                        id: "4f2e99ba65e34bb8af49733d9778fb8e"
                    }
                });

                // national parks layer
                const layer = new FeatureLayer({
                    url: "https://services9.arcgis.com/RHVPKKiFTONKtxq3/arcgis/rest/services/Live_Stream_Gauges_v1/FeatureServer/0",
                    outFields: ["*"]
                });

                const map = new Map({
                    basemap,
                    layers: [layer]
                });

                const view = new MapView({
                    container: "viewDiv",
                    map: map,
                    center: [-112.4, 33.5],
                    zoom: 9,
                    padding: {
                        right: 380
                    }
                });

                const layerView = await view.whenLayerView(layer);

                view.when(function () {
                    view.ui.add("buttons", "bottom-right");
                    document.getElementById("btnTest1").addEventListener('click', function () { testDef("All"); });
                    document.getElementById("btnTest2").addEventListener('click', function () { testDef("Flowing") });
                });
                
                function testDef(inTest) {
                    var defExp = "";
                    if (inTest == "Flowing") {
                        defExp = "stage_ft > 0.3 and org = 'Maricopa County'";
                    }
                    layer.definitionExpression = defExp;
                    layer.refresh();
                }
            })());
    </script>
</head>

<body>
    <div id="viewDiv"></div>
    <div id="buttons">
        <button id="btnTest1">All</button>
        <button id="btnTest2">Flowing</button>
    </div>
</body>
</html>
0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

It works for me as well. Would you be able to share a reproducible case? If not can you please share the relevant code (layer constructor - renderers, labels etc). Also the code where and how you are setting the definitionExpression? 

@JeffreyWilkerson you do not need to call refresh when definitionExpression is set. 

0 Kudos
RyderRoss
New Contributor II

After disabling snapshot mode (https://community.esri.com/t5/arcgis-api-for-javascript-questions/enterprise-point-featurelayers-in-...), this problem went away. I'm wondering if it was due to the layer still trying to load all of the >350k features into memory. 

0 Kudos