Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old April 21st, 2022, 02:37 PM
LisaHansen LisaHansen is offline
Junior Community Member
 
Join Date: Sep 2008
Location: Atlanta, GA
Posts: 16
Default Issue with blank or no selection

I'm working on a project where images are returned from an external data file. The user can make up to three choices from drop-downs. In the ExDF, several drop-down options may point to the same image file. The client does not want duplicate images, so I put together the code below. If options are chosen for all three fields, it works great.

Code:
var externalDF = new ExternalDataFileEx("Competitor-ExDF.xlsx", "Excel");

var a = externalDF.FindRecord("CompetitorDropDown", Field("CP-Product-1-A"));
var b = externalDF.FindRecord("CompetitorDropDown", Field("CP-Product-1-B"));
var c = externalDF.FindRecord("CompetitorDropDown", Field("CP-Product-1-C"));

var caseA = ((externalDF.GetFieldValue(a, "CompetitorImage")));
var caseB = ((externalDF.GetFieldValue(b, "CompetitorImage")));
var caseC = ((externalDF.GetFieldValue(c, "CompetitorImage"))); 

var resultA = Resource(externalDF.GetFieldValue(a, "CompetitorImage")).content;
var resultB = Resource(externalDF.GetFieldValue(b, "CompetitorImage")).content;
var resultC = Resource(externalDF.GetFieldValue(c, "CompetitorImage")).content;


if ((caseA != caseB) && (caseA != caseC) && (caseB != caseC))  {return resultA + " " + resultB + " " + resultC;}

if ((caseA == caseB) && (caseA != caseC))  {return resultA + " " + resultC;}

if ((caseA == caseB) && (caseA == caseC))  {return resultA;}

if ((caseA != caseB) && (caseA == caseC))  {return resultA + " " + resultB;}

if ((caseA != caseB) && (caseA != caseC) && (caseB == caseC)) {return resultA + " " + resultB;}

else return "";
I'm having an issue if they don't choose something for all three. Ideally, they should be able to choose just A or A and B if they'd like. Currently, if only one or two options are chosen, it breaks the whole table. This rule is just for the first row. The images appear side-by-side if there is more than one unique image. How can I change this so if there is nothing in choices B or C it will still return A (or A & B if only C is blank)?

Thank you for looking!

Last edited by LisaHansen; April 21st, 2022 at 02:49 PM.. Reason: Added information
Reply With Quote
  #2  
Old April 21st, 2022, 03:07 PM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 957
Default Re: Issue with blank or no selection

Would this work?
Code:
var externalDF = new ExternalDataFileEx("Competitor-ExDF.xlsx", "Excel");
var unique = function (value, index, self) {
  return value ? self.indexOf(value) === index : false;
};
var getResource = function (value) {
  var fieldValue = externalDF.GetFieldValue(value, "CompetitorImage");
  return fieldValue ? Resource(fieldValue).content : '';
};
return ["CP-Product-1-A", "CP-Product-1-B", "CP-Product-1-C"]
  .map(Field)
  .filter(unique)
  .map(getResource)
  .filter(String)
  .join(' ');
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #3  
Old April 21st, 2022, 03:39 PM
LisaHansen LisaHansen is offline
Junior Community Member
 
Join Date: Sep 2008
Location: Atlanta, GA
Posts: 16
Default Re: Issue with blank or no selection

Thank you so much for checking this out, Step!

Currently, my tag file has data in all three fields. With your code, I get this error:
uncaught exception: Error: In Resource(), no resource named CompetitorImage.

I put my code back and it works again (with all three fields having data). I did test yours with B & C having no data and received the same result as with all three populated.
Reply With Quote
  #4  
Old April 21st, 2022, 05:21 PM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 957
Default Re: Issue with blank or no selection

Whoops I forgot to get the record from the external data field. Try this:
Code:
var externalDF = new ExternalDataFileEx("Competitor-ExDF.xlsx", "Excel");
var unique = function (value, index, self) {
  return value ? self.indexOf(value) === index : false;
};
var getResource = function (value) {
  var competitor = externalDF.FindRecord("CompetitorDropDown", value);
  if (competitor) {
    var competitorImage = externalDF.GetFieldValue(competitor, "CompetitorImage");
    if (competitorImage) {
      return Resource(competitorImage).content;
    }
  }
  return '';
};
return ["CP-Product-1-A", "CP-Product-1-B", "CP-Product-1-C"]
  .map(Field)
  .filter(unique)
  .map(getResource)
  .filter(String)
  .join(' ');
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #5  
Old April 21st, 2022, 06:04 PM
LisaHansen LisaHansen is offline
Junior Community Member
 
Join Date: Sep 2008
Location: Atlanta, GA
Posts: 16
Default Re: Issue with blank or no selection

This one works if any or all of them exists, however, it's allowing the duplicate image. Unfortunately, that's the reason I fell down this rabbit hole. In my tag, I have three different choices for A, B & C, they all point to the same image in the ExDF. It's showing that image three times instead of just showing it once.

Could you explain the logic in lines 2 & 3 for me, please? I've not seen that before and would love to understand what you had in mind there. I did try changing false to true in line 3, no dice.

I appreciate you!
Reply With Quote
  #6  
Old April 21st, 2022, 06:16 PM
LisaHansen LisaHansen is offline
Junior Community Member
 
Join Date: Sep 2008
Location: Atlanta, GA
Posts: 16
Default Re: Issue with blank or no selection

Hold up! Slight tweak and you did it!!!

I flipped your filters at the bottom so .filter(String) was first and .filter(unique) was second. I figured it needed the resource before it could know if it was unique or not.

You are brilliant! Thank you!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 06:13 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2022, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.