AnsweredAssumed Answered

Reverse engineering the feature layer name after features are selected?

Question asked by MollyKFoley on Sep 7, 2018
Latest reply on Sep 7, 2018 by rscheitlin

I have a draw function that allows someone to draw a polygon around a set of features, select them, and then populate a table with those selected features' attributes. I use the same draw function for creating selection queries for multiple layers though (using conditionals where the variable "selectionType" is chosen from a dialog box by the user before the draw function is activated) and when the selection is complete ("selection-complete" event), all those layers will then call the same function to populate a table. 

 

//Create selection by drawing on map
            function selectFeatures(selectionType, qHabSelection) {
                var selectTool = new Draw(map);
                selectTool.activate(Draw.POLYGON);
                attrTableSpinner.show();
                //Deactivate popups when drawing
                map.setInfoWindowOnClick(false);
                var selectQuery = new Query();

                on(selectTool, "draw-complete", function (Geom) {
                    selectTool.deactivate();
                    selectQuery.geometry = Geom.geometry;
                    //Select all patches within the bounding polygon
                    if (selectionType == "selectTractMgmtData") {
                        hbMgmtTractFL.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW);
                    }
                    else if (selectionType == "selectLineMgmtData") {
                        hbMgmtLineFL.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW);
                    }
                    else if (selectionType == "selectPatchData" && qHabSelection == "selectAll") {
                        patchesFL.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW);
                    }
                    //Select only patches that are designated as quail habitat in the bounding polygon
                    else if (selectionType == "selectPatchData" && qHabSelection == "selectQH") {
                        selectQuery.where = "OfficialQH = 1";
                        patchesFL.selectFeatures(selectQuery, FeatureLayer.SELECTION_NEW);
                    }
                    //Reactivate popups when done drawing
                    map.setInfoWindowOnClick(true);
                });
            };

//When user is finished selecting features, call the popuplateTable function
hbMgmtTractFL.on("selection-complete", populateTable);
hbMgmtLineFL.on("selection-complete", populateTable);
patchesFL.on("selection-complete", populateTable);

 

Because each of those layers has different attributes, I need to actually be able to tell what layer called the populateTable function so I can structure the table appropriately. Right now, in my "populateTable" function, I'm doing this by getting the title of the InfoTemplate and differentiating between the layers that way. I'm wondering though if there's a more straight forward way to do this? Yes, I know I could create a separate function for each one that creates a unique table but there would be redundant pieces of code as there are some repeated elements for each table despite there being different attributes among the layers that call the function.

 

//This function populates the table with feature attribute information after features are selected by user
function populateTable(event) {
    var countFeatures = event.features.length;
    tableData = [];
    arrayUtil.forEach(event.features, function (feature) {
        var rowData = [];
        var title = feature.getTitle();
        if (title == "Habitat Mgmt Tract"){
           //structure table this way
        }
        else if (title == "Habitat Mgmt Line") {
           //structure table another way
        }
        else if (titel == "Habitat Patches") {
           //structure table another way
        }
    }
}

Outcomes