Jump to content

Highligting fields based on a word


strido

Recommended Posts

I have a template with a rather advanced rule called "order details", that creates and places columns on the page, calling in 5 different fields that make up the order details text box.

 

I have been asked if I can shade two fields, if one of the two contains a specific word.

 

This template already shades every other line, so I would have to change the shading, in two areas (not the whole line) only if a specific word appears.

 

The keyword would be in the "description" field, and I would have to highlight that, and the field next to it - "Sku".

 

:confused:

Link to comment
Share on other sites

Apologies - it's pretty wordy, too

//Create an empty variable that will be populated with a

//string of text that lists the customer's purchases

returnStr = '';

var pod = 0;

var CustomState = '';

 

if(FusionPro.Composition.isPreview == true || FusionPro.inValidation == true)

{

Rule("OnJobStart");

}

 

 

//Get a count of the total number of records in the external data file

numRecsExtDF = externalDF.recordCount;

// margins="top:720;left:7200"

returnStr += '<table columns="6" title="Top" alignment="right" shadestyle="by:row;first:1,Gray,50;next:1,White,0" margins="left:0;top:0;bottom:30;right:0" >';

returnStr += '<column width="10000">';

returnStr += '<column width="6000">';

returnStr += '<column width="5800" >';

returnStr += '<column width="10000">';

returnStr += '<column width="24000">';

returnStr += '<column width="5000">';

 

 

var backGroundShading = 0;

 

shadestyle="by:row;first:3,green,5;next:1,red,5"

 

 

for (recordWalker=1; recordWalker <= numRecsExtDF; recordWalker++)

{

//Compare the value of the OrderID field in the current record

//we are looking at in the external data file to the value

//of the Orderid field in the main data file.

 

if (externalDF.GetFieldValue(recordWalker, 'OrderID') == Field("OrderID"))

{

 

 

 

 

{

 

// Internal ID

 

qty = externalDF.GetFieldValue(recordWalker, 'Quantity');

pod = externalDF.GetFieldValue(recordWalker, 'POD');

returnStr += '<row>';

 

backGroundShading = 0;

if(recordWalker % 2 == 1){

backGroundShading = 5;

 

}

 

returnStr += '<cell margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false">'

 

if (externalDF.GetFieldValue(recordWalker, 'StateKit') == '1') {

CustomState = ' (' + Left(externalDF.GetFieldValue(recordWalker, 'CustomState'),3) + ')'

returnStr += '<b>  STATE KIT ' + CustomState + ' </b>';

}

else

{

returnStr += externalDF.GetFieldValue(recordWalker, 'Category');

}

 

returnStr += '<cell margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false">' + externalDF.GetFieldValue(recordWalker, 'Location');

 

if (pod == 1){

returnStr += ' (POD)';

}

 

returnStr += '<cell margins="left:0;right:0;botton:0">' + '<p quad="R" br = "false">' + FormatNumber('#,###', qty) + ' ';

 

if (externalDF.GetFieldValue(recordWalker, 'StateKit') == '1')

{

returnStr += '<cell margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false"> ' + externalDF.GetFieldValue(recordWalker, 'SKU') + '<b>' + CustomState + '</b>' ;

returnStr += '<cell margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false"><b>' + Left(externalDF.GetFieldValue(recordWalker, 'ProductDescription'),55) + '</b>';

 

} else {

returnStr += '<cell margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false"> ' + externalDF.GetFieldValue(recordWalker, 'SKU');

returnStr += '<cell margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false">' + Left(externalDF.GetFieldValue(recordWalker, 'ProductDescription'),40);

 

}

returnStr += '<cell margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false">' + externalDF.GetFieldValue(recordWalker, 'Sequence');

}

}

}

 

// returnStr += '<row type="footer">';

// returnStr += '<cell hstraddle="6" rulings="top:thin,Black"> <cell><cell><cell><cell>';

 

returnStr += '</table>';

 

return returnStr;

 

I'm not much into coding unfortunately. I'm trying to modify this existing rule without much Javascript knowledge...

Link to comment
Share on other sites

Okay, I think you should be able to make your existing code work by making the following edits:

//Create an empty variable that will be populated with a 
//string of text that lists the customer's purchases
returnStr = '';
var pod = 0;
var CustomState = '';

[color="red"]var keyword = 'key'; // Your keyword
var flagColor = 'Red'; // The color you want the Description & SKU cells to be if keyword is found
var flagShade = 50; // The tint you of 'flagColor' you want the cells to display[/color]

if(FusionPro.Composition.isPreview == true || FusionPro.inValidation == true)
{
Rule("OnJobStart");
}


//Get a count of the total number of records in the external data file
numRecsExtDF = externalDF.recordCount;
// margins="top:720;left:7200"
returnStr += '<table columns="6" title="Top" alignment="right" shadestyle="by:row;first:1,Gray,50;next:1,White,0" margins="left:0;top:0;bottom:30;right:0" >';
returnStr += '<column width="10000">';
returnStr += '<column width="6000">';
returnStr += '<column width="5800" >';
returnStr += '<column width="10000">';
returnStr += '<column width="24000">';
returnStr += '<column width="5000">';


var backGroundShading = 0;

shadestyle="by:row;first:3,green,5;next:1,red,5"


for (recordWalker=1; recordWalker <= numRecsExtDF; recordWalker++)
{
//Compare the value of the OrderID field in the current record
//we are looking at in the external data file to the value
//of the Orderid field in the main data file.

if (externalDF.GetFieldValue(recordWalker, 'OrderID') == Field("OrderID"))
{




{ 

// Internal ID 

qty = externalDF.GetFieldValue(recordWalker, 'Quantity');
pod = externalDF.GetFieldValue(recordWalker, 'POD');
returnStr += '<row>';

[color="red"]var shading = flagColor && new RegExp('(\\b' + keyword + '\\b)', 'gi')
   .test(externalDF.GetFieldValue(recordWalker, 'ProductDescription')) ? 
   'shading="' + [flagColor,(flagShade || 100)].join(',') + '" ' : '';[/color]

backGroundShading = 0;
if(recordWalker % 2 == 1){
backGroundShading = 5;

}

returnStr += '<cell margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false">'

if (externalDF.GetFieldValue(recordWalker, 'StateKit') == '1') {
CustomState = ' (' + Left(externalDF.GetFieldValue(recordWalker, 'CustomState'),3) + ')'
returnStr += '<b>  STATE KIT ' + CustomState + ' </b>'; 
}
else
{
returnStr += externalDF.GetFieldValue(recordWalker, 'Category'); 
}

returnStr += '<cell margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false">' + externalDF.GetFieldValue(recordWalker, 'Location');

if (pod == 1){
returnStr += ' (POD)'; 
} 

returnStr += '<cell margins="left:0;right:0;botton:0">' + '<p quad="R" br = "false">' + FormatNumber('#,###', qty) + ' '; 

if (externalDF.GetFieldValue(recordWalker, 'StateKit') == '1')
{
returnStr += '<cell[color="red"] '+ shading +' [/color]margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false"> ' + externalDF.GetFieldValue(recordWalker, 'SKU') + '<b>' + CustomState + '</b>' ; 
returnStr += '<cell[color="red"] '+ shading +'[/color]margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false"><b>' + Left(externalDF.GetFieldValue(recordWalker, 'ProductDescription'),55) + '</b>';

} else {
returnStr += '<cell[color="red"] '+ shading +' [/color]margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false"> ' + externalDF.GetFieldValue(recordWalker, 'SKU'); 
returnStr += '<cell[color="Red"] '+ shading +' [/color]margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false">' + Left(externalDF.GetFieldValue(recordWalker, 'ProductDescription'),40); 

}
returnStr += '<cell margins="left:0;right:0;botton:0">' + '<p quad="L" br = "false">' + externalDF.GetFieldValue(recordWalker, 'Sequence'); 
}
}
}

// returnStr += '<row type="footer">';
// returnStr += '<cell hstraddle="6" rulings="top:thin,Black"> <cell><cell><cell><cell>';

returnStr += '</table>';

return returnStr;

What that does is: checks the 'ProductDescription' field of each record for the presence of "key" (you replace this with the word you're looking for). If it's found, it sets the "shading" variable to the value necessary to shade the background of the ProductDescription cell and SKU cell the specified color (in the case of this example: 50% Red).

 

That being said, there are a number of things in your existing code that aren't necessary. For example the brackets ({ }) before and after the "internal ID" section or the section that sets the value of a 'backGroundShading' variable that is never used in the rule. Instead of diving into the particulars of that, I think you could benefit more from the use of the 'FPTable' constructor. That would allow for some simplification in your code:

var numRecsExtDF = externalDF.recordCount;
var keyword = 'key';
var flagColor = 'Red';
var flagShade = 50;

if (IsPreview() || FusionPro.inValidation) {
 Rule("OnJobStart");
}

var table = new FPTable();
table.AddColumns(10000, 6000, 5800, 10000, 24000, 5000);

// Set the shading rules
table.ShadingColor1 = "Green";
table.ShadingPct1 = 5;
table.ShadingRepeat1 = 3;
table.ShadingColor2 = "Red";
table.ShadingPct2 = 5;
table.ShadingRepeat2 = 1;
table.ShadingType = "ByRow";

for (var recordWalker=1; recordWalker <= numRecsExtDF; recordWalker++) {

 // If the OrderID's don't match, skip it. 
 if (Field("OrderID") != ExField('OrderID')) continue;

 // Variable add rows
 var row = table.AddRow();

 // Set cell properties (alignment & margins)
 var cell = row.Cells[0];
 cell.Margins = {'Left': 200, 'Right': 200};
 cell.HAlign = 'Left';

 // Apply them to all cells on this row
 row.CopyCells(0, 1, 2, 3, 4, 5);

 // Helper variables
 var pod = ExField('POD') == '1' ? ' (POD)' : '';
 var sk = ExField('StateKit') == '1';
 var customState = Left(ExField('CustomState'), 3);

 // Variables that will hold the content of your columns
 var category = sk ? '<b>STATE KIT (' + customState + ')</b>' : ExField('Category');
 var location = ExField('Location') + pod;
 var qty = '<p quad="R" br=false>' + ExField('Quantity');
 var sku = ExField('SKU') + (sk ? '<b>' + customState + '</b>' : '');
 var desc = Left(ExField('ProductDescription'), (sk ? 55 : 40));
 var seq = ExField('Sequence');

 // Search for the 'keyword' in 'ProductDescription'
 if (new RegExp('(\\b' + keyword + '\\b)', 'gi').test(ExField('ProductDescription')) && flagColor) {
   [row.Cells[3], row.Cells[4]].forEach(function(s) {
     s.ShadeColor = flagColor
     s.ShadePct = flagShade || 100;
   });
 }

 // Assign the variables to the columns
 row.SetContents(category, location, qty, sku, desc, seq);
}

// Let FP convert the table into tags
return table.MakeTags();

// Helper function to return external field values
function ExField(str) { return externalDF.GetFieldValue(recordWalker, str); }

 

And if you happen to be using FP9, you can take advantage of the 'findRecords' function which will pull only the records that match the current 'OrderID' so that you don't have to iterate over the entire data file:

var keyword = 'key';
var flagColor = 'Red';
var flagShade = 50;

if (IsPreview() || FusionPro.inValidation) {
 Rule("OnJobStart");
}

var table = new FPTable();
table.AddColumns(10000, 6000, 5800, 10000, 24000, 5000);

// Set the shading rules
table.ShadingColor1 = "Green";
table.ShadingPct1 = 5;
table.ShadingRepeat1 = 3;
table.ShadingColor2 = "Red";
table.ShadingPct2 = 5;
table.ShadingRepeat2 = 1;
table.ShadingType = "ByRow";

var records = externalDF.FindRecords('OrderID', Field("OrderID"));
for (var rec in records) {
 // Variable add rows
 var row = table.AddRow();

 // Set cell properties (alignment & margins)
 var cell = row.Cells[0];
 cell.Margins = {'Left': 200, 'Right': 200};
 cell.HAlign = 'Left';

 // Apply them to all cells on this row
 row.CopyCells(0, 1, 2, 3, 4, 5);

 // Helper variables
 var pod = ExField('POD') == '1' ? ' (POD)' : '';
 var sk = ExField('StateKit') == '1';
 var customState = Left(ExField('CustomState'), 3);

 // Variables that will hold the content of your columns
 var category = sk ? '<b>STATE KIT (' + customState + ')</b>' : ExField('Category');
 var location = ExField('Location') + pod;
 var qty = '<p quad="R" br=false>' + ExField('Quantity');
 var sku = ExField('SKU') + (sk ? '<b>' + customState + '</b>' : '');
 var desc = Left(ExField('ProductDescription'), (sk ? 55 : 40));
 var seq = ExField('Sequence');

 // Search for the 'keyword' in 'ProductDescription'
 if (new RegExp('(\\b' + keyword + '\\b)', 'gi').test(ExField('ProductDescription')) && flagColor) {
   [row.Cells[3], row.Cells[4]].forEach(function(s) {
     s.ShadeColor = flagColor
     s.ShadePct = flagShade || 100;
   });
 }

 // Assign the variables to the columns
 row.SetContents(category, location, qty, sku, desc, seq);
}

// Let FP convert the table into tags
return table.MakeTags();

// Helper function to return external field values
function ExField(str) { return externalDF.GetFieldValue(records[rec], str); }

Link to comment
Share on other sites

Wow.

 

This works great, but there's one syntax issue I'm running into.

 

I apologize for not being more specific at the outset, but it seems the keyword actually contains two tilde's. ~~Steve (i mean.. really?? :eek:)Removing the tilde's from the that line of code will cause the rule to work to perfection. So if I just searched for 'Steve' I'm good. I'm sure it has something to do with the RegEx, but I can't seem to find the proper syntax that would allow me to search for the tilde's. I've tried '\~~Steve', but that doesn't seem to want to work.I really appreciate your help on this. It's amazing. Thanksfor your help and patience

 

Edit - the list of JavaScript colors I've found on in internet includes "GreenYellow" and "Lime" - but neither of these output as the right color. They default to black. Is there a limited palette available?

I'll leave that last question up in case someone else has the same problem. But it seems the colors are taken from the library for that specific template. In my case, I went in and created a new color, named it "LimeGreen" and put in the proper CMYK formula. From there I could add it to the flagcolor = "LimeGreen".

Edited by strido
Link to comment
Share on other sites

Edit - the list of JavaScript colors I've found on in internet includes "GreenYellow" and "Lime" - but neither of these output as the right color. They default to black. Is there a limited palette available?

I'll leave that last question up in case someone else has the same problem. But it seems the colors are taken from the library for that specific template. In my case, I went in and created a new color, named it "LimeGreen" and put in the proper CMYK formula. From there I could add it to the flagcolor = "LimeGreen".

Yes, it's the colors you define in your FusionPro template. Although you can also specify a color "on the fly" in a JavaScript rule, with the "new FusionProColor" function, like so:

new FusionProColor("LimeGreen", 100, 0, 75, 0);

The parameters are the color name, and the CMYK values from 0 to 100. Then you can use that named color just like any color defined in the Colors dialog.

 

Keep in mind when looking at online information about JavaScript that the primary usage of JavaScript is still for client web page scripting, so most examples will be in that context, and in fact, many online tutorials and docs will wrongly assume that you must be doing web development if you're using JavaScript. The basic language features of JavaScript are the same in a FusionPro rule as on a web page, but the integration is different. There's no document.alert function in FusionPro, and there's no Field function on a web page. Likewise, in a web client context, there will be lots of resources available, such as default web client colors, but that's specific to the web integration, and not part of the JavaScript language itself.

Link to comment
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.

×
×
  • Create New...