javascript - AngularJS fixed header scrollable table directive -
i'm stuck on problem involving fixed header table. i'm using angularjs fixed header scrollable table directive build table fixed header. once search/filter term (field on top of page) entered , removed -> tr rows don't fill hole page width (100%). hint: td width set static px values
i've put plunker.
directive code
/** * angularjs fixed header scrollable table directive * @author jason watmore <jason@pointblankdevelopment.com.au> (http://jasonwatmore.com) * @version 1.2.0 */ (function () { angular .module('angufixedheadertable', []) .directive('fixedheader', fixedheader); fixedheader.$inject = ['$timeout']; function fixedheader($timeout) { return { restrict: 'a', link: link }; function link($scope, $elem, $attrs, $ctrl) { var elem = $elem[0]; // wait data load , transform table $scope.$watch(tabledataloaded, function(istabledataloaded) { if (istabledataloaded) { transformtable(); } }); function tabledataloaded() { // first cell in tbody exists when data loaded doesn't have width // until after table transformed var firstcell = elem.queryselector('tbody tr:first-child td:first-child'); return firstcell && !firstcell.style.width; } function transformtable() { // reset display styles column widths correct when measured below angular.element(elem.queryselectorall('thead, tbody, tfoot')).css('display', ''); // wrap in $timeout give table chance finish rendering $timeout(function () { // set widths of columns angular.foreach(elem.queryselectorall('tr:first-child th'), function (thelem, i) { var tdelems = elem.queryselector('tbody tr:first-child td:nth-child(' + (i + 1) + ')'); var tfelems = elem.queryselector('tfoot tr:first-child td:nth-child(' + (i + 1) + ')'); var columnwidth = tdelems ? tdelems.offsetwidth : thelem.offsetwidth; if (tdelems) { tdelems.style.width = columnwidth + 'px'; } if (thelem) { thelem.style.width = columnwidth + 'px'; } if (tfelems) { tfelems.style.width = columnwidth + 'px'; } }); // set css styles on thead , tbody angular.element(elem.queryselectorall('thead, tfoot')).css('display', 'block'); angular.element(elem.queryselectorall('tbody')).css({ 'display': 'block', 'height': $attrs.tableheight || 'inherit', 'overflow': 'auto' }); // reduce width of last column width of scrollbar var tbody = elem.queryselector('tbody'); var scrollbarwidth = tbody.offsetwidth - tbody.clientwidth; if (scrollbarwidth > 0) { // reason trimming width 2px lines better scrollbarwidth -= 2; var lastcolumn = elem.queryselector('tbody tr:first-child td:last-child'); lastcolumn.style.width = (lastcolumn.offsetwidth - scrollbarwidth) + 'px'; } }); } } } })();
Comments
Post a Comment