I have an infoTemplate with a function for generating the content. I'd like to be able to use this same function again for my click event on my grid. The problem is that I have some field aliases. For my map click, it finds the alias fields just fine. When I do the grid click, I'm executing a queryTask and the graphic result doesn't know about the field aliases. I've read through another thread this is 'expected behavior', that the identifyTask uses the aliases just fine.
I have multiple layers I want to be able to click on on the map, but only one layer that's populated in a grid. At the moment, that layer is loaded as a ArcGISDynamicMapServiceLayer. Would it help to load this as a featureLayer instead and do a featureLayer.selectFeatures? Do featureLayers honor aliases any better than a queryTask does?
I've created a series of arrays to use for each layer, since I want to control which fields will appear in the infoTemplate:
var pubSchoolAtt = ['OBJECTID','Facility','Address', 'City','Phone', 'County', 'Enrollment', 'Teachers', 'BeginningGrade', 'EndingGrade'];
var schoolDistAtt = ['OBJECTID','District Name', 'District Code'];
var legisDistAtt = ['OBJECTID','District'];
My infoTemplate is defined as
generateInfoTemplate = new InfoTemplate();
generateInfoTemplate.setContent(generateWindowContent);
function generateWindowContent(graphic) {
var attString = "";
var layerName = graphic.attributes.layerName;//case sensitive field names
if (!layerName){
layerName = "School";
}
var fieldNamesArr = [];
for (fieldName in graphic.attributes) {
if (graphic.attributes.hasOwnProperty(fieldName) && fieldName !=='Shape' && fieldName !== 'Shape.area' && fieldName !== 'Shape.len') {
fieldNamesArr.push(fieldName);
}
}
switch(layerName){
case "House":
attString = createAttRows(graphic, legisDistAtt);
break;
case "Senate":
attString = createAttRows(graphic, legisDistAtt);
break;
case "School":
attString = createAttRows(graphic, pubSchoolAtt);
break;
case "Public School":
attString = createAttRows(graphic, pubSchoolAtt);
break;
case "School District":
attString = createAttRows(graphic, schoolDistAtt);
break;
default:
attString = createAttRows(graphic, fieldNamesArr, layerName);
}
initTable = "<b class = 'infoHeader'>"+ layerName +" Information</b>";
initTable = initTable + "<table id='infoWindowTable' data-dojo-type='dgrid/OnDemandGrid',' data-dojo-props='class:'infoTable'' ><tr><th></th><th></th></tr>";
initTable = initTable + attString;
initTable = initTable + "</table>";
return initTable;
}
Here's my grid click event
function highlightGridSelection(event, dGrid) {
var row = dGrid.row(event);
var query = new Query();
var schName = [row.data.facility];
var cityName = [row.data.city];
query.where = "Facility = '" + schName + "' and " + "City = '" + cityName + "'";
query.returnGeometry = true;
query.outFields = ["*"];
query.outSpatialReference = spatialReference;
var queryTask = new QueryTask(educationLayer.url+"/0");
queryTask.execute(query, highlightResults);
}
function highlightResults(results) {
if (results) {
var feature = results.features[0];//already esri.Graphic
// var featType = feature.geometry.type;
var graphic = new Graphic(feature.geometry, highlightMarkerSymbol);
feature.setInfoTemplate(generateInfoTemplate);
map.infoWindow.setFeatures(results.features);
map.infoWindow.show(feature.geometry);
map.graphics.add(graphic);
} else {
console.log("No records by that ID");
}
}
Does anybody have any clever ideas on how I can my one function work for both type of events?
I had a similar issue with the Query returning the field names and the Identify returning field aliases. I was trying to get an attribute from a feature that could be returned from either a Query or an Identify. I resolved this by first attempting to use the field alias on a returned feature. If that returned a null, then I used the field name.