How to perform row-by-row validation in Oracle APEX tabular form using Ajax

Using the same check / processing that I performed based on this thread:

Calling an Oracle Function Through Ajax in Place Verification Objectives in Oracle APEX v4.2.2

Now I have a tabular form that can contain from 1 to n rows, but when the user clicks the Apply button, I need to perform the same type of check that I have, based on the stream described above, that is:

An example rowset might look like this:

Col1    Col2    Col3    Col4
----------------------------
A       10      20      30
B       5       8       9
C       92      88      12
D       1       2       44
E       95      77      88

According to the above scenario, when the user clicks the Apply button, by calling Dynamic Action / Ajax (apex.server.process), I need to iterate through each row using the above four columns (without checkboxes used here, just select the lists, select the column values), call my ajax to check:

(Col2 > Col4) for all rows 

and if so, return via javascript, a warning message to the user indicating that the problem was found with the data (i.e. the INVALID response from apex.server.process).

I need to check this validation for all rows before writing records to the database.

Any help on how to achieve this (hopefully through DA / Ajax / jQuery) would be greatly appreciated.

Thank.

+3
source share
1 answer

. , (?) . . , , "". . , , td [headers]. , . js .
!

JavaScript:

function validaterows(){
  var arrf01 = [], arrf02 = [];

  //fetch all the values from the source columns and put them in
  //a javascript array.
  $("input[name=f03]").each(function(){
    arrf01.push($v(this));
  });

  $("input[name=f04]").each(function(){
    arrf02.push($v(this));
  });

  //provide the constructed arrays to the on-demand process by using
  //the global f## arrays
  apex.server.process ( "MY_PROCESS", {
      f01: arrf01
    , f02: arrf02
  }, {
  , success: function( pData ) { 
      //pData should be an object, because jquery will have parsed the returned json-string
      apex.debug(pData);

      $.each(pData.validationArray, function(index, value){
        if ( value === 'INVALID' ) {
          // do something here when the result is invalid
          // maybe you want to color something red for example
          alert('The data at row '+index+' is not valid!');
        };
      });

      }
  } );
}

plsql :

DECLARE
  l_return VARCHAR2(4000);
BEGIN
  FOR i IN apex_application.g_f01.count
  LOOP
    -- remember: the f## arrays are varchar arrays. Important for comparisons.
    -- Also take into account that the values could NOT be numeric at all.
    -- you'll probably want to run each value through an is-number check or else 
    -- you'll run into ORA errors
    IF to_number(apex_application.g_f01(i)) > to_number(apex_application.g_f02(i))
    THEN
      l_return := l_return || ',"INVALID"';
    ELSE
      l_return := l_return || ',"VALID"';
    END IF;
  END LOOP;

  IF l_return IS NOT NULL
  THEN
    -- create a json string 
    -- holds an object with 1 property (validationArray) with the value being
    -- an array holding a value for each row submitted
    l_return := '{"validationArray":['||LTRIM(l_return, ',')||']}';
  END IF;

  -- write the output to the buffer
  htp.p(l_return);
END;
+1

All Articles