Commits

Anonymous committed 9a12091

add some plugin functions

Comments (0)

Files changed (1)

htraf/js/htraf.plugins.js

 
 
 })(jQuery);
+
+
+// Misc functions
+
+// Convert table columns of named like_to_ into links when
+// data values are of form link_name|link_href
+function makeLinks(id)
+{
+    var table_selector = "#" + id;
+            
+    $(table_selector).find("th").each(function(i) {
+                  
+        if ( $(this).text().indexOf('link_to_') != -1 )
+        {
+            $(this).text($(this).text().substring($(this).text().indexOf('link_to_')+8));
+            selector = "td:nth-child("+(i+1)+")";
+            $(table_selector).find(selector).each(function(e) {
+                if ( $(this).text().indexOf('|') != -1 )
+                {
+                    $(this).html('<a href="'+$(this).text().substring($(this).text().indexOf('|')+1)+'">'+$(this).text().substring(0,$(this).text().indexOf('|'))+'</a>');
+                }
+            });
+        }
+    });
+
+}
+
+// hide column in tables
+function hideCol(id,col)
+{ 
+    selector = "#" + id + ' td:nth-child('+col+')';
+    $(selector).hide();
+    selector = "#" + id + ' th:nth-child('+col+')';
+    $(selector).hide();
+}
+    
+// right align column in tables
+function alignCol(id,col)
+{ 
+    selector = "#" + id + ' td:nth-child('+col+')';
+    $(selector).css("text-align","right");
+    selector = "#" + id + ' th:nth-child('+col+')';
+    $(selector).css("text-align","right");
+}
+
+
+// Add column totals to tables
+function colTotals(id,cols)
+{
+
+    selector = "#" + id + " tbody tr";
+    for (i in cols)
+    {
+        tot = 0;
+        dollars = false;
+        $(selector).children("td:nth-child(" + cols[i] + ")")
+        .each(function() {
+            if ($(this).html().indexOf('$')>=0)
+                dollars = true;
+            fl = isFloat( $(this).html().replace('$','').replace(/,/g,''));
+            if ( fl ) {
+                tot += parseFloat($(this).html().replace('$','').replace(/,/g,''));
+            } else {
+                tot += parseInt($(this).html().replace('$','').replace(/,/g,''));
+            }
+        });
+        
+        tot = (isFloat(tot)) ? tot.toFixed(2) : tot;
+        tot = addCommas(tot);
+        if (dollars)
+            tot = '$'+tot;
+
+        selector2 = "#" + id + " tfoot th:nth-child(" + cols[i] + ")";
+        alert(tot);
+        $(selector2).html(tot);
+
+    }
+
+}
+
+// used by colTotals
+function isFloat(x)
+{
+    if (x == parseInt(x) && x == parseFloat(x)) 
+    {
+        return false;
+    } 
+    else if (x == parseFloat(x)) 
+    {
+        return true;
+    } 
+    else 
+        return false;
+}
+
+// used by colTotals
+function addCommas(nStr)
+{
+	nStr += '';
+	x = nStr.split('.');
+	x1 = x[0];
+	x2 = x.length > 1 ? '.' + x[1] : '';
+	var rgx = /(\d+)(\d{3})/;
+	while (rgx.test(x1)) {
+		x1 = x1.replace(rgx, '$1' + ',' + '$2');
+	}
+	return x1 + x2;
+}
+
+// End add column totals to tables
+
+
+