3.2.0

Show/hide columns using Javascript

Here a little function I wrote today

/**
 * function toggleColumnVisibility(oDataGrid, iColumnNr)
 * 
 * + shows/hides column in grid
 * 
 * @param  object   oDataGrid  ActiveWidgets grid object
 * @param  integer  iColumnNr  column nr of column to show/hide
 * 
 * @returns  nothing
 **/
function toggleColumnVisibility(oDataGrid, iColumnNr)
{
  /* init */

  //get visible columns array
  var aVisibibleColumns    = oDataGrid.getColumnValues();
  
  //get index of column to hide    
  var iHideIndex = aVisibibleColumns.indexOf(iColumnNr);

  /* do something */

  if (iHideIndex>=0)
  {
    /* column found, hide it *
    aVisibibleColumns.splice(iHideIndex, 1);  
    oDataGrid.setColumnValues(aVisibibleColumns);  

    oDataGrid.refresh();
  }
  else
  {
    /* column not found, show it */
    aVisibibleColumns.splice(0, 0, iColumnNr);
    aVisibibleColumns.sort(function compareNumbers(a,b){return a - b;});       
    oDataGrid.setColumnValues(aVisibibleColumns);  

    oDataGrid.refresh();
  }      
}


This function needs the following prototype function:

/**
 * Array.prototype.indexOf
 * 
 * + adds indexOf functionality to JS Array objects
 *
 **/     
Array.prototype.indexOf = function(obj)
{
  for (var i = 0; i < this.length; i++)
  {
    if (this[i] == obj)
      return i;
  }
  return -1;
}


Have fun and take care!
Rekcor
March 8,
Woops, add a / to

/* column found, hide it *


But you found this one yourself, didn't ya :-)
Rekcor
March 8,

This topic is archived.

See also:


Back to support forum