Message Alert!

1206
10
Jump to solution
02-23-2018 02:07 PM
James_001
Occasional Contributor II

Hi fellows,

I was trying to add message alert in one of my custom widget to get the features count for two features layers together but somehow it only does for one layer. What is the best way to fix this? I will appreciate any help!

This is the block of code 

on.once(this.map, 'update-end', lang.hitch(this, function () {
var aa = this.layerInfosObj.getLayerInfoById('Mylayer_1 || Mylayer_2');
if (aa.layerObject.graphics.length === 0) {
this._showMessage("No features found");
}
else {
var a1 = aa.layerObject.graphics.length;
this._showMessage(a1 + " Feature(s) found");
}
}));

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
James_001
Occasional Contributor II

Thank You Robert,

Programming is Fun! Finally after little struggle, I got the message alert on my app here is the working code: This is my first little effort. Suggest me is it ok.

on.once(this.map, 'update-end', lang.hitch(this, function () {
var aa = this.layerInfosObj.getLayerInfoById('MyLayer_1);
var bb = this.layerInfosObj.getLayerInfoById('MyLayer_2');
if (aa.layerObject.graphics.length=== 0 || bb.layerObject.graphics.length=== 0){
this._showMessage("No features found");
}
// else {(aa.layerObject.graphics.length!= 0 || bb.layerObject.graphics.length!= 0)
else {
var a1 = aa.layerObject.graphics.length;
var b1 = bb.layerObject.graphics.length;
this._showMessage( a1+ b1+ " Feature(s) found");
}
}));

View solution in original post

0 Kudos
10 Replies
RobertScheitlin__GISP
MVP Emeritus

Irfan,

    You can not use a line like this:

var aa = this.layerInfosObj.getLayerInfoById('Mylayer_1 || Mylayer_2');

The getLayerInfoById is expecting one layer id and that is all.

Maybe something like:

var aa = this.layerInfosObj.getLayerInfoById('Mylayer_1');
var bb = this.layerInfosObj.getLayerInfoById('Mylayer_2');
...

is what you are looking for.

0 Kudos
James_001
Occasional Contributor II

Robert,

Thank you once more for your help!

I was trying the above code with your suggestions. Not an idea how to incorporate in this block of code to get the message alert for both layers. Can you please help me . I am new to programming and trying to learn.

I also have tried this too:

on.once(this.map, 'update-end', lang.hitch(this, function () {
var aa = this.layerInfosObj.getLayerInfoById('Mylayer_1');
var bb = this.layerInfosObj.getLayerInfoById('Mylayer_2');
if (aa.layerObject.graphics.length=== 0) || bb.layerObject.graphics.length=== 0){
this._showMessage("No features found");
} 
else {

var a1 = aa.layerObject.graphics.length ||var a2 = bb.layerObject.graphics.length;
this._showMessage(a1+a2+ " Feature(s) found");
}
}));

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Irfan,

   You do not use an || (or) in a variable declaration unless you do something like this.

var a1 = aa.layerObject.graphics.length || 0;
var a2 = bb.layerObject.graphics.length || 0;‍‍
0 Kudos
James_001
Occasional Contributor II

Thank You Robert,

Programming is Fun! Finally after little struggle, I got the message alert on my app here is the working code: This is my first little effort. Suggest me is it ok.

on.once(this.map, 'update-end', lang.hitch(this, function () {
var aa = this.layerInfosObj.getLayerInfoById('MyLayer_1);
var bb = this.layerInfosObj.getLayerInfoById('MyLayer_2');
if (aa.layerObject.graphics.length=== 0 || bb.layerObject.graphics.length=== 0){
this._showMessage("No features found");
}
// else {(aa.layerObject.graphics.length!= 0 || bb.layerObject.graphics.length!= 0)
else {
var a1 = aa.layerObject.graphics.length;
var b1 = bb.layerObject.graphics.length;
this._showMessage( a1+ b1+ " Feature(s) found");
}
}));

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Irfan,

   That is OK but it would be better if you use the bit I provided in my last reply.

0 Kudos
James_001
Occasional Contributor II

Robert,

Thanks. Something like this. It is also working.

on.once(this.map, 'update-end', lang.hitch(this, function () {
var aa = this.layerInfosObj.getLayerInfoById('MyLayer_1');
var bb = this.layerInfosObj.getLayerInfoById('MyLayer_1);
if (aa.layerObject.graphics.length=== 0 || bb.layerObject.graphics.length=== 0){
this._showMessage("No features found");
}
// else {(aa.layerObject.graphics.length!= 0 || bb.layerObject.graphics.length!= 0)
else {
var a1 = aa.layerObject.graphics.length || 0;
var b1 = bb.layerObject.graphics.length || 0;
this._showMessage( a1+ b1+ " Feature(s) found");
}
}));

0 Kudos
KenBuja
MVP Esteemed Contributor

Since the if statement uses the "Logical or" (||) operator, you'll get the message "No features found" when one of the layers has graphics but the other doesn't. Is that what you're expecting?

James_001
Occasional Contributor II

Ken,

Actually, I was trying to get message alert (no of features) on submit event button. By default, I am accessing both  layers together(same attribute). That's right if any layer have number of graphic it should show in message alert or on the other hand it show 'No features found" both layer must be true.

0 Kudos
KenBuja
MVP Esteemed Contributor

In that case, you should use the && operator instead.

0 Kudos