Oracle Apex 22.2:点击Add Row按钮默认进入单行视图的实现问题
Solution for Oracle Apex 22.2 Interactive Grid: Add Row Directly to Single-Row View & Return to Report After Save
Issue Analysis
Your current code inserts a new record but fails to select it before invoking the single-row view, which causes the view to open the previously selected record (or the first record if no selection exists). To fix this, you need to explicitly select the newly created record before switching to single-row view. Additionally, we'll add logic to return to the report view after saving.
Corrected Initialization JavaScript Code
function(config) { config.initActions = function(actions) { // Remove default "Add Row" action actions.remove('selection-add-row'); // Add custom "Add Row" action actions.add({ name: "selection-add-row", label: "Add row", iconBeforeLabel: true, action: function(event, focusElement) { // Get the Interactive Grid instance const ig = $(actions.context).interactiveGrid('instance'); const model = ig.model; // Insert new record and capture the reference to it const newRecord = model.insertNewRecord(); // Select the newly created record in the grid view ig.view.setSelectedRecords([newRecord]); // Switch to single-row view for the new blank record ig.actions.invoke("single-row-view"); } }); // Add listener to return to report view after save completes const ig = $(actions.context).interactiveGrid('instance'); ig.model.on('save', function() { // Small delay to ensure save operation finishes before switching views setTimeout(() => { ig.actions.invoke('report-view'); }, 400); }); }; return config; }
Key Fixes Explained
Capture & Select New Record:
model.insertNewRecord()returns the newly created record object. We store this innewRecord.ig.view.setSelectedRecords([newRecord])sets the grid's selection to the new record, ensuring the single-row view opens the correct entry.
Return to Report View After Save:
- We listen to the model's
saveevent, which triggers when a save operation starts. - A short
setTimeoutensures the save completes before invokingreport-viewto switch back to the grid's default view. Adjust the delay (in milliseconds) if needed based on your application's performance.
- We listen to the model's
Notes
- If you want the new record inserted at the end instead of the beginning, use
model.insertNewRecord(-1)(the-1parameter specifies inserting at the last position). - The
setTimeoutis a reliable way to handle the asynchronous save operation; alternative approaches like listening tointeractivegridaftersaveevent can also be used if preferred.
内容的提问来源于stack exchange,提问作者listix
相关产品推荐
相关产品推荐

