how to remove graphic from view using attribute in 4.6

951
2
Jump to solution
01-16-2018 09:00 AM
LoriEmerson_McCormack
Occasional Contributor

How can I remove a graphic using an attribute?  I am able to add the graphic.

view.graphics.graphics.length is invalid.  I am having a hard time finding what I need in the view object because of all the _proto_ levels.

//*************************************************//

//** show Graphic for selected feature **//

//*************************************************//

function showGraphic(myID) {

   if (arrayIdFeatures.length) {

      for (i = 0; i < arrayIdFeatures.length; i++) {

         if (arrayIdFeatures.attributes['Legend ID'] == myID) {

            var pointAtt = {

               "LegendID": arrayIdFeatures.attributes['Legend ID']

                     };

            var pointGeography = arrayIdFeatures.geometry;

            var pointGraphic = new Graphic({

                        geometry: arrayIdFeatures.geometry,

                        symbol: pointSymbol,

                        attributes: pointAtt

                     });

                     view.graphics.add(pointGraphic);

         } // end if stmt

      } // end for stmt

   } //end if stmt

} // end function showGraphic()

//*************************************************//

//** remove Graphic for selected feature **//

//*************************************************//

function removeGraphic(myID) {

   if (view.graphics.graphics.length) {

      for (i = 0; i < view.graphics.graphics.length; i++) {

         if (view.graphics.graphics.attributes.LegendID == myID) {

                  view.graphics.remove(view.graphics.graphics);

         } // end if stmt

      } // end for stmt

   } //end if stmt

} // end function removeGraphic()

 

 

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Lori,

   They way you are working with collections is incorrect. Collections are a bit different than arrays.

//*************************************************//
//** remove Graphic for selected feature **//
//*************************************************//
function removeGraphic(myID) {
   if (view.graphics.graphics.length) {
      for (i = 0; i < view.graphics.length; i++) {
         if (view.graphics.getItemAt(i).attributes.LegendID == myID) {
             view.graphics.remove(view.graphics.getItemAt(i));
         } // end if stmt
      } // end for stmt
   } //end if stmt
} // end function removeGraphic()

View solution in original post

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

Lori,

   They way you are working with collections is incorrect. Collections are a bit different than arrays.

//*************************************************//
//** remove Graphic for selected feature **//
//*************************************************//
function removeGraphic(myID) {
   if (view.graphics.graphics.length) {
      for (i = 0; i < view.graphics.length; i++) {
         if (view.graphics.getItemAt(i).attributes.LegendID == myID) {
             view.graphics.remove(view.graphics.getItemAt(i));
         } // end if stmt
      } // end for stmt
   } //end if stmt
} // end function removeGraphic()
0 Kudos
LoriEmerson_McCormack
Occasional Contributor

Robert,

Thanks for the insight about Collections.  I will learn more about it.

Using view.graphics.length worked.  Also, using view.graphics.getItemAt(I).attributes.<custom attribute> worked.  Brilliant.  Thanks!!

0 Kudos