Last active
August 29, 2015 14:02
-
-
Save desigens/d7c33ea6137ce80825c2 to your computer and use it in GitHub Desktop.
Sum of selected cells in table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.getScript('//code.jquery.com/ui/1.10.4/jquery-ui.js', function () { | |
function toFixed(value, precision) { | |
var power = Math.pow(10, precision || 0), | |
strip = value * power; | |
return Math.round(parseFloat(strip.toPrecision(12))) / power; | |
} | |
function format(x) { | |
x = x.toString().split('.') | |
x[0] = x[0].replace(/\B(?=(\d{3})+(?!\d))/g, " "); | |
return x.join('.'); | |
} | |
var css = document.createElement("style"); | |
css.type = "text/css"; | |
css.innerHTML = ".ui-selection:after {content: attr(title); position: absolute; top: 100%; width: 100%; text-align: center; background: rgba(0,0,0,.7); color: #fff; padding: 0 3px 3px; margin-left: -3px; margin-top: 3px } .ui-selection {pointer-events: none; border: 3px rgba(0,0,0,.7) solid; position: absolute; box-sizing: border-box; } .ui-selected, .ui-selecting { background: #efefef; }"; | |
document.body.appendChild(css); | |
var $selection = $('<div class="ui-selection"></div>'); | |
$('table').selectable({ | |
filter: 'td', | |
stop: function () { | |
var $cells = $(".ui-selected", this), | |
count = 0; | |
$cells | |
.each(function() { | |
var number = parseFloat($(this).text().trim().replace(/\s/gi, '')); | |
count += number || 0; | |
}); | |
$selection.attr('title', format(toFixed(count, 4))); | |
var first = $cells.first().offset(); | |
var last = $cells.last().offset(); | |
$selection.css(first); | |
$selection.css({ | |
height: last.top - first.top + $cells.last().innerHeight(), | |
width: last.left - first.left + $cells.last().innerWidth() | |
}); | |
$('body').append($selection); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment