[GIS] ArcGIS JavaScript API Select Features

arcgis-javascript-apifeature-layer

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

Best Answer

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.