Oracle Apex:PL/SQL 块中的 Javascript 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34263401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:06:30  来源:igfitidea点击:

Oracle Apex: Javascript code in PL/SQL Block

javascriptoracleplsqloracle-apex

提问by ApexDev

Is it possible to have JavaScript code in the PL/SQL block. I want to execute the pl/sql block containing JavaScript code on submit in oracle Apex page process.

PL/SQL 块中是否可以包含 JavaScript 代码。我想在 oracle Apex 页面过程中提交时执行包含 JavaScript 代码的 pl/sql 块。

DECLARE
  v_count   NUMBER;

  BEGIN
        select count(*) into v_count
        from summary
        where prd_items = 'Total';

 HTP.p ('<script type="text/javascript">');
 HTP.p (   'alert(''The value of Total for BU is ' ||v_count|| '.\n'
      || 'You have to enter correct values to proceed further \n'');');
 HTP.p ('</script>');
END; 

I have Submitbutton in my page region and this pl/sql block is page processing item and execute on page submit(Conditional:Submit).

Submit我的页面区域中有按钮,这个 pl/sql 块是页面处理项目并在页面提交(Conditional:Submit)上执行。

But I am not able to pop-up the alert box. Please advise.

但我无法弹出警报框。请指教。

Thank you.

谢谢你。

采纳答案by brenners1302

Is it possible to have JavaScript code in the PL/SQL block?

PL/SQL 块中是否可以包含 JavaScript 代码?

  • YES
  • 是的

But, what you're trying to do wont work which is passing javascript function AFTER SUBMIT.It'll only work if you change the point of execution to AFTER HEADER.

但是,您尝试执行的操作是在提交后传递 javascript 函数不起作用。它仅在您将执行点更改为 AFTER HEADER 时才起作用。

Alternatively, if you just want to validate the values entered and doesn't want to use apex validation, you can use APEX_ERROR package.Try this.

或者,如果您只想验证输入的值并且不想使用顶点验证,您可以使用 APEX_ERROR 包。试试这个。

DECLARE
  v_count   NUMBER;

  BEGIN
        select prd_items into v_count
        from summary
        where prd_items = 'Total';
        -- I dont really know what you want to 
        --accomplish with this query but Im pretty sure 
        --It will not return a number
        -- if you want to count the number of prd_items it should be like this
        --select COUNT(*) 
        --into v_count
        --from summary
        --where prd_items = 'Total';


    APEX_ERROR.ADD_ERROR(
      p_message            => 'The value of Total for BU is '||v_count||'.<br>'||
                              'You have to enter correct values to proceed further',
      p_display_location   => apex_error.c_inline_in_notification 
    );  

END; 

EDIT: if you want to show the error if count not equal to 100 then do something like this:

编辑:如果您想在计数不等于 100 时显示错误,请执行以下操作:

DECLARE
  v_count   NUMBER;

  BEGIN

     Select COUNT(*) 
     into v_count
     from summary
     where prd_items = 'Total';

  IF v_count != 100 THEN
    APEX_ERROR.ADD_ERROR(
      p_message            => 'The value of Total for BU is '||v_count||'.<br>'||
                              'You have to enter correct values to proceed further',
      p_display_location   => apex_error.c_inline_in_notification 
    );  


 END IF;
END; 

回答by Cristian_I

To embed the javascript code from the pl/sql procedure you will have to place the procedure at a "Before Header" point. But i do not think this is the best solution for what you are trying to achieve.

要嵌入 pl/sql 过程中的 javascript 代码,您必须将该过程放在“Before Header”点。但我认为这不是您想要实现的最佳解决方案。

What you are trying to do is to add a validation right? If so why not use the apex validations. Create a validation with options like this:

您要做的是添加验证吗?如果是这样,为什么不使用顶点验证。使用以下选项创建验证:

  1. Identify the validation level:Page Item
  2. Identify the Page Item that is to be validated: Select the item you want.
  3. Select a validation type: PL/SQL
  4. Pick the type of validation you wish to create:Function Returning Error Text
  5. Validation Code:
  1. 确定验证级别:Page Item
  2. 确定要验证的页面项目:选择您想要的项目。
  3. 选择验证类型:PL/SQL
  4. 选择您要创建的验证类型:函数返回错误文本
  5. 验证码:
    DECLARE  v_count          NUMBER;
  V_validation_msg VARCHAR2(500);
BEGIN
  SELECT prd_items INTO v_count FROM summary WHERE prd_items = 'Total';
  V_validation_msg:='The value of Total for BU is ' ||v_count|| '.\n' || 'You have to enter correct values to proceed further';
  IF 1= 1 THEN --add some condition here if you want
    RETURN V_validation_msg;
  ELSE
    RETURN NULL;
  END IF;
END;
    DECLARE  v_count          NUMBER;
  V_validation_msg VARCHAR2(500);
BEGIN
  SELECT prd_items INTO v_count FROM summary WHERE prd_items = 'Total';
  V_validation_msg:='The value of Total for BU is ' ||v_count|| '.\n' || 'You have to enter correct values to proceed further';
  IF 1= 1 THEN --add some condition here if you want
    RETURN V_validation_msg;
  ELSE
    RETURN NULL;
  END IF;
END;
  1. When Button Pressed: Your Submit button.
  1. 按下按钮时:您的提交按钮。