Tag: library functions
Javascript HTML Tables EnsureMinimumNumberOfRows
by webdood on Apr.20, 2010, under HTML, Javascript, Software Development
Here I present a useful function called “EnsureMinimumNumberOfRows”.
This function operates on a table and effectively clones the last row in the table a given number of times to ensure that a minimum number of rows exist within the table. it does not clone the contents of the cells, but rather the nodes themselves and their classnames (by way of cloneNode(false).
///////////////////////////////////////////////////////////////////////////////
// EnsureMinimumNumberOfRows(element, params)
// ==========================================
// Ensures a table will have minimum number of visible rows.
// Supported params are:
// numberOfRows - gives the minimum number of rows that will appear
// default minimumNumberOfRows is 10
// rowHeight - height, in pixels for added rows. Default is 30px
// *NOTE: Does not support empty tables
////////////////////////////////////////////////////////////////////////////////
function EnsureMinimumNumberOfRows(element, params) {
var minimumNumberOfRows = params.numberOfRows || 10,
rowHeight = params.rowHeight || 30,
// Get the first element as $(element).select returns an array
// *Note this is a rare use of the Prototype Library on my part.
oTable = $(element).select('div.resultList table')[0],
numberOfRowsToInsert = minimumNumberOfRows - oTable.rows.length + 1;
if (numberOfRowsToInsert > 0) {
var clonedRow = oTable.rows[ oTable.rows.length - 1 ]
clonedCells = clonedRow.getElementsByTagName('td');
for (var i=0;i<numberOfRowsToInsert;i++) {
var oRow = document.createElement("TR");
for (j=0;j<clonedCells.length;j++) {
var oCell = clonedCells[j].cloneNode(false);
oCell.style.height = rowHeight + "px";
oCell.appendChild( document.createTextNode("\u00a0") );
oRow.appendChild(oCell);
}
oTable.appendChild( oRow );
}
}
}
Shannon Norrell
Now posted on GitHub
Javascript addClassName, hasClassName and removeClassName
by webdood on Apr.07, 2010, under Javascript, Software Development
Useful CSS Class handling functions.
Here I present addClassName, hasClass and removeClassName and also my old implementation of Array.indexOf. Since this is built into JS these days, you probably won’t need it.
addClassName and removeClassName are useful functions because you can pass in space separated classNames and it will add/remove them all.
////////////////////////////////////////////////////////////////////////////////
//
// addClassName([object|string] oHTMLElement, string classNameToAdd)
// ===========
// Adds classNameToAdd to an HTMLElement
// Guaranteed not to add the same className twice.
// - classNameToAdd can be a space separated list of classNames.
// - You can pass in the id to an object or the actual object
//
////////////////////////////////////////////////////////////////////////////////
function addClassName(oHTMLElement, classNameToAdd) {
if (typeof(oHTMLElement)=="string") {
oHTMLElement = document.getElementById(oHTMLElement);
}
if (oHTMLElement) {
var theClassName = oHTMLElement.className;
// If oHTMLElement already has a class name, some more work is needed
if (theClassName && (theClassName.length > 0)) {
var classNamesToAdd = classNameToAdd.split(" ");
// If we only have one className to potentially add, take the "less work" approach
if (classNamesToAdd.length===1 && ((" " + theClassName + " ").lastIndexOf(" " + classNameToAdd + " ") === -1) ) {
oHTMLElement.className = oHTMLElement.className + " " + classNameToAdd;
} else {
var theClassNames = theClassName.split(" "),
iEnd = classNamesToAdd.length,
aClassName,
theClassNamesToAddArray = [];
for (var i=0;i<iEnd;i++) {
aClassName = classNamesToAdd[i];
if (theClassNames.indexOf(aClassName)===-1) {
theClassNamesToAddArray.push( aClassName );
}
}
oHTMLElement.className = oHTMLElement.className + " " +((theClassNamesToAddArray.length > 1) ? theClassNamesToAddArray.join(" ") : theClassNamesToAddArray[0]);
}
} else {
// If oHTMLElement did not already have a class name, just add it
oHTMLElement.className = classNameToAdd;
}
}
}
////////////////////////////////////////////////////////////////////////////////
//
// hasClassName([object|string] oHTMLElement, string classNameOfInterest)
// Returns a boolean value of if an HTMLElement has the className of interest
// You can pass in the id to an object or the actual object
//
////////////////////////////////////////////////////////////////////////////////
function hasClassName(oHTMLElement, classNameOfInterest) {
return ((" " + oHTMLElement.className + " ").lastIndexOf(" " + classNameOfInterest + " ") > -1);
}
////////////////////////////////////////////////////////////////////////////////
//
// removeClassName([object|string] oHTMLElement, string classNameToRemove)
// Removes classNameToRemove from an HTMLElement, if it exists.
// - classNameToRemove can be a space separated list of classNames.
// - You can pass in the id to oHTMLElement or the actual object
//
////////////////////////////////////////////////////////////////////////////////
function removeClassName(oHTMLElement, classNameToRemove) {
if (typeof(oHTMLElement)=="string") {
oHTMLElement = document.getElementById(oHTMLElement);
}
if (oHTMLElement) {
var theClassName = oHTMLElement.className;
if (theClassName && (theClassName.length > 0)) {
var theClassNameArray = theClassName.split(" "),
classNamesToRemove = classNameToRemove.split(" "),
iEnd = theClassNameArray.length,
aClassName,
theNewClassNameArray = [];
for (var i=0;i<iEnd;i++) {
aClassName = theClassNameArray[i];
if (classNamesToRemove.indexOf(aClassName)===-1) {
theNewClassNameArray.push( aClassName );
}
}
switch (true) {
case (theNewClassNameArray.length>1) :
oHTMLElement.className = theNewClassNameArray.join(" ");
break;
case (theNewClassNameArray.length==1) :
oHTMLElement.className = theNewClassNameArray[0];
break;
case (theNewClassNameArray.length==0) :
oHTMLElement.className = "";
break;
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
//
// Array.indexOf() - returns integer index where valueToSearchFor is in an Array
//
////////////////////////////////////////////////////////////////////////////////
if (Array.prototype.indexOf===undefined) {
Array.prototype.indexOf = function( valueToSearchFor ) {
var iEnd = this.length;
var retVal = -1;
for (var i=0;i<iEnd; i++) {
if (this[i] == valueToSearchFor) {
retVal = i;
break;
}
}
return retVal;
};
}
by Shannon Norrell
showOrHide algorithm
by webdood on Mar.01, 2010, under Javascript, Software Development
Making it easier for me to look up my own most useful block of code for displaying/hiding elements. This is the same bit of code I wrote for Microsoft that shipped with Vista and now Windows 7 with the Sidebar gadgets.
////////////////////////////////////////////////////////////////////////////////
//
// showOrHide([object|string] oHTMLElement, boolean bShowOrHide)
// Shows or Hides an HTMLElement.
// You can pass in the id to an object or the actual object
//
////////////////////////////////////////////////////////////////////////////////
function showOrHide(oHTMLElement, bShowOrHide) {
try {
if (typeof(oHTMLElement)=="string") {
oHTMLElement = document.getElementById(oHTMLElement);
}
if (oHTMLElement && oHTMLElement.style) {
if (bShowOrHide == 'inherit') {
oHTMLElement.style.visibility = 'inherit';
} else {
if (bShowOrHide) {
if (oHTMLElement.nodeName == 'TR') {
oHTMLElement.style.visibility = 'inline-table';
} else {
oHTMLElement.style.visibility = 'visible';
}
} else {
oHTMLElement.style.visibility = 'hidden';
}
try {
if (bShowOrHide) {
oHTMLElement.style.display = 'block';
} else {
oHTMLElement.style.display = 'none';
}
}
catch (ex) {
}
}
}
}
catch (ex) {
}
}
Shannon Norrell
Now on GitHub
UPDATE TO THIS POSTING 4/14/2011
I have now taken to adding and removing a special className (usually called “hidden”) to oHTMLElements to show or hide them.
For example:
Assume in your CSS, there is a declaration like this:
.hidden { display:none; }
function showOrHide( oHTMLElement, bVisible ) {
if (typeof(oHTMLElement)=="string") {
oHTMLElement = document.getElementById(oHTMLElement);
}
if (oHTMLElement) {
if (!bVisible) {
addClassName( oHTMLElement, 'hidden' );
} else {
removeClassName( oHTMLElement, 'hidden' );
}
}
See our link about addClassName and removeClassName for more information on this technique.
Shannon Norrell



