Tuesday, 22 July 2025

ORACLE APEX IG/IR removing leading trailing spaces before searching

 

ORACLE APEX IG/IR removing leading trailing spaces before searching

Trimming your copy and paste search inputs

One of these requests landed on my to do list the other day. 

Brief was can I help when users copy and paste their inputs into Interactive reports/Interactive Grids search boxes to trim leading and trailing spaces. 

Very often your end users like to copy and paste inputs into APEX applications. 

Problem with this and most of us using web apps will know this beforehand we would copy and paste into text editor first then copy it again into desired search fields. 

Reason why we tend to do this is to avoid having issues caused with having to carry empty spaces when you copy the original text. 

How can we do this on application level so we do not have to do this on every page where reports are there. Of course we can look into Dynamic Actions and JavaScript 

On the page 0 page add this On page load JavaScript: 

function trimSearchInput() {
   $('.a-IRR-search-field, .a-IG .a-Toolbar-input').on('paste', function(e)
   {
     e.preventDefault();
     var myInput = e.originalEvent.clipboardData.getData('text');
     var firstChar = myInput.substr(0, 1);
     apex.debug.info(myInput);
     //only if input has trailing or leading white space
     if (myInput !== $.trim(myInput) ) {
       if (firstChar === '^') {
          // Do something if the input starts with '^'
          apex.debug.log("The a-IRR/IG-search-field input value starts with '^'.");
          this.value = myInput.substr(1);
       }
       else {
            apex.debug.log("Trimming the a-IRR/IG-search-field input value."); 
            this.value = $.trim(myInput);
       }
      }   
    }); 
}

//Try executing when theme is ready
apex.jQuery(window).on('theme42ready', function() {
  //apex.debug.log('Do a-IRR-search-field after UI elements are rendered on the page.');  

  setTimeout(function() {
     trimSearchInput();
   },1000);
});

//for slower internet connections 1second is not enough to load the page so setting a timer to wait until it loads
function waitForElement(selector, callback) {
    var interval = setInterval(function() {
        if ($(selector).length) {
            clearInterval(interval);
            callback($(selector)); // Pass the found element to the callback
        }
    }, 100); // Check every 100 milliseconds
}

// Example usage:
waitForElement('.a-IRR-pagination', function(element) {
    console.log('Element exists:', element);
    // Perform actions on the element here
    trimSearchInput();
});

// Example usage:
waitForElement('.a-IG .a-Toolbar-input', function(element) {
    console.log('Element exists:', element);
    // Perform actions on the element here
    trimSearchInput();
});

Happy APEXing,

SLino