I am using the QueryTask and would like to use the Alias in the results rather than the field names. The documentation sounds like I can do this
https://developers.arcgis.com/javascript/3/jshelp/intro_querytask.html
You must use the actual field names rather than the alias names, but you can use the alias names later when you display the results.
but I could not find an example for doing this programmatically.
Can someone provide an example of using the Alias Names rather than the Field names in the results from the Query Task?
Thanks,
Mele
Solved! Go to Solution.
Mele,
Here is a sample of getting the aliases for the QueryTask results:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Query State Info without Map</title>
<script src="https://js.arcgis.com/3.18/"></script>
<script>
var count = 0, lyrFields;
require([
"dojo/dom", "dojo/on", "dojo/dom-attr", "esri/request", "dojo/_base/array",
"esri/tasks/query", "esri/tasks/QueryTask", "dojo/domReady!"
], function (dom, on, domAttr, esriRequest, array, Query, QueryTask) {
var queryTask = new QueryTask("http://gis.calhouncounty.org/arcgis2/rest/services/ParcelViewer/MapServer/67");
var layersRequest = esriRequest({
url: queryTask.url,
content: { f: "json" },
handleAs: "json",
callbackParamName: "callback"
});
layersRequest.then(
function(response) {
lyrFields = response.fields;
}, function(error) {
console.log("Error: ", error.message);
});
var query = new Query();
query.returnGeometry = false;
query.outFields = ["PPIN", "NAME", "ADDRESS_3","SUBDIVISION","PARCEL_NUMBER"];
on(dom.byId("execute"), "click", execute);
function execute () {
query.where = "1=1";
query.start = count;
query.num = 20;
query.orderByFields = ["PPIN DESC"];
queryTask.execute(query, showResults);
count += 20;
domAttr.set(dom.byId("execute"),"value", "Get Next 20");
dom.byId("tCount").innerHTML = "Records Displayed: " + count.toString();
}
function showResults (results) {
var resultItems = [];
var resultCount = results.features.length;
for (var i = 0; i < resultCount; i++) {
var featureAttributes = results.features[i].attributes;
for (var attr in featureAttributes) {
resultItems.push("<b>" + getFldAlias(attr) + ":</b> " + featureAttributes[attr] + "<br>");
}
resultItems.push("<br>");
}
dom.byId("info").innerHTML += resultItems.join("");
}
function getFldAlias(fieldName){
var retVal = "";
array.some(lyrFields, function(item){
if(item.name === fieldName){
retVal = item.alias;
return true;
}
});
return retVal;
}
});
</script>
</head>
<body>
<input id="execute" type="button" value="Get First 20"> <span id="tCount">Records Displayed: 0</span>
<br />
<br />
<div id="info" style="padding:5px; margin:5px; background-color:#eee;">
</div>
</body>
</html>
Mele,
Here is a sample of getting the aliases for the QueryTask results:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>Query State Info without Map</title>
<script src="https://js.arcgis.com/3.18/"></script>
<script>
var count = 0, lyrFields;
require([
"dojo/dom", "dojo/on", "dojo/dom-attr", "esri/request", "dojo/_base/array",
"esri/tasks/query", "esri/tasks/QueryTask", "dojo/domReady!"
], function (dom, on, domAttr, esriRequest, array, Query, QueryTask) {
var queryTask = new QueryTask("http://gis.calhouncounty.org/arcgis2/rest/services/ParcelViewer/MapServer/67");
var layersRequest = esriRequest({
url: queryTask.url,
content: { f: "json" },
handleAs: "json",
callbackParamName: "callback"
});
layersRequest.then(
function(response) {
lyrFields = response.fields;
}, function(error) {
console.log("Error: ", error.message);
});
var query = new Query();
query.returnGeometry = false;
query.outFields = ["PPIN", "NAME", "ADDRESS_3","SUBDIVISION","PARCEL_NUMBER"];
on(dom.byId("execute"), "click", execute);
function execute () {
query.where = "1=1";
query.start = count;
query.num = 20;
query.orderByFields = ["PPIN DESC"];
queryTask.execute(query, showResults);
count += 20;
domAttr.set(dom.byId("execute"),"value", "Get Next 20");
dom.byId("tCount").innerHTML = "Records Displayed: " + count.toString();
}
function showResults (results) {
var resultItems = [];
var resultCount = results.features.length;
for (var i = 0; i < resultCount; i++) {
var featureAttributes = results.features[i].attributes;
for (var attr in featureAttributes) {
resultItems.push("<b>" + getFldAlias(attr) + ":</b> " + featureAttributes[attr] + "<br>");
}
resultItems.push("<br>");
}
dom.byId("info").innerHTML += resultItems.join("");
}
function getFldAlias(fieldName){
var retVal = "";
array.some(lyrFields, function(item){
if(item.name === fieldName){
retVal = item.alias;
return true;
}
});
return retVal;
}
});
</script>
</head>
<body>
<input id="execute" type="button" value="Get First 20"> <span id="tCount">Records Displayed: 0</span>
<br />
<br />
<div id="info" style="padding:5px; margin:5px; background-color:#eee;">
</div>
</body>
</html>
Robert, Thank you. This is perfect, I could not find any samples like this so I appreciate you posting this!