Skip to content

Instantly share code, notes, and snippets.

@maxx-coffee
Created June 23, 2014 20:54
Show Gist options
  • Save maxx-coffee/34eae2e11e8a54b0d416 to your computer and use it in GitHub Desktop.
Save maxx-coffee/34eae2e11e8a54b0d416 to your computer and use it in GitHub Desktop.
calculate table columns in javascript
calculate_summary = ($el) ->
$cells = $("td", $el)
$cells.each (i, v) ->
val = parseInt($(this).text())
int = if isNaN(val) then 0 else val
if !sums[i]?
sums[i] = 0
sums[i] = sums[i] + int
return sums
$ ->
$table = $("#daily_summary")
$rows = $("tr", $table)
window.sums = []
$.each $rows, (i, v) ->
window.calculated_sums = calculate_summary( $(this))
$lastRow = $("tr:last-child", $table)
calculated_sums.shift()
$.each calculated_sums, (i,v) ->
console.log "hi"
$lastCell = $("td:last-child", $lastRow)
$lastCell.after("<td>#{v}</td>")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment