本文介绍了如何使用Ajax在Oracle APEX表格上执行逐行验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于此线程,使用与我执行的相同的验证/处理:

调用Oracle APEX v4.2.2中通过Ajax实现Oracle功能以进行验证的目的

我现在有一个表格形式,可以有1到n行,但是当用户按下应用"按钮时,我需要执行基于上面线程的这种相同类型的验证,即: /p>

一组示例行可能类似于:

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

基于上述情况,当用户通过Dynamic Action/Ajax(apex.server.process)调用按下应用"按钮时,我需要使用上述四列(没有复选框)遍历每一行在这里使用,只需选择列表,以选择列值),请调用ajax进程检查是否:

(Col2 > Col4) for all rows

,如果是,则通过javascript返回一条警告消息,提示用户发现数据存在问题(例如,来自apex.server.process的INVALID响应).

在将记录提交到数据库之前,我基本上需要检查所有行的验证.

理想情况下,我想通过DA/Ajax/jQuery解决此问题.

解决方案

我将如何解决它.请注意,这并不涵盖所有(任何?)偶然性或大量错误处理.您必须自己详细说明.例如,我使用输入作为选择器,则需要选择".数组可能不匹配.您可能需要一个完全其他的选择器,例如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;

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

Calling an Oracle Function via Ajax for on the spot Validation Purposes in Oracle APEX v4.2.2

I now have a tabular form that can have 1 to n rows but when the user presses the "Apply" button, I need to perform this same type of validation, that I have, based on the thread above, i.e.:

An example set of rows might be like:

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

Based on the above scenario, when the user presses the "Apply" button, via a Dynamic Action/Ajax (apex.server.process) call, I need to iterate through each row, using the above four columns (no checkboxes used here, just select lists, to select column values), call myajax process to check whether:

(Col2 > Col4) for all rows

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

I basically need to check this validation for all rows, prior to committing records to the database.

Ideally I would like to solve this via DA/Ajax/jQuery.

解决方案

How I would try to solve it. Note that this doesn't cover all (any?) eventuality or much error handling. You'll have to elaborate on it a bit yourself. eg I used input for a selector, you'll need "select". The arrays may not match. You may need a completely other selector such as by td[headers]. Maybe your return object will need to hold other or more values. Your js may need more expanding upon.
Nevertheless it should provide you with a good base to start from!

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!');
        };
      });

      }
  } );
}

On-demand plsql process:

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;

这篇关于如何使用Ajax在Oracle APEX表格上执行逐行验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 00:18