Skip to main content

Interactive Grid - Freeze/Unfreeze Columns Using JavaScript

In Oracle APEX forums, there was a question about "how to conditionally unfreeze interactive grid columns based on the screen size". I had bookmarked this question on my blog notes. Because, this is a good candidate for blogpost as it's a common use case and it may be helpful for other developers. 

So, I have started building demo for this blogpost. Then, I found that freezing, unfreezing columns based on column name may not be the best solution, because whenever we change column order, then we need to change JavaScript code as-well. With further research (reading documentation thoroughly 😊), I found out that we can also pass column definition object while using freezeColumn and unfreezeColumn methods. This solves the problem with column names. 

And then I did a little google search and found another similar question in APEX forums where John Snyders has provided a solution. This solution looks little different. Here, column property frozen is updated directly. Then I went back to documentation and found that freezeColumn/unfreezeColumn methods trigger the grid refresh as-well along with updating the column property. So, when we are freezing, unfreezing multiple columns, it is better to update the frozen property value first and at the end we can refresh the grid (only once).

Based on this, I have written a JavaScript function which freezes, unfreezes interactive grid columns based on screen size (window/viewport width). Here I am freezing up to first 4 columns based on screen size. If screen size is more than 1200 pixels, then I am freezing first 4 columns and if screen size is less than 600 pixels, then I am freezing only 1st column. When screen size is in between 600 and 1200 pixels, I am freezing 2 or 3 columns. I have used this logic for demo purpose and of course you can change it based on your requirements.

JavaScript function code:

function freezeUnfreezeIG(pIGStaticID) {
    apex.debug.info("BEGIN freezeUnfreezeIG");
    var grid$ = apex.region(pIGStaticID).call("getCurrentView").view$;
    apex.debug.info(grid$);
    // get column objects array
    var columnsArr = grid$.grid("getColumns");
    // get window width
    var winWidth = $(window).width();
    //apex.debug.info(`Window Width ${winWidth}`);
    // Unfeeze first 4 columns, ignoring hidden columns
    var i, counter = 0;
    for (i = 0; i < columnsArr.length; i++) {
        if (!columnsArr[i].hidden) {
            apex.debug.info(columnsArr[i]);
            columnsArr[i].frozen = false;
            //grid$.grid("unfreezeColumn", columnsArr[i]);
            counter++;
        }
        if (counter === 4) break;
    }
    var maxFrezee = 0;
    // determine number of columns to freeze based on window width
    if (winWidth <= 600) {
        maxFrezee = 1;
    }
    else if (winWidth > 600 && winWidth <= 900) {
        maxFrezee = 2;
    }
    else if (winWidth > 900 && winWidth <= 1200) {
        maxFrezee = 3;
    }
    else {
        maxFrezee = 4;
    }
    // freeze columns
    counter = 0;
    for (i = 0; i < columnsArr.length; i++) {
        if (!columnsArr[i].hidden) {
            columnsArr[i].frozen = true;
            //grid$.grid("freezeColumn",columnsArr[i]);
            counter++;
        }
        if (counter === maxFrezee) break;
    }
    grid$.grid("refreshColumns").grid("refresh");
    apex.debug.info("END freezeUnfreezeIG");
}

Demo:

For demo purpose, let's create a page with editable interactive grid on EMP table and give Static ID as emp.

Next, keep above JavaScript function code in Page > JavaScript > Function and Global Variable Declaration section.

Next, create dynamic action as below.

  • Name: Freeze/UnFreeze IG Columns (or any proper name)
  • When:
    • Event: Custom
    • Custom Event: apexwindowresized
    • Selection Type: JavaScript Expression
    • JavaScript Expression: window
  • True Action: Execute JavaScript Code
    • Code: freezeUnfreezeIG("emp");
    • Execution Options: 
      • Fire on Initialization: Yes (Enable)

Here, please observe, this dynamic action is fired when window is resized, as-well as when page is initially loaded. Executing this dynamic action on window resize will be helpful in several cases, for e.g. consider users with laptop and external monitor set-up. If users move your application window from external monitor to laptop, then you may want to unfreeze some of the columns as laptop screen width may be less compared to external monitor. 

That's it and here is the link for demo.

Thank you.

Comments

Popular posts from this blog

ORA-00936: missing expression error while updating data using Tabular form

Are you getting "ORA-00936: missing expression" error while trying to update existing data using tabular form? Are you using "Multi Row Update" or "Tabular Form" processes (ApplyMRU or Apply MRD), then most probably the issue is with a space or new line character you have entered in "Runtime Where Clause" section of "ApplyMRU" process However to know the exact reason behind the error, run page in debug mode and try saving data. Then see debug output. If the issue is with space or new line character which you have entered, then you will see entry similar to below as you can see above (highlighted with yellow color), APEX finds there is some value entered for  "Runtime Where Clause" and it will append this where clause to update query. Since it's not a valid SQL Update statement, we will get "ORA-00936: missing expression" error. Fix : It's obvious. Go to page designer and remove any spaces or new l...

Interactive Grid - Conditional Enable/Disable

In this blogpost, I am going to discuss few approaches using which we can conditionally enable/disable Interactive Grid (IG) column(s) based on other column(s) values. Note:    There is a bug  30801170  in APEX 19.2/20.1 with respect to "enable/disable" dynamic actions for IG columns. Workaround for this bug is provided at the end of this blogpost . This bug has been fixed in APEX version 20.2. Client Side Only Conditions If conditions to enable/disable are simple, then we can check those conditions easily on the client side. For e.g. let's consider IG on EMP table with following SQL Query. SELECT EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO FROM EMP Let's consider the requirement as - Enable "Commission" column only when JOB is equals to 'SALESMAN' and disable "Commission" column in all other cases. This can be done declaratively using dynamic actions (DA) DA "Enable/Disable Commission - 1" Create DA and give it a prope...

Interactive Grid - Bulk Operation on Selected Rows

What's the Problem? Let's say you have an IG on employee table and you want to update selected employees commission based on user's input. Ideally, it should be very simple, where you can write UPDATE statement in page process and select IG region as "Editable Region" under "Execution Options" of the page process. But, when you select rows and submit page, you can see that, this process won't get executed! The reason is  Selection of 'Row Selector' check-boxes is not considered as row-change. Thus selected rows are not submitted to server. So, is there any work around?  Yes! Luckily there are lot of JavaScript (JS) APIs available to work with IG. If you are not already aware, you can refer "APEX IG Cookbook"  or  JavaScript API Reference documentation. If we continue with above Employee IG example, when user selects IG rows, enters "Commission %" and clicks on "Update Commission" button, then we can writ...