jquery/javascript how to calculate width faster -
I have a table with 100 columns. I am running a loop through all the other elements, so that Width can be set explicitly.
var $ ths = $ ("table th"); $ Ths.each (function (i, th) {var $ th = $ (th); var width = $ th.width (); $ th.width (width);}); For 100 columns, it takes a lot of time (more than 60). Each "received width" call takes 500-700 ms because the need to prepare the layout every time, I get the width
Question - is there a fast way to get the width?
The reason - I am freeing the header. For this, I have to set the width of each header for fixed pixel width. Everything is dynamic and the pre-set width of each column is not an option. .
You might consider writing plain vanilla javascript for it. Maybe your milliseconds may be shave from your time.
var cells = myTable.rows [0]. children; For (var i = 0; i & lt; cells; length; i ++) {cells [i] .style.width = cells [i] .offsetwidth + "px"; } Because you are using the offsetwid, however, the width of the cell is estimated based on how many letters of TH (assuming that the text is not wrapped and that you have a Define, using monotype fonts) and
// Doc head functions Attach CSS classes to the stylesheet in append style (var css = document.createElement ('style') ; Css.type = 'text / css'; // Check browser compatibility if (CSS. Stylesheet) CSS. Stylesheet.code text = style; Else css.appendChild (document.createTextNode (style)); Document.getElementsByTagName ("head") [0] .appendChild (CSS); } Var cells = myTable.rows [0]. children; Var cellWidth = 0; Var letter wide = 5; // Assume that each letter is 5px wide var classname = ''; Var styles = ''; For (var i = 0; i You can take it even further by providing all the things outside the DOM on the runout and by using the fragments of the document () by swapping the piece after loading the DOM ... but Seriously, at this point you really have to ask yourself that the juice is worth squeezing.
Comments
Post a Comment