Do you find this script useful?
Yes
No
Comments?
JScript

When developing a display form for WebPublisher one often wants to output two fields side-by-side. However if these fields are repeatable fields DBTextWorks will generate all instances of the first field, followed by all instances of the second field. That is, it generates column-wise field1:value1, field1:value2...field1:valueN, followed by field2:value1, field2:value2...field2:valueN. Often you need to output the data in a row-wise order, e.g. field1:value1, field2:value1, field1:value2, field2:value2...field1:valueN, field2:valueN.

A typical application might be two repeatable fields URL and URLtext which contain (respectively) the internet address, and the associated displayed hyperlink text. One can construct HTML hyperlinks with these only if you can output <a href=URL1>URLtext1</a>, then <a href=URL2>URLtext2</a>, etc. That is, these must be output in corresponding pairs.

The solution to this problem requires one to create a new repeatable field into which the respective pairs will be put. It is this latter field which is then output in the web form, not the original fields. The attached script automatically generates these pairs row-wise, and puts them in the new field when a record is saved.

Using this method enables one to work around the shortcomings of the standard DBTextWorks table form.

Script Language: JavaScript For use with:
DB/TextWorks
Requires at least version 4.0
// A DbTextWorks script
// Automatically create a combined field (named "Combined")
// from the contents of two other fields (named "Field1"
// and "Field2"). Both fld1 and fld2 contain multiple entries,
// and there must be the same number of entries in both,
// otherwise a warning is issued.
// Combined is contructed with optional prefixes and suffixes.
//
// Barry Cheney. Parliament of Victoria.
// February 23, 2001

function onRecordSave()
{
  var fld1;                // Field1 box object
  var fld2;                // Field2 box object
  var Combined;            // Combined box object
  var fld1array;           // Array of Field1 entries
  var fld2array;           // Array of Field2 entries
  var Combinedcont;        // Contents of Combined box
  var p1;                  // Prefix for Field1
  var s1;                  // Suffix for Field1
  var p2;                  // Prefix for Field2
  var s2;                  // Suffix for Field2
  var f1;                  // Temporary Field1 string
  var f2;                  // Temporary Field1 string
  var f3;                  // Temporary contents of Field3 box
  var str;                 // Temporary string
  var es;                  // entry separator character
  var i;                   // Loop counter
  var nentry;              // No. of entries

// Extract contents of boxes
  fld1 = Form.boxes("Field1");
  fld2 = Form.boxes("Field2");
  Combined = Form.boxes("Combined");

// Set up prefixes and suffixes (optional)
  p1 = "Prefix1";
  p2 = "Prefix2";
  s1 = "Suffix1";
  s2 = "Suffix2";

// Check if boxes exist. If not, exit.
  if (!fld1)
    return;
  if (!fld2)
    return;

// Split entries into arrays at entry-separator
  es = Application.entrySeparator;
  fld1array = fld1.content.split(es);
  fld2array = fld2.content.split(es);

// Warn if Field1 and Field2 entries do not balance
  if (fld1array.length != fld2array.length) {
      Application.message("Number of entries must be equal");
      return;
  }

// Clear out all current Combined entries ...
  Combined.content = "";
  nentry = fld1array.length;
// If boxes have been cleared manually, length is
// not dynamically updated
// so forcibly reset nentry to 0.
  if ( fld1.content == "")
     nentry = 0;
  if ( fld2.content == "")
     nentry = 0;

// ... and re-create from current entries in Field1 and Field2
  for (i = 0; i < nentry; i++) {
      f1 = fld1array[i].toString();        // Convert entry to string
      f2 = fld2array[i].toString();        // Convert entry to string
      str = p1 + f1 + s1 + p2 + f2 + s2;   // Concatenate prefix, field, suffix
      if (Combined.content == "")          // Don't insert separator for first entry
         f3 = str;
      else
         f3 = Combined.content + es + str;
      Combined.content = f3;
  }
}
Notes: Just the caveat that I am not a Javascript expert. N.B. The conversion via .toString() may fail if the array element is not present. A test such as array[i] != null may need to be included.
Submitted by Barry Cheney on 7-Mar-2001 1:40

Powered by DB/Text WebPublisher, from Inmagic WebPublisher PRO