You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Oracle Apex 5.1中根据输入数量动态添加文本框?

Got it, let's tackle this step by step for Oracle Apex 5.1. The core challenge here is bridging your PL/SQL logic with front-end rendering to create dynamic text boxes based on the input number. Here's a practical, tested approach:

1. Prep Your Page Structure First

  • Add a Static Region to your page (let's name its static ID P11_DYNAMIC_CONTAINER). This will be the placeholder where your dynamic text boxes appear.
  • Create a Hidden Item called P11_DYNAMIC_HTML, set its Storage Type to Session State — we'll use this to pass generated HTML from PL/SQL to the front end.
  • Add a validation to P11_TESTBOX (your number input) to ensure it's a positive integer:
    • Validation Type: Number
    • Validation Rule: Value > 0
    • Error Message: "Please enter a valid positive number of text boxes"

2. Refactor Your TEST_THIS Procedure

PL/SQL can't directly create front-end elements, so we'll modify it to generate clean, Apex-styled HTML for the text boxes. Update it like this:

CREATE OR REPLACE PROCEDURE TEST_THIS(
    p_num IN NUMBER,
    p_html OUT CLOB -- Use CLOB instead of VARCHAR2 to avoid length limits for large numbers
) AS
    v_html CLOB;
BEGIN
    v_html := '';
    FOR i IN 1..p_num LOOP
        -- Match Apex 5.1's default text box styling and add unique IDs/names
        v_html := v_html || '<div class="form-group">' ||
                  '<label class="control-label">Dynamic Text Box ' || i || '</label>' ||
                  '<input type="text" ' ||
                  'id="P11_DYNAMIC_' || i || '" ' ||
                  'name="P11_DYNAMIC[' || i || ']" ' ||
                  'class="apex-item-text form-control" />' ||
                  '</div>';
    END LOOP;
    p_html := v_html;
END;
/

Don't forget to grant execute permissions on this procedure to your Apex parsing schema (e.g., GRANT EXECUTE ON TEST_THIS TO APEX_PUBLIC_USER;).

3. Build the Dynamic Action for the TEST Button

This is where we tie everything together:

  1. Create a new Dynamic Action on the TEST button, triggered by Click.
  2. Add a True Action of type Execute PL/SQL Code:
    • PL/SQL Code:
      DECLARE
          v_output CLOB;
      BEGIN
          TEST_THIS(p_num => :P11_TESTBOX, p_html => v_output);
          :P11_DYNAMIC_HTML := v_output;
      END;
      
    • Under Items to Submit, add P11_TESTBOX
    • Under Items to Return, add P11_DYNAMIC_HTML
  3. Add a second True Action (right after the PL/SQL one) of type Execute JavaScript Code:
    • JS Code:
      // Clear existing content first, then insert new text boxes
      const container = document.getElementById('P11_DYNAMIC_CONTAINER');
      container.innerHTML = apex.item('P11_DYNAMIC_HTML').getValue();
      

4. (Optional) Capture Values from Dynamic Text Boxes

If you need to submit these dynamic text box values to the database later, use Apex's global arrays. Since we named the inputs name="P11_DYNAMIC[X]", you can retrieve them in PL/SQL like this:

DECLARE
    v_value VARCHAR2(4000);
BEGIN
    FOR i IN 1..:P11_TESTBOX LOOP
        v_value := apex_application.g_f01(i); -- Use g_f01, g_f02, etc. (avoid conflicting with existing form items)
        -- Do something with v_value (e.g., insert into a table)
    END LOOP;
END;

Note: Each g_fXX array holds up to 50 values — if you expect more than 50 text boxes, split them across multiple arrays (e.g., use g_f01 for 1-50, g_f02 for 51-100, etc.).

Testing Tips

  • First test the procedure directly in SQL Developer to confirm it generates valid HTML for a sample number (e.g., TEST_THIS(3, v_html); DBMS_OUTPUT.PUT_LINE(v_html);).
  • In Apex, use the browser's DevTools to check if the HTML is being inserted into the P11_DYNAMIC_CONTAINER correctly when you click the button.

内容的提问来源于stack exchange,提问作者fireside68

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:55:52