diff options
author | Michael Leibman <michael.leibman@gmail.com> | 2012-01-23 19:09:38 -0800 |
---|---|---|
committer | Michael Leibman <michael.leibman@gmail.com> | 2012-01-23 19:09:38 -0800 |
commit | 1190b2317f82930774b8893410be32704426b46b (patch) | |
tree | 93b3aa99dcb3cf6fff81667ae60fdfbaa5b9e685 /slick.grid.js | |
parent | 8f5e9be816b3632ac69c7f04afb9e27f63290351 (diff) | |
download | SlickGrid-1190b2317f82930774b8893410be32704426b46b.zip SlickGrid-1190b2317f82930774b8893410be32704426b46b.tar.gz SlickGrid-1190b2317f82930774b8893410be32704426b46b.tar.bz2 |
Fixed autosizeColumns (Issues #185 and #248).
Diffstat (limited to 'slick.grid.js')
-rw-r--r-- | slick.grid.js | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/slick.grid.js b/slick.grid.js index f1610f2..a286945 100644 --- a/slick.grid.js +++ b/slick.grid.js @@ -845,37 +845,43 @@ if (typeof Slick === "undefined") { widths = [], shrinkLeeway = 0, total = 0, - existingTotal = 0, + prevTotal = 0, availWidth = viewportHasVScroll ? viewportW - scrollbarDimensions.width : viewportW; for (i = 0; i < columns.length; i++) { c = columns[i]; widths.push(c.width); - existingTotal += c.width; - shrinkLeeway += c.width - Math.max(c.minWidth || 0, absoluteColumnMinWidth); + total += c.width; + if (c.resizable) { + shrinkLeeway += c.width - Math.max(c.minWidth, absoluteColumnMinWidth); + } } - total = existingTotal; - // shrink - while (total > availWidth) { - if (!shrinkLeeway) { - return; - } + prevTotal = total; + while (total > availWidth && shrinkLeeway) { var shrinkProportion = (total - availWidth) / shrinkLeeway; for (i = 0; i < columns.length && total > availWidth; i++) { c = columns[i]; - if (!c.resizable || c.minWidth === c.width || c.width === absoluteColumnMinWidth) { + var width = widths[i]; + if (!c.resizable || width <= c.minWidth || width <= absoluteColumnMinWidth) { continue; } - var shrinkSize = Math.floor(shrinkProportion * (c.width - Math.max(c.minWidth || 0, absoluteColumnMinWidth))) || 1; + var absMinWidth = Math.max(c.minWidth, absoluteColumnMinWidth); + var shrinkSize = Math.floor(shrinkProportion * (width - absMinWidth)) || 1; + shrinkSize = Math.min(shrinkSize, width - absMinWidth); total -= shrinkSize; + shrinkLeeway -= shrinkSize; widths[i] -= shrinkSize; } + if (prevTotal == total) { // avoid infinite loop + break; + } + prevTotal = total; } // grow - var previousTotal = total; + prevTotal = total; while (total < availWidth) { var growProportion = availWidth / total; for (i = 0; i < columns.length && total < availWidth; i++) { @@ -887,10 +893,10 @@ if (typeof Slick === "undefined") { total += growSize; widths[i] += growSize; } - if (previousTotal == total) { + if (prevTotal == total) { // avoid infinite loop break; - } // if total is not changing, will result in infinite loop - previousTotal = total; + } + prevTotal = total; } for (i = 0; i < columns.length; i++) { |