I need help. I'm trying to retrieve the value of an attribute (Lets all it "name") of selected features of a Featuretable. I tried many things, but all seem to end up not working, so I wondered if someone here has a suggestion.
So just to be clear. I want:
1. Make a selection in the table.
2. Retrieve an attribute (name).
3. Use Name to make a selection in a FeatureLayer
Solved! Go to Solution.
It's kind of annoying that the new event doesn't return the feature. Instead, it will return the id of the feature, so you'll have to use the result of the event in a query. This is what I came up with
featureTable.highlightIds.on("change", (event) => {
if (event.added[0] != undefined) {
const query = new Query();
query.where = `${featureLayer.objectIdField} = ${event.added}`;
query.outFields = ["Name"];
featureLayer.queryFeatures(query).then((results) => { console.log(results.features[0].attributes.Name) });
}
});
You can get the attribute this way from the first item in the event.added array
let name;
featureTable.highlightIds.on("change", (event) => {
name = event.added[0].feature.attributes["name"]);
});
I have this now. If I change it to highlightIds it stops working because it can't find the "name" attribute. I know that selection-change is deprecated. But this works so far.
featureTable.on('selection-change', (event) => {
if (event.added.length > 0) {
filterArray.push(event.added[0].feature.attributes['name']);
}
if (event.removed.length === 1) {
const i = filterArray.indexOf(event.removed[0].feature.attributes['name']);
filterArray.splice(i, 1);
} else if (event.removed.length > 0) {
filterArray = [];
}
if (filterArray.length > 0) {
featureFilter.where = `name IN (${filterArray.map(value => `'${value}'`).join(',')})`;
} else {
featureFilter.where = expression;
}
featureLayer.definitionExpression = featureFilter.where;
});
It's kind of annoying that the new event doesn't return the feature. Instead, it will return the id of the feature, so you'll have to use the result of the event in a query. This is what I came up with
featureTable.highlightIds.on("change", (event) => {
if (event.added[0] != undefined) {
const query = new Query();
query.where = `${featureLayer.objectIdField} = ${event.added}`;
query.outFields = ["Name"];
featureLayer.queryFeatures(query).then((results) => { console.log(results.features[0].attributes.Name) });
}
});
Thank you for the reply. I think I will keep using the deprecated solution for now because the new 'appropriate' way of doing this is too cumbersome compared to the old way....but I'll keep this bookmarked if they will remove the old function.