This sample demonstrates how to add a FeatureTable widget to your application. The FeatureTable allows users to view and sort data and attributes from a FeatureLayer. In this example, the table is displayed in addition to its associated map.
It is possible to select a feature in the table and have its associated feature automatically reflect as highlighted in the map. In order for this to work correctly, the table's view property must be set. This sample demonstrates how to select a feature in the map and have that selection reflected within the table. The selected features are added into the FeatureTable's collection of highlightIds. If highlightEnabled is true, any features associated with this collection are highlighted.
In addition, this sample also demonstrates how to filter and have the table display only the associated features that are currently displayed within the map. It does this by listening for when the view's extent updates. Whenever a view's extent changes, the FeatureTable's filterGeometry is set to this new extent and only those features that fall within this area display.
How it works
First, we add the FeatureTable with a specified FeatureLayer and field column templates to display. The view must also be set in order for any selected row(s) in the table to display its associated feature(s) as highlighted in the map. In addition, we remove the Zoom to selectionvisibleElement since it is not needed as we are already filtering by the view's extent.
Next, listen to the table's selection-change event. If the row (feature) is checked, or added to the selected features, add it to the features[] array. If unchecked, remove it.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Listen for the table's selection-change eventfeatureTable.on("selection-change", (changes) => {
// If the selection is removed, remove the feature from the array changes.removed.forEach((item) => {
const data = features.find((data) => {
return data.feature === item.feature;
});
if (data) {
features.splice(features.indexOf(data), 1);
}
});
// If the selection is added, push all added selections to array changes.added.forEach((item) => {
const feature = item.feature;
features.push({
feature: feature
});
});
});
Next, listen for when the view's extent updates. Once this happens, pass the updated extent to the table's filterGeometry.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
reactiveUtils.when(
() => view.stationary === true,
() => {
// Get the new extent of view/map whenever map is updated.if (view.extent) {
// Filter out and show only the visible features in the feature table. featureTable.filterGeometry = view.extent;
}
}, {initial: true}
);
Next, listen for the view's immediate-click event. It performs a hitTest on the point location and, if applicable, selects the corresponding feature's row in the table. It does this by adding its associated ObjectId into the FeatureTable's highlightIds collection.
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Listen for the view.click and access the associated graphicview.on("immediate-click", (event) => {
view.hitTest(event).then((response) => {
candidate = response.results.find((result) => {
return (
result.graphic &&
result.graphic.layer &&
result.graphic.layer === featureLayer
);
});
// Add the graphic's objectId into the collection of highlightIds.// Check that the featureTable.highlightIds collection does not include an already highlighted feature.if (candidate) {
const objectId = candidate.graphic.getObjectId();
if (featureTable.highlightIds.includes(objectId)) {
// Remove feature from current selection if feature// is already added to highlightIds collection featureTable.highlightIds.remove(objectId);
} else {
// Add this feature to the featureTable highlightIds collection featureTable.highlightIds.add(objectId);
}
}
});
});
Lastly, the sample also watches for any changes to highlightIds' length. If updated, it then iterates through any activeFilters. If showing a selection of features, it listens for any updates, (i.e. removing/adding), and updates the selection filter appropriately.
Known Limitations
For a comprehensive list of limitations, please refer to the widget's API Reference documentation.