


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>

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

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.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

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.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.