


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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.

The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion

Once you have mastered the entry-level TypeScript tutorial, you should be able to write your own code in an IDE that supports TypeScript and compile it into JavaScript. This tutorial will dive into various data types in TypeScript. JavaScript has seven data types: Null, Undefined, Boolean, Number, String, Symbol (introduced by ES6) and Object. TypeScript defines more types on this basis, and this tutorial will cover all of them in detail. Null data type Like JavaScript, null in TypeScript
