MarcomCentral (PTI) and FusionPro User Communities

MarcomCentral (PTI) and FusionPro User Communities (http://forums.pti.com/index.php)
-   The JavaScript Library (http://forums.pti.com/forumdisplay.php?f=23)
-   -   Issue with blank or no selection (http://forums.pti.com/showthread.php?t=10645)

LisaHansen April 21st, 2022 03:37 PM

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!

step April 21st, 2022 04:07 PM

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(' ');


LisaHansen April 21st, 2022 04:39 PM

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.

step April 21st, 2022 06:21 PM

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(' ');


LisaHansen April 21st, 2022 07:04 PM

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!

LisaHansen April 21st, 2022 07:16 PM

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!


All times are GMT -7. The time now is 07:19 AM.

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