4

I'm having some trouble using the selectFeatures method with a feature layer. The code below builds a query using input text. If anyone has any pointers on what I could be doing wrong here, it'd be a huge help! I added a function to get some feedback after the selection is complete. It appears to be working fine when I test the search, but the results are not being displayed correctly.

//Query
var query = new Query();
searchButton.onclick=function(){
    var value = dom.byId('searchText').value;
    query.returnGeometry = true 
    query.where = '"Type"' + " LIKE '%" + value + "%'"
    console.log('Query Registered');
    roundabouts.selectFeatures(query,FeatureLayer.SELECTION_NEW, function(selection){
            console.log('Selection Query: ' + query.where);
            console.log(selection.length + ' Results Returned');
    });
};
3

If I were writing this function I would structure it this way.

function search(){
     var query = new Query();
     var value = dom.byId('searchText').value;
     query.returnGeometry = true 
     query.where = '"Type"' + " LIKE '%" + value + "%'"
     console.log('Query Registered');
     roundabouts.selectFeatures(query,FeatureLayer.SELECTION_NEW, 
     function(selection){
          console.log('Selection Query: ' + query.where);
          console.log(selection.length + ' Results Returned');
     });
}

dom.byId('*** SEARCH BUTTON ***').on("click", search);

Also make sure you have the necessary modules imported and variables defined.

var roundabouts = new FeatureLayer("***YOUR FEATURE LAYER***,{
    mode: FeatureLayer.MODE_ONDEMAND,
    outFields: ["*"]
});

require(["esri/layers/FeatureLayer", "esri/tasks/query", "dojo/dom", "dojo/on"], function(FeatureLayer, Query, dom, on) { /* Query code goes here */ }); 

One final issue, is "selection.length" a method of the selection object that is returned? This may be why you are not seeing the desired results.

  • Thanks Jeremy, I double checked that I have the necessary modules. The selection.length provides the number of records that have been selected. I used it to get some feedback on whether or not the selection was working. According to my console messages, the selection is returning results. However, a selection symbology is not applied even though it's defined in my code, and the features that meet the query's criteria end up displaced on the map. – Wolfmapper Mar 11 '14 at 17:18
  • I found some additional information in the API that could be related: The outSpatialReference set by the query object is ignored and the map's spatial reference is used. developers.arcgis.com/javascript/jsapi/… – Wolfmapper Mar 11 '14 at 17:22
  • @Wolfmapper So is your issue resolved? – Jeremy Hamm Mar 11 '14 at 17:31
  • Unfortunately, no. I'm still not sure exactly what's causing the issue or how to resolve it. – Wolfmapper Mar 11 '14 at 17:53
  • Do you have any additional information you could post? How do you wish to display the results? What do you mean by they are not displaying correctly? Try the following code in the select features function and post the results. console.log(selection) – Jeremy Hamm Mar 17 '14 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.