Welcome to the nuBuilder Forums!

Join our community by registering and logging in.
As a member, you'll get access to exclusive forums, resources, and content available only to registered users.

Filtering out (and not printing) empty columns

Questions related to customising nuBuilder Forte with JavaScript or PHP.
Post Reply
Paul
Posts: 105
Joined: Mon Aug 25, 2025 6:03 am
Has thanked: 24 times
Been thanked: 3 times

Filtering out (and not printing) empty columns

Unread post by Paul »

This code works great for filtering out a specified column based on non-admin users:

Code: Select all

if (!nuGlobalAccess()) {
    var columnIndex = 0;
    nuSetBrowseColumnSize(columnIndex, 0);
nuPrintExcludeColumns([columnIndex]);
}
Might there be a similar method for filtering out (and not printing) empty columns from the Browse form?
kev1n
nuBuilder Team
Posts: 4560
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 76 times
Been thanked: 526 times
Contact:

Re: Filtering out (and not printing) empty columns

Unread post by kev1n »

There’s no built-in function, but creating a JS function to dynamically show or hide columns shouldn’t be too difficult. AI can definitely help with that
Paul
Posts: 105
Joined: Mon Aug 25, 2025 6:03 am
Has thanked: 24 times
Been thanked: 3 times

Re: Filtering out (and not printing) empty columns

Unread post by Paul »

A search in AI has produced some code and I placed it below the code for non-globeadmin filtering like this, but I honestly have no idea how to 'massage' it for my purpose. I get no errors when logged in as globeadmin and all columns are shown. When logged in as non-globeadmin, there is no error in the console and all columns are shown.

Code: Select all

if (!nuGlobalAccess()) {
    var columnIndex = 0;
    nuSetBrowseColumnSize(columnIndex, 0);
nuPrintExcludeColumns([columnIndex]);
}



function hideBlankColumns(tableId) {
  // Find the table element using its ID.
  const table = document.getElementById(tableId);
  if (!table) {
    console.error(`Table with ID "${tableId}" not found.`);
    return;
  }

hideBlankColumns('cases');


  // Get all table rows.
  const rows = table.querySelectorAll('tr');
  if (rows.length === 0) {
    return; // No data rows to process.
  }

  // Determine the number of columns.
  const colCount = rows[0].querySelectorAll('th, td').length;
  const isColumnBlank = new Array(colCount).fill(true);

  // Iterate through all table rows, skipping the header.
  for (let i = 1; i < rows.length; i++) {
    const cells = rows[i].querySelectorAll('td');
    for (let j = 0; j < cells.length; j++) {
      // Check if the cell has content. Trim whitespace for accuracy.
      if (cells[j].textContent.trim() !== '') {
        isColumnBlank[j] = false; // Mark column as not blank.
      }
    }
  }

  // Apply styling to hide the blank columns.
  const headerCells = rows[0].querySelectorAll('th, td');
  for (let j = 0; j < colCount; j++) {
    if (isColumnBlank[j]) {
      // Hide the column header.
      if (headerCells[j]) {
        headerCells[j].style.display = 'none';
      }
      // Hide all corresponding data cells in that column.
      for (let i = 1; i < rows.length; i++) {
        const cells = rows[i].querySelectorAll('td');
        if (cells[j]) {
          cells[j].style.display = 'none';
        }
      }
    }
  }
}
kev1n
nuBuilder Team
Posts: 4560
Joined: Sun Oct 14, 2018 6:43 pm
Has thanked: 76 times
Been thanked: 526 times
Contact:

Re: Filtering out (and not printing) empty columns

Unread post by kev1n »

nuBuilder tables don’t use standard <table>, <tr>, or <td> elements. Instead, they are built with <div> elements. I provided the AI with a sample output from a nuBuilder table along with your code, and this is what it generated.

Code: Select all

if (!nuGlobalAccess()) {
    var columnIndex = 0;
    nuSetBrowseColumnSize(columnIndex, 0);
    hideBlankColumns();
    nuPrintExcludeColumns([columnIndex]);
}

function hideBlankColumns() {
    // Find the browse container element using its ID
    const browseDiv = document.getElementById('nuRECORD');
    if (!browseDiv) {
        console.error(`Browse div with ID "${'nuRECORD'}" not found.`);
        return;
    }

    // Get all browse title divs (headers)
    const headers = browseDiv.querySelectorAll('.nuBrowseTitle');
    if (headers.length === 0) {
        console.error('No browse title headers found.');
        return;
    }

    const colCount = headers.length;
    const isColumnBlank = new Array(colCount).fill(true);

    // Get all data cells (excluding empty rows)
    const dataCells = browseDiv.querySelectorAll('.nuCell[data-nu-column]');

    // Check each data cell to see if the column has content
    dataCells.forEach(cell => {
        const colIndex = parseInt(cell.getAttribute('data-nu-column'));
        const content = cell.textContent.trim();

        // Mark column as not blank if it has content
        if (content !== '' && colIndex >= 0) {
            isColumnBlank[colIndex] = false;
        }
    });

    // Hide blank columns using nuBuilder's built-in function
    for (let j = 0; j < colCount; j++) {
        if (isColumnBlank[j]) {
            nuSetBrowseColumnSize(j, 0);
        }
    }
}
To-do: exclude empty columns from printing
Post Reply