Is it possible to filter a shapefile by its attributes once it's loaded onto the map?
Solved! Go to Solution.
There are no methods in the API to do this but you could write your own.
The first thing I would do is to make sure that your graphics layer gets an Id with it is created. Check out the options parameter when creating the GraphicsLayer. Then when you want to apply a definition query:
Something like this:
var gl = map.getLayer('shapefileGL'); // < this needs to be your graphics layer id var value = dom.byId('defValue').value; // this is the value to filter on var field = "STRAHLER"; // this is the field (attribute) to filter on setSimpleDefinitionQuery(gl, field, value); function setSimpleDefinitionQuery(gl, field, value){ dojo.forEach(gl.graphics,function(gr){ gr.visible = (gr.attributes[field] == value); }); gl.redraw(); }
How are you adding the shapefile?
This question is a bit vague - how are you adding the shapefile to a javascript map?
If you are talking about a FeatureLayer then you can use setDefinitionExpression:
Set's the definition expression for the FeatureLayer. Only the features that match the definition expression are displayed. A definition expression limits the features available for display and queries by applying constraints to the layer's attribute fields.
If you are talking about an ArcgisDynamicMapServiceLayer then you can use setlayerdefinitions:
Sets the layer definitions used to filter the features of individual layers in the map service.
Apologies for the lack of detail, shapefile is uploaded using esri/request:
var uploadRequest = esriRequest({ url: this._uploadUrl, form: dojo.byId("formUploadShapeFile") });
Then it's added to the map as a GraphicsLayer.
There are no methods in the API to do this but you could write your own.
The first thing I would do is to make sure that your graphics layer gets an Id with it is created. Check out the options parameter when creating the GraphicsLayer. Then when you want to apply a definition query:
Something like this:
var gl = map.getLayer('shapefileGL'); // < this needs to be your graphics layer id var value = dom.byId('defValue').value; // this is the value to filter on var field = "STRAHLER"; // this is the field (attribute) to filter on setSimpleDefinitionQuery(gl, field, value); function setSimpleDefinitionQuery(gl, field, value){ dojo.forEach(gl.graphics,function(gr){ gr.visible = (gr.attributes[field] == value); }); gl.redraw(); }