Solved! Go to Solution.
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.
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>
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.
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.