Home >Web Front-end >JS Tutorial >Step by step analysis of the use of ScrollableGridPlugin.js (fixed header) plug-in in jQuery_jquery
This ScrollableGridPlugin.js is a plug-in with a fixed header effect found on the Internet. It is very easy to use, and the effect looks good. But after all, it is not tailor-made, so I still need to make some small changes in some parts of my project. Because I really like this plug-in, I entered it for the first time to see the original author's ideas and writing methods. , and then you can know how to change it to suit your project.
I am a very amateur player when it comes to js. I will analyze this plug-in based on my current situation. Anyway, I have figured out what is going on. If the analysis is incorrect, please ask experts for advice.
/*! * This plug-in is developed for ASP.Net GridView control to make the GridView scrollable with Fixed headers. */ (function ($) { $.fn.Scrollable = function (options) {//1、定义一个jQuery实例方法,也是我们调用这个插件的入口 var defaults = { ScrollHeight: 300, Width: 0 }; var options = $.extend(defaults, options); //2、扩展集合,如果外部没有传入ScrollHeight的值,就默认使用300这个值,如果传入,就用传入的ScrollHeight值 return this.each(function () { var grid = $(this).get(0);//3、获取当前gridview控件的对象 var gridWidth = grid.offsetWidth;//4、获取gridview的宽度 var headerCellWidths = new Array(); for (var i = 0; i < grid.getElementsByTagName("TH").length; i++) { headerCellWidths[i] = grid.getElementsByTagName("TH")[i].offsetWidth; }//5、创建了一个存储表头各个标题列的宽度的数组 grid.parentNode.appendChild(document.createElement("div"));//6、在文档中gridview的同级位置最后加一个div元素 var parentDiv = grid.parentNode;//7、gridview的父节点,是个div var table = document.createElement("table");//8、在dom中创建一个table元素 for (i = 0; i < grid.attributes.length; i++) { if (grid.attributes[i].specified && grid.attributes[i].name != "id") { table.setAttribute(grid.attributes[i].name, grid.attributes[i].value); } }//9、把gridview的所有属性加到新创建的table元素上 table.style.cssText = grid.style.cssText;//10、将样式也加到table元素上 table.style.width = gridWidth + "px";//11、为table元素设置与gridview同样的宽 table.appendChild(document.createElement("tbody"));//12、为table元素加一个tbody元素 table.getElementsByTagName("tbody")[0].appendChild(grid.getElementsByTagName("TR")[0]);//13、把gridview中的第一行数据加到tbody元素中,即table里现在存着一行gridview的标题元素,同时在gridview里删除了标题这一行的元素 var cells = table.getElementsByTagName("TH");//14、取得表格标题列的集合 var gridRow = grid.getElementsByTagName("TR")[0];//15、gridview中第一行数据列的集合 for (var i = 0; i < cells.length; i++) { var width; if (headerCellWidths[i] > gridRow.getElementsByTagName("TD")[i].offsetWidth) {//16、如果标题格的宽度大于数据列的宽度 width = headerCellWidths[i]; } else {//17、如果标题格的宽度小于数据列的宽度 width = gridRow.getElementsByTagName("TD")[i].offsetWidth; } cells[i].style.width = parseInt(width - 3) + "px"; gridRow.getElementsByTagName("TD")[i].style.width = parseInt(width - 3) + "px";//18、将每个标题列和数据列的宽度均调整为取其中更宽的一个,-3是出于对表格的样式进行微调考虑,不是必须 } parentDiv.removeChild(grid);//19、删除gridview控件(注意只是从文档流中删除,实际还在内存当中,注意27条),现在的情况是table里只有一个表头 var dummyHeader = document.createElement("div");//20、在文档中再创建一个div元素 dummyHeader.appendChild(table);//21、把表头table加入其中 parentDiv.appendChild(dummyHeader);//22、把这个div插入到原来gridview的位置里 if (options.Width > 0) {//23、如果我们最初传入了一个想要的表格宽度值,就将gridWidth赋上这个值 gridWidth = options.Width; } var scrollableDiv = document.createElement("div");//24、再创建一个div gridWidth = parseInt(gridWidth) + 17;//25、在原基础上+17是因为这是一个具有滑动条的table,当出现滑动条的时候,会在宽度上多出一个条的宽度,为了使数据列与标题列保持一致,要把这个宽度算进行,17这个值也不是必须,这个可以试验着来。 scrollableDiv.style.cssText = "overflow:auto;height:" + options.ScrollHeight + "px;width:" + gridWidth + "px";//26、给具有滚动条的div加上样式,height就是我们想让它在多大的长度时出现滚动条 scrollableDiv.appendChild(grid);//27、将gridview(目前只存在数据存在数据列)加到这个带有滚动条的div中,这里是从内存中将grid取出 parentDiv.appendChild(scrollableDiv);//28、将带有滚动条的div加到table的下面 }); }; })(jQuery);
Only by understanding what is going on inside the plug-in can you know how to modify it.
Actually, there is something here that I still don’t quite understand, and Baidu has not been able to figure it out. I hope friends who understand can tell me, that is, grid.getElementsByTagName("TR") is used in both places 13 and 15[ 0]); On the surface, this statement should get the same tr, right? But when I traced it through the browser, I found that the call in 13 got the first tr of the grid, which is the title tr containing the th column. The one in 15 was also the first tr in the grid, but it contained The first data column tr of column td.
The strange thing is that after executing 13, the number of tr in the grid is reduced by 1, that is, the tr containing the th column is missing. I thought the appendChild method transferred elements for insertion, rather than copying elements for insertion, but checking this method did not clearly indicate that it was what I thought. I was a little confused.
The calling method of this plug-in is as follows. Friends who are interested can try it. It feels really good.
<script type="text/javascript" src="../Scripts/PlugIn/ScrollableGridPlugin.js"></script> <script type="text/javascript"> jQuery(document).ready(function () { jQuery("#<%=gv_bugList.ClientID %>").Scrollable({ ScrollHeight: 400, width: 500 }); }) </script>