Select to view content in your preferred language

Display text unless null, if null display text of next attribute colum

924
4
Jump to solution
10-10-2014 02:33 PM
RickeyFight
MVP Regular Contributor

I have trail data.

I want to show map tips if the trail has a name.

Unfortunately, I cannot change the data. I want the Name to be displayed. If Name is Null then displayed the Group_ field if not the group then Route_2 and if not Route_2 then Route_1

My code right now looks like this:

dojo.connect(Layer, "onMouseMove", function (evt) { 

                    if (evt.graphic) { 

                        if (evt.graphic.attributes) { 

                            ShowMapTip(evt, "<b>Name</b>: " + evt.graphic.attributes.NAME ); 

                        } 

                    } 

                }); 

                dojo.connect(Layer, "onMouseOut", CloseMapTip);

routeNull.png

Any suggestions would be greatly appreciated!

0 Kudos
1 Solution

Accepted Solutions
SteveCole
Frequent Contributor

Maybe there's an issue with your fields being blank vs NULL; hard to say from my end on the outside. Let's try evaluating your field values another way, Let's look at the length of the field's value.:

if (evt.graphic.attributes) {

                            var theString;

                            if(evt.graphic.attributes.NAME.length > 0) {

                                 theString = "<b>Name1</b>: " + evt.graphic.attributes.NAME;

                            } else if (evt.graphic.attributes.GROUP_.length > 0) {

                                 theString = "<b>Name2</b>: " + evt.graphic.attributes.GROUP_;

                           } else if (evt.graphic.attributes.ROUTE_2.length > 0) {

                                 theString = "<b>Name3</b>: " + evt.graphic.attributes.ROUTE_2;

                           }  else {

                                theString = "<b>Name4</b>: " + evt.graphic.attributes.ROUTE_1;

                          }

                            ShowMapTip(evt, theString);

                        }

View solution in original post

4 Replies
SteveCole
Frequent Contributor

How about something like....

                        if (evt.graphic.attributes) {

                            var theString;

                            if(!evt.graphic.attributes.NAME) {

                                 theString = "<b>Name</b>: " + evt.graphic.attributes.NAME;

                            } else if (!evt.graphic.attributes.GROUP_) {

                                 theString = "<b>Name</b>: " + evt.graphic.attributes.GROUP_;

                           } else if (!evt.graphic.attributes.ROUTE_2) {

                                 theString = "<b>Name</b>: " + evt.graphic.attributes.ROUTE_2;

                           }  else {

                                theString = "<b>Name</b>: " + evt.graphic.attributes.ROUTE_1;

                          }

                            ShowMapTip(evt, theString);

                        }

0 Kudos
RickeyFight
MVP Regular Contributor

Thank you for the quick response!

I changed the code slightly (bold numbers) to see why it was not working correctly. The trails with NAME's displayed with a label of Name4. Every other trail had a label of Name4 but nothing after that. I am not sure why it is pull the label of the last option.

Name4.png

if (evt.graphic.attributes) {

                            var theString;

                            if(!evt.graphic.attributes.NAME) {

                                 theString = "<b>Name1</b>: " + evt.graphic.attributes.NAME;

                            } else if (!evt.graphic.attributes.GROUP_) {

                                 theString = "<b>Name2</b>: " + evt.graphic.attributes.GROUP_;

                           } else if (!evt.graphic.attributes.ROUTE_2) {

                                 theString = "<b>Name3</b>: " + evt.graphic.attributes.ROUTE_2;

                           }  else {

                                theString = "<b>Name4</b>: " + evt.graphic.attributes.ROUTE_1;

                          }

                            ShowMapTip(evt, theString);

                        }

0 Kudos
SteveCole
Frequent Contributor

Maybe there's an issue with your fields being blank vs NULL; hard to say from my end on the outside. Let's try evaluating your field values another way, Let's look at the length of the field's value.:

if (evt.graphic.attributes) {

                            var theString;

                            if(evt.graphic.attributes.NAME.length > 0) {

                                 theString = "<b>Name1</b>: " + evt.graphic.attributes.NAME;

                            } else if (evt.graphic.attributes.GROUP_.length > 0) {

                                 theString = "<b>Name2</b>: " + evt.graphic.attributes.GROUP_;

                           } else if (evt.graphic.attributes.ROUTE_2.length > 0) {

                                 theString = "<b>Name3</b>: " + evt.graphic.attributes.ROUTE_2;

                           }  else {

                                theString = "<b>Name4</b>: " + evt.graphic.attributes.ROUTE_1;

                          }

                            ShowMapTip(evt, theString);

                        }

RickeyFight
MVP Regular Contributor

Thank you!

The only thing i changed was the 0 to 1 and it worked! (in bold below)

if (evt.graphic.attributes) {

                            var theString;

                            if(evt.graphic.attributes.NAME.length > 1) {

                                 theString = "<b>Name1</b>: " + evt.graphic.attributes.NAME;

                            } else if (evt.graphic.attributes.GROUP_.length > 1) {

                                 theString = "<b>Name2</b>: " + evt.graphic.attributes.GROUP_;

                           } else if (evt.graphic.attributes.ROUTE_2.length > 1) {

                                 theString = "<b>Name3</b>: " + evt.graphic.attributes.ROUTE_2;

                           }  else {

                                theString = "<b>Name4</b>: " + evt.graphic.attributes.ROUTE_1;

                          }

                            ShowMapTip(evt, theString);

                        }