


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>

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.


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

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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