Select to view content in your preferred language

Find the values of an attribute of selected features on a FeatureTable

654
4
Jump to solution
03-22-2023 05:35 AM
NielsPerfectPlace
New Contributor III

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

 

I got the part of featureTable.highlightIds.on("change", (event) => {}
 
But the next part I have no clue how to do. 
 
Any suggestion would be amazing. Thank you in advance. 
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

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) });
  }
});

 

View solution in original post

4 Replies
KenBuja
MVP Esteemed Contributor

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"]);
});

 

 

0 Kudos
NielsPerfectPlace
New Contributor III

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;
          });

 

0 Kudos
KenBuja
MVP Esteemed Contributor

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) });
  }
});

 

NielsPerfectPlace
New Contributor III

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. 

 

0 Kudos