常见问题

常见问题解答:是否可以修改查询和搜索结果以显示编码值域描述?

Last Published: September 13, 2023

答案

Coded value domains are a set of valid values for an attribute, which often contains 'String' type information and can be retrieved using JavaScript. However, map service query operations, such as the Search widget and QueryTask method return raw data from the database, in the form of a numeric value that does not contain any valuable information instead of the coded values.

It is possible to modify query and search results to display the coded value domain information by creating a custom function to retrieve the information from the coded value and return the containing value.

The following code snippet demonstrates a sample of the custom function to retrieve information from a coded value in a QueryTask request:

function getCodedValue(layer, fieldName, fieldValue) {
var returnValue = fieldValue;
dojo.forEach(layer.fields,function(fld){
if (fld.name == fieldName) {
cDomain = fld.domain;
if (cDomain)
dojo.forEach(cDomain.codedValues,function(cVal){
if (cVal.code == fieldValue)
returnValue = cVal.name;
});
}
});
return returnValue;
}

The same logic can also be used for the Search widget. The following code demonstrates a full code sample:

require([
        "esri/map", "esri/dijit/Search", "esri/layers/FeatureLayer", "esri/InfoTemplate", "esri/graphic",
        "esri/symbols/SimpleMarkerSymbol", "dojo/_base/Color", "dojo/domReady!"
    ], function(Map, Search, FeatureLayer, InfoTemplate, Graphic, SimpleMarkerSymbol, Color) {
        var map = new Map("map", {
            basemap: "streets",
            center: [-97, 38], // lon, lat
            zoom: 5
        });

        var search = new Search({
            enableLabel: false,
            enableInfoWindow: true,
            showInfoWindowOnSelect: false,
            map: map
        }, "search");

        var sources = [];

        //Push the sources used to search. The ArcGIS Online World geocoder is included by default.  
        var featureLayer = new FeatureLayer("https://sampleserver6.arcgisonline.com/arcgis/rest/services/SF311/FeatureServer/0");
        sources.push({
            featureLayer: featureLayer,
            searchFields: ["objectid"],
            displayField: "objectid",
            exactMatch: false,
            outFields: ["*"],
            name: "Incidents",
            placeholder: "2739248",
            maxResults: 6,
            maxSuggestions: 6,
            enableSuggestions: true,
            minCharacters: 0
        });

        //Set the sources above to the search widget
        search.set("sources", sources);

        search.startup();

        search.on("select-result", showLocation);

        function showLocation(e) {
            map.graphics.clear();
            var point = e.result.feature.geometry;
            var symbol = new SimpleMarkerSymbol().setStyle(
                SimpleMarkerSymbol.STYLE_SQUARE).setColor(
                new Color([255, 0, 0, 0.5])
            );
            var graphic = new Graphic(point, symbol);
            map.graphics.add(graphic);

            var resultItems = "";
            var featureAttributes = e.result.feature.attributes;
            for (var field in featureAttributes) {
                if (field == "status") {
                    var value = getCodedValue(featureLayer, field, featureAttributes[field]);
                    resultItems += "<b>" + field + ": </b>" + value + "</br>";
                } else if (field == "objectid") {
                    resultItems += "<b>" + field + ": </b>" + featureAttributes[field] + "</br>";
                }
            }
            map.infoWindow.setTitle("Search Result");
            map.infoWindow.setContent(resultItems);
            map.infoWindow.show(e.result.feature.geometry);
        }

        //custom method that checks if the field has a coded value and if yes, it will return the description
        function getCodedValue(layer, fieldName, fieldValue) {
            var returnValue = fieldValue;
            dojo.forEach(layer.fields, function(fld) {
                if (fld.name == fieldName) {
                    cDomain = fld.domain;
                    if (cDomain)
                        dojo.forEach(cDomain.codedValues, function(cVal) {
                            if (cVal.code == fieldValue)
                                returnValue = cVal.name;
                        });
                }
            });
            return returnValue;
        }
    });

文章 ID:000016437

从 ArcGIS 专家处获得帮助

联系技术支持部门

下载 Esri 支持应用程序

转至下载选项

相关信息

发现关于本主题的更多内容