Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Click the paperclip at the top of the page to access and download a sample application that demonstrates these functions in action.


Table of Contents

Check Table for Entry

Checks to see if the specified text exists anywhere (any cell, any row) within the table. If the string is found then the function returns true, otherwise false.

...

Code Block
languagejs
// table: This is the actual table object, not the string ID (e.g. BO.F_Table)
// entry: this is the value of what you want to search for, the content of the table column will be converted to a string
// usage: This is usually called within the onClick of a button that adds rows to a table.  Or you might want to search to see if a record exists in the table.

app.getSharedData().checkTableForEntry = function(table, entry) {
  var entryFound = false;

  for(var i = 0; i < table.getLength(); i++) {
      var row = table.get(i);
      var tmpentry = entry;

      //loop through all the columns of the source table
      var cols = row.getChildren();
      for(var j=0;j<cols.getLength();j++) {
          var cfld = cols.get(j);
          var c = fld.getValue();
  
          if(typeof c === "string") {
            if(c.indexOf(tmpentry) !== -1) {
              return true;
            }
          } else if(c instanceof Datefld.getType() {
    === "date" || fld.getType() === "timeStamp") {
            var entryAsDt = new Date(entry);
            if(entryAsDt.getYear() === c.getYear() && entryAsDt.getMonth() === c.getMonth() && entryAsDt.getDay() === c.getDay()) {
              return true;
            }    
          } else {

            //Convert to string for comparison             
            c = c.toString();            

            if(c === entry) {
              return true;
            }
          }
        } //inner for
      } //outer for
  return entryFound;
}

...

Code Block
languagejs
if(!app.getSharedData().checkTableForEntry(BO.F_Table, theSearchValue)) {
     //the string was NOT found in the table, so now do something!
}


Check Table for Entry By Column

This function checks a specific column of a table for the specified string.  If the string is found then the function returns true, otherwise false. 

...

Code Block
languagejs
// table: This is the actual table object, not the string ID (e.g. BO.F_Table)
// column: This is the string of the columnID that you want to seach
// entry: this is the value of what you want to search for, the data type should match that of the column you are searching (i.e. string, int, date)
app.getSharedData().checkTableForEntryByColumn = function(table, column, entry) {
  var entryFound = false;

  for(var i = 0; i < table.getLength(); i++) {
    var row = table.get(i);
    var tmpfld = get(row,column);
    var tmpval = tmpfld.getValue();
    if(typeof tmpval === "string") {
      if(tmpval.indexOf(entry) !== -1) {
        entryFound = true;
        break;
      }
    } else if(tmpval instanceof Datetmpfld.getType() === "date" || tmpfld.getType() === "timeStamp") {
      var entryAsDt = new Date(entry);
      if(entryAsDt.getYear() === tmpval.getYear() && entryAsDt.getMonth() === tmpval.getMonth() && entryAsDt.getDay() === tmpval.getDay()) {
       entryFound = true;
       break;
      }    
    } else {
      tmpval = tmpval.toString();
      if(tmpval === entry) {
        entryFound = true;
        break;
      }
    }
  }
  return entryFound;
}

...

Code Block
languagejs
var row = app.getSharedData().getTableRowThatContainsEntry(BO.F_theTableObj, 'F_theColumnToSearch','theValueYouAreSearchingFor');
if(row !== null) {
 //do something with the row
}

Count Table Rows that Contain Entry By Column

Returns the number of rows that contain the specified entry.

...

Code Block
languagejs
// table: This is the actual table object, not the string ID (e.g. BO.F_Table)
// column: This is the string of the columnID that you want to seach
// entry: this is the value of what you want to search for, 
// the data type should match that of the column you are searching (i.e. string, int, date)
app.getSharedData().countTableRowsForEntry = function(table, column, entry) {
   var var entryFoundCount = 0;
  for  for(var i = 0; i < table.getLength(); i++) {
    if(get(
    var r = table.get(i),column).getValue() === entry) {
      entryFoundCount++;
    }
  }
  return entryFoundCount;
}

2. To use this function you need to decide where and how you want to check the value that the user might be trying to add. When you call the function you would use the following statement:

Code Block
languagejs
app.getSharedData().countTableRowsForEntry(BO.F_theTableObj,'F_theColumnToSearch','theValueYouAreSearchingFor');

Calculate Sum of ColumnA Where ColumnB Meets Specified Criteria

Returns the sum of columnA for all the rows where columnB matches the specified entry.  For example: Sum all the 'currency fields' in the rows where dropdown = 'approved'

1. Copy the code to the Settings...Events...onStart section of your application.

Code Block
languagejs
// table - pass the table object - BO.F_Table
// columnA - the string ID of the field to be part of the summation
// columnB - the string ID of the field used to determine if the row should be included
// entry - the string value to compare against columnB
app.getSharedData().sumColumnA_searchByColumnB = function(table, columnA, columnB, entry) {  
  var sum = 0;
  for(var i = 0; i < table.getLength(); i++) {
    if(get(table.get(i),columnB).getValue;
    var fld = get(r,column);
    var val = fld.getValue();
    
    if(typeof val === "string") { 
      if(val.indexOf(entry) !== -1) {
        entryFoundCount++;
      }
    } else {
      if(val.toString() === entry) {
        entryFoundCount++;
      }
    }
  }
  return entryFoundCount;
}


Note

This function as written will not all using a date or timestamp field as the search modifier (ColumnB).  If you want to enable it to support dates then it will need to be modified to include the instanceOf logic that may be found in the checkTableForEntry function.


2. To use this function you need to decide where and how you want to check the value that the user might be trying to add. When you call the function you would use the following statement:

Code Block
languagejs
app.getSharedData().countTableRowsForEntry(BO.F_theTableObj,'F_theColumnToSearch','theValueYouAreSearchingFor');

Calculate Sum of ColumnA Where ColumnB Meets Specified Criteria

Returns the sum of columnA for all the rows where columnB matches the specified entry.  For example: Sum all the 'currency fields' in the rows where dropdown = 'approved'

1. Copy the code to the Settings...Events...onStart section of your application.

Code Block
languagejs
// table - pass the table object - BO.F_Table
// columnA - the string ID of the field to be part of the summation
// columnB - the string ID of the field used to determine if the row should be included
// entry - the string value to compare against columnB
app.getSharedData().sumColumnA_searchByColumnB = function(table, columnA, columnB, entry) {  
  var sum = 0;
  for(var i = 0; i < table.getLength(); i++) {
    var r = table.get(i);
    var fld = get(r,columnB);
    var val = fld.getValue();

    if(typeof val === "string") { 
      if(val.indexOf(entry) !== -1) {
        sum += parseFloat(get(r,columnA).getValue());
      }
    } else {
      if(val.toString() === entry) {
        sum += parseFloat(get(table.get(i),columnA).getValue());     }   }   return sum; }

...

+= parseFloat(get(r,columnA).getValue());
      }
    }
  }
  return sum;
}


Note

This function as written will not all using a date or timestamp field as the search modifier (ColumnB).  If you want to enable it to support dates then it will need to be modified to include the instanceOf logic that may be found in the checkTableForEntry function.

2. This function assumes that you are trying to sum a set of rows of a table where a field in those rows meets a specified criteria.  Copy the function into the Settings...Events section of your application.

To use this function you will need to place the following in the onShow event of the field where the sum is being shown, as well as the onAdd and onRemove events of the table: 

Code Block
languagejs
BO.F_Currency4.setValue(app.getSharedData().sumColumnA_searchByColumnB(BO.F_Table, 'F_Currency', 'F_DropDown4', 'Approved'));

Copy Table Contents to another Table

Copy all the rows from one table to another.  This assumes that the tables have the same number of rows and it will copy the columns in order.  There is no error checking so if you try to copy a datatype that does not match it may throw a run-time error.

...

Code Block
languagejs
app.getSharedData().copyRowsToAnotherTable(BO.F_Table, BO.F_Table0, true);

Duplicate Selected Row and Append to Same Table

This function will duplicate the selected row of a table and then append it to the end of the table.

...

Code Block
languagejs
app.getSharedData().duplicateSelectedRow(BO.F_Table, page.F_Table.getSelection());

Move a Table Row Up One Position

Change the order of the table by moving a row up one position.

...

Code Block
languagejs
app.getSharedData().moveSelectionUp(BO.F_InputParameters, page.F_InputParameters.getSelection())
page.F_InputParameters.setSelection(-1);

Move a Table Row Down One Position

Change the order of the table by moving a row down one position.

...

Code Block
languagejs
app.getSharedData().moveSelectionDown(BO.F_InputParameters, page.F_InputParameters.getSelection())
page.F_InputParameters.setSelection(-1);

Function:

Get Index of Row in Table

Description:

This is a function that will allow you to determine the index of a specific row in a table.  Now the intention of this function is to use it in conjunction with the table.getSelection() which returns the "selected" row, by passing that into this function you can get back the "index" of that row in the table.

...

Code Block
languagejs
var inx = app.getSharedData().getIndexInTable(BO.F_Table1,page.F_Table1.selectedRow);

 Print The Contents of a Table

I created this function because I wanted a quick way to render the content of a table, that comes back from a service call, in a pure text format.  This is an iterative function that walks all the rows and columns of a table and converts its content to a string.  This is just an example of the type of thing that could be accomplished.  Ideally you would take the premise and expand it for your situation.  You may want to add more detailed html or css to really fine-tune what the printed result looks like.

...