In Oracle APEX, Interactive Grid column widths are usually fixed or manually adjusted.
However, in some scenarios, a static width may not be ideal—especially when the column data length varies significantly.
To improve readability and user experience, we can dynamically set the column width based on the actual data displayed.
For example, consider the EMP table, where the ENAME column contains employee names of varying lengths.
Instead of assigning a fixed width, we can automatically adjust the column width so that it fits the longest value in the grid.
This ensures that the data is fully visible without unnecessary truncation or excessive whitespace.
Step 1: Create an Interactive Grid and assign the static ID as EMP.
Step 2: Paste the following JavaScript code in Execute JavaScript Code under Page Load.
(function () {
var region = apex.region("EMP");
var ig$ = region.widget();
var gridView = ig$.interactiveGrid("getViews", "grid");
var grid$ = gridView.view$;
var model = gridView.model;
var col = "ENAME";
var maxLen = col.length;
console.log('maxLen='+maxLen);
model.forEach(function (rec) {
var v = model.getValue(rec, col);
if (v) {
maxLen = Math.max(maxLen, v.toString().length);
}
});
var widthPx = (maxLen * 9) + 20;
grid$.grid("setColumnWidth", col, widthPx);
})();
Comments
Post a Comment