Jump to content
Sign in to follow this  
SDDENR

Javascript Loop

Recommended Posts

In my Details page I want to display a link dependent on whether the link field in my table contains an URL. Each record contains 50+ link fields named Link1, Link2, Link3, etc.

I have the following javascript code working to display the link only when the field has an URL present:

if('[@field:gw_spillpdfs_link1]' != "")

{

var frm = "PDF1";

document.write(frm.link('[@field:gw_spillpdfs_link1]'));

document.write(' ');

}

if('[@field:gw_spillpdfs_link2]' != "")

{

var frm = "PDF2";

document.write(frm.link('[@field:gw_spillpdfs_link2]'));

document.write(' ');

}

Here is the issue -- rather than have 50+ IF statements I would like to do a loop to move through the fields and then break the loop when I find an empty field (all the following fields will be empty) but I am having trouble with how to place the counter variable in the @Field reference -- the following does not work and I have tried several variations -- maybe you can't even use a variable as part of an @Field reference:

var i=1;

for (i=1;i<=100;i++)

{

if ('[@field:gw_spillpdfs_link]'+ i = "")

{

break;

}

var frm = "PDF"+i;

document.write(frm.link('[@field:gw_spillpdfs_link]'+i));

}

Javascript is not something I am very familiar with. If anyone can shed some light on this I would greatly appreciate it.

Thank you,

Ron

Share this post


Link to post
Share on other sites

Hello,

You may want to put them in an array:http://www.w3schools.com/js/js_obj_array.asp

var PDF = new Array("EditRecordgw_spillpdfs_link1","EditRecordgw_spillpdfs_link2","EditRecordgw_spillpdfs_link3","EditRecordgw_spillpdfs_link4") ;

function CheckLink() {

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

var frm = document.getElementById(PDF).value;

if (frm != "") {

.

.

.

}

}

}

document.getElementById("caspioform").onsubmit = CheckLink;

Share this post


Link to post
Share on other sites

Thank you for the response. Since the fields on my results form are display only, those elements on the web page do not have ID’s so using document.getElementById will not work -- at least that is my understanding based upon a past issue.

I will look into the ARRAY option but wonder whether I will still have the issue of how to combine an @FIELD value with the value of my counter variable.

Share this post


Link to post
Share on other sites

Thanks for being willing to take a look. Here is the initial search page from which you can work your way to a details page: http://denr.sd.gov/des/gw/Spills/dbspillsearch.aspx

At this point I just went with the 50+ "IF statements" to get the page up and running. The links show up fine but are not active yet (I have to have our tech folks do a bit of mapping to make them available online). The IF statement got so long I had to break it across 2 HTML Blocks due to a 10,000 character limit.

Ron

Share this post


Link to post
Share on other sites

I am looking at the original post and as I understand the objective of using this Java Script is to only show the field value if it is not blank. If this is correct, Caspio has a new feature which hides the field if it is blank, this feature will be available in the future release of Caspio Bridge which is scheduled for end of October.

I suggest if you wait for the feature instead of using such a long scripting.

Best,

Bahar M.

Share this post


Link to post
Share on other sites

Bahar,

Your assessment is correct and it sounds like a useful feature that Caspio has coming. There have been numerous times I have used javascript to display fields conditioned upon whether the field contains a value. Otherwise, using the caspio report interface, you end up with field labels for empty fields needlessly taking up space.

I'm still curious though as to whether it is possible to use a javascript variable as part of an "@Field" field name when doing a loop.

Thank you,

Ron

Share this post


Link to post
Share on other sites

Hi, Ron

No, it is not possible to use a javascript variable to enumerate @field placeholders. That is because values are inserted into such placeholders on the server side. It means that if you have the following javascript inserted into your DataPage:

var x = '[@field:field1]';

It will be rendered to, for example, the following:

var x = 'the value of field1';

This means that the browser never sees the @field notation, instead it receives from the server the actual values of the fields.

Share this post


Link to post
Share on other sites

ShWolf,

Thank you for answering the question. It now makes perfect sense as to why it can't be done. The value for the @Field is already delivered by the server at the point when I am trying to tell it what to deliver through use of a variable. It is obvious now that the "big picture" has been explained.

Thanks again!!

Ron

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...