Grid Checkbox - auto data fill and Update data Process

 


My Table Structure.

  CREATE TABLE "TEST_DOC" 

   ( "DOCUMENT_ID" VARCHAR2(30), 
      "PARTY_NAME" VARCHAR2(250), 
"DOCUMENT_DATE" DATE, 
"CLEAR_DATE" DATE, 
"DESCRIPTION" VARCHAR2(500), 
"AMOUNT" NUMBER, 
"STATUS" VARCHAR2(20)
   ) ;

Step:1
=====

Create Grid Region.
And Set Static ID: clear

Create Page Item: P108_UPDATE_DATA

Select Grid Region and create a dynamic Action on grid region.

Action Name: GRID_DATA_AUTO_FILL_1

Action: Execute JavaScript Code
Code: 

var gridView = apex.region("clear").call("getViews", "grid");  // Grid Static ID : clear

var model = gridView.model;

//var record = this.data.record;

var selectedRecords = gridView.getSelectedRecords();

var pairs = [];


// 1. Define today's date as the default

var today = new Date();

var defaultDate = apex.date.format(today, "DD-MON-YYYY");

var ig$ = apex.region("clear").widget();   // Grid Static ID : clear

//gridView.setSelectedRecords([record], true);


selectedRecords.forEach(function(record) {

    var id = model.getValue(record, "DOCUMENT_ID");

    var dt = model.getValue(record, "CLEAR_DATE");

    var doc_date = model.getValue(record, "DOCUMENT_DATE");


    // 2. Logic: If date is empty, use the default today's date

    if (!dt || dt === "") {

        dt = doc_date ; //defaultDate;

        // Optional: Visually update the grid so the user sees the date

        model.setValue(record, "CLEAR_DATE", doc_date);

    }else {

        // 2. If NOT NULL: Re-assign the same value to force the row to 'Modified' state

        // This ensures the row is sent to the server during the Save process.

        model.setValue(record, "CLEAR_DATE", dt);

    }

    

    if (id) {

        // 3. Format exactly as your PL/SQL expects: ["DATE","ID"]

        pairs.push('["' + id + '","' + dt + '"]');

    }

    if (!ig$.interactiveGrid("getActions").get("edit")) {

    ig$.interactiveGrid("getActions").set("edit", true);

     }

    

});


// 4. Assign the concatenated string to your item

apex.item("P108_UPDATE_DATA").setValue(pairs.join(":"));


Step:2
=====

Select Grid Column CLEAR_DATE and Create Dynamic Action.

Action Name: GRID_DATA_AUTO_FILL_2

Event: Change
Selection Type: Column(s)
Interactive Grid: TEST_DOC_GRID
Column(s): CLEAR_DATE

Action: Execute JavaScript Code
Code: 

// 1. Get the new date value from the input

var newValue = $v(this.triggeringElement);


// 2. Identify the specific row being edited

var $te = $(this.triggeringElement);

var rowId = $te.closest('tr').data('id'); 

var ig$ = apex.region("clear").widget(); // Grid Static ID : clear

// 3. Access the model

var igRegion = apex.region("clear"); // Grid Static ID : clear

var model = igRegion.widget().interactiveGrid("getViews", "grid").model;


var finalValues = [];

//var defaultDate = apex.date.format(today, "DD-MON-YYYY");

// 4. Loop through all rows

model.forEach(function(record) {

    var recordId = model.getRecordId(record);

    var rowDate;

    // Get the ID for this row (Replace 'DOCUMENT_ID' with your actual column name)

    var docId = model.getValue(record, "DOCUMENT_ID");

   

    if (recordId == rowId) {

        rowDate = newValue;

    } else {

        rowDate = model.getValue(record, "CLEAR_DATE");

    }


    // Only add if both ID and Date exist

    if (docId && rowDate && rowDate.toString().trim() !== "") {

       // finalValues.push("[" + docId + ":" + rowDate + "]");

        finalValues.push('["' + docId + '","' + rowDate + '"]');

    }


    if (!ig$.interactiveGrid("getActions").get("edit")) {

    ig$.interactiveGrid("getActions").set("edit", true);

     }

});


// 5. Join the pairs with a colon (or any other separator you prefer)

apex.item("P108_UPDATE_DATA").setValue(finalValues.join(":"));


Step:3
=====

Create a PL/SQL process for data update.

PL/SQL Code:

declare

    v_cleared_date DATE;
    v_payment_id   VARCHAR2(100);


BEGIN

    -- 1. Split the main string by the colon separator

    FOR r IN (

        SELECT column_value AS pair 

        FROM table(apex_string.split(:P108_UPDATE_DATA, ':'))

    )

    LOOP

        -- 2. Extract Date and ID using Regex 
        -- Removes brackets/quotes and splits by comma

        v_payment_id   := REGEXP_SUBSTR(r.pair, '"([^"]+)"', 1, 1, NULL, 1);

        v_cleared_date := TO_DATE(REGEXP_SUBSTR(r.pair, '"([^"]+)"', 1, 2, NULL, 1), 'DD-MON-YYYY');

        

        --  Logic for CLEARING 

        IF :P108_UPDATE_DATA  IS NOT NULL THEN

            UPDATE    TEST_DOC
               SET CLEAR_DATE    = v_cleared_date,
                   STATUS = 'CLEARED'
            WHERE DOCUMENT_ID = v_payment_id;

       END IF;

    END LOOP;


    COMMIT;

exception when others then

NULL;

END;


When Button Pressed: Update

Post a Comment

Previous Post Next Post