Populate a combo box with unique values using a query layer

In an earlier post, we showed how to populate a ComboBox with a set of unique values from a map service by querying the service and manipulating the data in the browser to generate the unique list. At ArcGIS 10, we can use a Query Layer to accomplish this task in a more straightforward manner. Let’s look at how we can use query layers to fill a combo box with a series of dams in the Pacific Northwest. 

Combo box with unique list of dams 

Query Layer

First we’ll create a new query layer using ArcMap. A query layer is a layer or table that is created using a SQL Query. We can use the SQL SELECT DISTINCT statement to generate a list of unique values from the specified field or fields. In this example we create a new query layer that contains a list of unique dam names.

create a query layer

After adding the query layer to the map, we’ll publish the map. The published map service “FishCount” contains a layer with counts of several fish species at each dam, plus a table with unique dam names. The snippet below queries the table of unique dam names to retrieve all the records.

var damLayer = new esri.layers.FeatureLayer("http://servicesbeta.esri.com/ArcGIS/rest/services/Portland/FishCount/MapServer/1",{
mode:esri.layers.FeatureLayer.MODE_SELECTION
});
var query = new esri.tasks.Query();
query.where = "1=1";
damLayer.queryFeatures(query,function(featureSet){

});

Now we have a featureSet containing the unique values from the dam table, which we can use to populate the ComboBox.

var values = dojo.map(featureSet.features,function(feature){
return {name:feature.attributes.DAM};
});
var dataItems = {
identifier: 'name',
label: 'name',
items: values
};
var store = new dojo.data.ItemFileReadStore({data:dataItems});
dijit.byId("mySelect").store = store;

Click here to view a live sample application that populates a ComboBox using a query layer.

Contributed by Kelly Hutchins of the ArcGIS API for JavaScript development team

This entry was posted in Services and tagged . Bookmark the permalink.

Leave a Reply

2 Comments

  1. bpatterson says:

    I’d like to do something similar in actionscript with the Flex API except instead of using a query layer, my data source is a table in the map service. Has anyone seen any samples to do this? Barb