Method and device for realizing table cascading scrolling based on DOM simulation
By setting up a virtual DOM structure and differentiation algorithm in the table, only rendering nodes in the visible area and optimizing event processing, it solves the problems of low performance and high complexity of traditional table cascade scrolling, and achieves efficient table rendering and scrolling performance. , while ensuring cross-browser compatibility.
Patent Information
- Application Number
- CN202410674038.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-28
- Publication Date
- 2024-08-30
- Estimated Expiration
- 2044-05-28
AI Technical Summary
When traditional methods implement cascading table scrolling, frequent DOM queries and update operations lead to high performance consumption, and full rendering causes performance burden when processing large amounts of data. Complex event monitoring and coordinate calculations increase implementation complexity and computational burden.
Simulate the DOM structure of the table by setting the data structure, initialize the virtual DOM, render nodes only in the visible area, use a differentiation algorithm to compare the virtual DOM before and after the update, perform the minimum necessary DOM operations, and optimize event processing with throttling and anti-shake technology , regularly clean up virtual DOM nodes and resources that are no longer needed.
Significantly improves the rendering and scrolling performance of large data tables, simplifies implementation complexity, and ensures cross-browser compatibility.
Smart Images

Figure CN118244957B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data processing, and in particular to a method, a device and an electronic device for realizing table cascade scrolling based on DOM simulation. Background Art
[0002] Cascading scrolling of tables is usually achieved by directly manipulating the DOM with JavaScript. Although this method can meet the requirements in terms of functionality, it has some major defects:
[0003] 1. The traditional method relies on direct manipulation of the DOM to achieve scrolling effects. Every time a scroll event occurs, a large number of DOM queries and updates are performed. This operation consumes a lot of performance because the browser needs to frequently perform layout calculations and redraw the page.
[0004] 2. Without the application of virtual scrolling or virtual DOM technology, table rendering must process and display all data rows instead of just rendering the part visible to the user. This full rendering method causes unnecessary performance burden, especially when processing large amounts of data.
[0005] 3. In order to achieve synchronous scrolling of multiple table areas (such as fixed table headers and columns), complex event monitoring and coordinate calculation are required. This not only increases the complexity of implementation, but also increases the computational burden at runtime.
[0006] The above problems become technical problems that need to be solved. Summary of the invention
[0007] In view of this, the embodiments of the present invention provide a method, device and electronic device for implementing Table cascading scrolling based on DOM simulation, which at least partially solve the problems existing in the prior art.
[0008] In a first aspect, an embodiment of the present invention provides a method for implementing Table cascading scrolling based on DOM simulation, comprising:
[0009] Setting a data structure to simulate the DOM structure of a table, wherein the data structure includes row, column, and cell information, as well as position, size, and style attributes of the row, column, and cell, initializing a virtual DOM according to a data source of the table, mapping the data to the structure of the virtual DOM, and creating a virtual node for each row, each column, and each cell;
[0010] According to the size and scroll position of the table container, the current visible area is calculated. According to the visible area, the corresponding node is selected from the virtual DOM. Only when the node enters the visible area, it is rendered as an actual DOM element.
[0011] Set a scroll event listener on the parent container of the table. When a scroll event is triggered, calculate the scroll offset, which will be used to update the visible area of the virtual DOM. For the table parts that need to be scrolled synchronously, calculate their new positions according to the scroll offset and update the corresponding virtual DOM nodes.
[0012] Use a differential algorithm to compare the virtual DOM before and after the update to determine the nodes that need to be added, deleted or updated, and then generate a differential comparison result. Based on the differential comparison result, perform the minimum necessary DOM operation to update the user interface.
[0013] According to a specific implementation of the embodiment of the present disclosure, after performing the minimum necessary DOM operation to update the user interface according to the differential comparison result, the method further includes:
[0014] Use throttling and debouncing techniques to optimize the execution of event handling functions;
[0015] Regularly clean up virtual DOM nodes and related resources that are no longer needed;
[0016] Implement performance monitoring to identify and resolve bottlenecks in the rendering process and ensure smooth scrolling operations.
[0017] According to a specific implementation of the embodiment of the present disclosure, the step of setting a data structure to simulate a DOM structure of a table includes:
[0018] Define Table to represent the data structure of the entire table, including rows, columns, and global style attributes;
[0019] Define Row to represent a row in a table, containing a list of cells and the position, size, and style attributes of the row;
[0020] Define Column to represent a column in a table, including the position, size, and style attributes of the column;
[0021] Definition Cell represents a cell, including the cell's content, position, size, and style attributes. The cell is linked to the row and column data structures for positioning and style calculations.
[0022] According to a specific implementation of the embodiment of the present disclosure, the step of setting a data structure to simulate a DOM structure of a table includes:
[0023] Create a virtual Table node based on the size and structure of the data source. The Table node contains global style attributes.
[0024] For each row in the data source, create a virtual Row node and set its position, size, and style properties;
[0025] For each data item in Row, create a virtual Cell node, which contains the cell content, position, size, and style attributes. When the data item is a nested structure, create child Cell nodes recursively.
[0026] Add the created Cell node to the corresponding Row node, and add the Row node to the Table node.
[0027] According to a specific implementation of the embodiment of the present disclosure, the current visible area is calculated according to the size and scrolling position of the table container, and the corresponding node is selected from the virtual DOM according to the visible area, including:
[0028] It is necessary to obtain the size of the table container, which includes the width and height of the container. By querying the scroll offset of the container, the scroll position of the container is determined, and the current visible area is calculated by combining the size and scroll position of the container.
[0029] Traverse the row or cell nodes in the virtual DOM. For each node, calculate its actual position in the container based on its position information in the virtual DOM, compare the node's position with the visible area, and determine whether the node is in the current visible area.
[0030] For nodes that enter the visible area, create or update the corresponding actual DOM elements, apply the style and content of the virtual node to the actual DOM elements, and for nodes that leave the visible area, recycle or hide the corresponding actual DOM elements.
[0031] According to a specific implementation of the embodiment of the present disclosure, a scroll event listener is set on the parent container of the table. When the scroll event is triggered, the scroll offset is calculated, and the offset is used to update the visible area of the virtual DOM. For the table parts that need to be scrolled synchronously, their new positions are calculated according to the scroll offset, and the corresponding virtual DOM nodes are updated, including:
[0032] To determine the parent container element of the table, the parent container element is the object to which the scroll event listener is attached, add a scroll event listener to the parent container, and the listener is triggered when the parent container scrolls;
[0033] When a scroll event is triggered, the listener obtains the current scroll offset and calculates the scroll amount in the vertical and horizontal directions based on the scroll offset.
[0034] According to a specific implementation of the embodiment of the present disclosure, a scroll event listener is set on the parent container of the table. When the scroll event is triggered, the scroll offset is calculated, and the offset is used to update the visible area of the virtual DOM. For the table parts that need to be scrolled synchronously, their new positions are calculated according to the scroll offset, and the corresponding virtual DOM nodes are updated, including:
[0035] Recalculate the current visible area using the scroll offset and the size of the parent container. The visible area is the currently visible part of the parent container.
[0036] Based on the new visible area, update which nodes in the virtual DOM should be rendered as actual DOM elements;
[0037] According to the scroll offset of the parent container, the new position of the synchronous scrolling part is calculated, and the node corresponding to the synchronous scrolling part in the virtual DOM is updated to reflect the new position of the node corresponding to the synchronous scrolling part in the virtual DOM.
[0038] According to a specific implementation of an embodiment of the present disclosure, the virtual DOM before and after the update is compared using a differential algorithm to determine the nodes that need to be added, deleted or updated, and then generate a differential comparison result. According to the differential comparison result, the minimum necessary DOM operation is performed to update the user interface, including:
[0039] Compare the virtual DOM tree before and after the update structurally. For each pair of compared nodes, check whether their types are the same. If the node types are different, delete the old node and add the new node. If the node types are the same, compare the properties of the nodes to check which properties are added, deleted, or modified.
[0040] For nodes that exist in the new tree but not in the old tree, add them to the DOM. For nodes that exist in the old tree but not in the new tree, delete them from the DOM. For nodes of the same type but with changed attributes or child nodes, update the corresponding nodes in the DOM.
[0041] Based on the comparison between the new tree and the old tree, a differential comparison result is generated, which describes the addition, deletion, update and move operations that need to be performed. Based on the differential comparison result, only the nodes that really need to be changed are modified without re-rendering the entire page or container.
[0042] In a second aspect, an embodiment of the present invention provides a device for implementing Table cascading scrolling based on DOM simulation, including:
[0043] A setting module is used to set a data structure to simulate the DOM structure of a table, wherein the data structure includes row, column and cell information, as well as position, size and style attributes of the row, column and cell, initialize a virtual DOM according to the data source of the table, map the data to the structure of the virtual DOM, and create a virtual node for each row, each column and each cell;
[0044] The calculation module calculates the current visible area according to the size and scroll position of the table container, selects the corresponding node from the virtual DOM according to the visible area, and renders it as an actual DOM element only when the node enters the visible area;
[0045] The update module sets a scroll event listener on the parent container of the table. When a scroll event is triggered, the scroll offset is calculated, and the offset is used to update the visible area of the virtual DOM. For the table parts that need to be scrolled synchronously, their new positions are calculated according to the scroll offset, and the corresponding virtual DOM nodes are updated;
[0046] The execution module uses a differential algorithm to compare the virtual DOM before and after the update, determines the nodes that need to be added, deleted or updated, and then generates a differential comparison result. According to the differential comparison result, the minimum necessary DOM operation is performed to update the user interface.
[0047] In a third aspect, an embodiment of the present invention further provides an electronic device, the electronic device comprising:
[0048] at least one processor; and,
[0049] a memory communicatively connected to the at least one processor; wherein,
[0050] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the method for implementing Table cascading scrolling based on DOM simulation in any of the first aspects or any implementation of the first aspect.
[0051] In a fourth aspect, an embodiment of the present invention further provides a non-transitory computer-readable storage medium, which stores computer instructions, and the computer instructions are used to enable the computer to execute the method of implementing Table cascading scrolling based on DOM simulation in the aforementioned first aspect or any implementation of the first aspect.
[0052] In the fifth aspect, an embodiment of the present invention further provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer executes the method of implementing Table cascading scrolling based on DOM simulation in the aforementioned first aspect or any implementation of the first aspect.
[0053] The solution for implementing cascading scrolling of Table based on DOM simulation in an embodiment of the present invention includes: setting a data structure to simulate the DOM structure of a table, the data structure includes row, column and cell information, as well as the position, size and style attributes of the row, column and cell, initializing a virtual DOM according to the data source of the table, mapping the data to the structure of the virtual DOM, and creating a virtual node for each row, each column and each cell; calculating the current visible area according to the size and scrolling position of the table container, selecting a corresponding node from the virtual DOM according to the visible area, and rendering it as an actual DOM element only when the node enters the visible area; setting a listener for the scrolling event on the parent container of the table, calculating the scrolling offset when the scrolling event is triggered, and the offset will be used to update the visible area of the virtual DOM, and for the table parts that need to be scrolled synchronously, calculating their new positions according to the scrolling offset, and updating the corresponding virtual DOM nodes; using a differential algorithm to compare the virtual DOM before and after the update, determining the nodes that need to be added, deleted or updated, and then generating a differential comparison result, and performing the minimum necessary DOM operation to update the user interface according to the differential comparison result. This solution can significantly improve the rendering and scrolling performance of large data tables, simplify the implementation complexity, and ensure cross-browser compatibility. BRIEF DESCRIPTION OF THE DRAWINGS
[0054] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0055] Figure 1 A flow chart of a method for implementing Table cascading scrolling based on DOM simulation provided by an embodiment of the present invention;
[0056] Figure 2 A schematic flow chart of another method for implementing Table cascading scrolling based on DOM simulation provided by an embodiment of the present invention;
[0057] Figure 3 A schematic diagram of the structure of a device for implementing Table cascade scrolling based on DOM simulation provided by an embodiment of the present invention;
[0058] Figure 4 A schematic diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0059] The embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0060] The following describes the embodiments of the present disclosure through specific examples, and those skilled in the art can easily understand other advantages and effects of the present disclosure from the contents disclosed in this specification. Obviously, the described embodiments are only a part of the embodiments of the present disclosure, rather than all of the embodiments. The present disclosure can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present disclosure. It should be noted that the following embodiments and features in the embodiments can be combined with each other without conflict. Based on the embodiments in the present disclosure, all other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of the present disclosure.
[0061] It should be noted that various aspects of the embodiments within the scope of the appended claims are described below. It should be apparent that the aspects described herein may be embodied in a wide variety of forms, and any specific structure and / or function described herein is merely illustrative. Based on the present disclosure, it should be understood by those skilled in the art that an aspect described herein may be implemented independently of any other aspect, and two or more of these aspects may be combined in various ways. For example, any number of aspects described herein may be used to implement the device and / or practice the method. In addition, other structures and / or functionalities other than one or more of the aspects described herein may be used to implement this device and / or practice this method.
[0062] It should also be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present disclosure. The drawings only show components related to the present disclosure rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed arbitrarily, and the component layout may also be more complicated.
[0063] Additionally, in the following description, specific details are provided to facilitate a thorough understanding of the examples. However, it will be understood by those skilled in the art that the aspects described may be practiced without these specific details.
[0064] The embodiment of the present disclosure provides a method for implementing table cascading scrolling based on DOM simulation. The method for implementing table cascading scrolling based on DOM simulation provided in this embodiment can be executed by a computing device, which can be implemented as software, or as a combination of software and hardware, and the computing device can be integrated in a server, terminal device, etc.
[0065] See also Figure 1 and Figure 2 The embodiment of the present disclosure provides a method for implementing Table cascading scrolling based on DOM simulation, including:
[0066] S101, setting a data structure to simulate the DOM structure of a table, wherein the data structure includes row, column and cell information, as well as position, size and style attributes of the row, column and cell, initializing a virtual DOM according to the data source of the table, mapping the data to the structure of the virtual DOM, and creating a virtual node for each row, each column and each cell.
[0067] To simulate the DOM structure of a table, you can design a data structure that can contain information about rows, columns, and cells, as well as the position, size, and style attributes of these elements.
[0068] Data structure definition :
[0069] Table: Represents the data structure of the entire table, including rows, columns, and possible global style attributes.
[0070] Row: Represents a row in a table, containing a list of cells and the position, size, and style properties of the row.
[0071] Column: Represents a column in a table, including the position, size, and style attributes of the column. Note that although column information is important for layout, in the virtual DOM, rows are usually the main organizational unit.
[0072] Cell: Represents a cell, including the cell's content, position, size, and style attributes. Cells can be linked to row and column data structures for easy positioning and style calculations.
[0073] During the initialization of the virtual DOM, the data source of the table can be parsed. The data source can be a two-dimensional array, an object array, or other formats, depending on the specific needs and data sources of the table.
[0074] Create a virtual Table node based on the size and structure of the data source. This node will contain the necessary global style properties, traverse each row of data in the data source, create a virtual Row node for each row in the data source, and set its position, size, and style properties.
[0075] For each data item in the Row, create a virtual Cell node. This node will contain the cell's content, position, size, and style properties. If the data items are nested, you may need to recursively create child Cell nodes. Add the created Cell node to the corresponding Row node, and add the Row node to the Table node.
[0076] When the data source changes, the virtual DOM needs to be updated to reflect those changes. Depending on the content of the changes, the updates to the virtual DOM are minimized. For example, if only the content of a cell has changed, then only that specific Cell node needs to be updated.
[0077] In this way, the data structure can be used to simulate the DOM structure of the table, and the virtual DOM can be initialized according to the data source. Each row, column, and cell has a corresponding virtual node in the virtual DOM, making it easy to manage and update the display of the table.
[0078] S102, calculating the current visible area according to the size and scrolling position of the table container, selecting a corresponding node from the virtual DOM according to the visible area, and rendering it as an actual DOM element only when the node enters the visible area.
[0079] First, you need to get the size of the table container, which usually includes the width and height of the container. Then, you need to determine the scroll position of the container, that is, the current position of the scroll bar. This can be achieved by querying the scroll offset of the container. Combining the size of the container and the scroll position, the current visible area can be calculated. The visible area is usually a rectangular area whose size and position are determined by the size and scroll position of the container.
[0080] Traverse the row or cell nodes in the virtual DOM, and for each node, calculate its actual position in the container based on its position information in the virtual DOM (such as row number, column number, etc.). Compare the node's position with the visible area to determine whether the node is in the current visible area.
[0081] For nodes that enter the visible area, create or update the corresponding actual DOM elements, and apply the style, content and other attributes of the virtual nodes to the actual DOM elements. For nodes that leave the visible area, their corresponding actual DOM elements can be recycled or hidden to save resources.
[0082] In this way, the visible area can be dynamically calculated according to the size and scroll position of the table container, and the corresponding nodes can be selected from the virtual DOM for rendering. This helps reduce unnecessary DOM operations and improve the rendering performance of the page.
[0083] S103, setting a scroll event listener on the parent container of the table, and when the scroll event is triggered, calculating the scroll offset, which will be used to update the visible area of the virtual DOM, and for the table parts that need to be scrolled synchronously, calculating their new positions according to the scroll offset, and updating the corresponding virtual DOM nodes.
[0084] Setting a scroll event listener on the table's parent container and handling the scroll event accordingly is a key step in achieving dynamic rendering and scroll synchronization. The following is a high-level description of the process without including specific code implementation.
[0085] Set up a scroll event listener:
[0086] Determine the parent container: First, you need to determine the parent container element of the table, which is the object to which the scroll event listener will be attached.
[0087] Add listener: Add a scroll event listener to the parent container. When the parent container scrolls, the listener will be triggered.
[0088] Calculate the scroll offset:
[0089] Get scroll offset: When a scroll event is triggered, the listener will get the current scroll offset. This offset indicates how much the parent container has scrolled relative to its initial position.
[0090] Handling scroll direction: Based on the scroll offset, you can tell whether the scrolling is happening in the vertical direction, horizontal direction, or both.
[0091] Update the visible area of the virtual DOM:
[0092] Recalculate the visible area: Recalculate the current visible area using the scroll offset and the size of the parent container. The visible area is the currently visible part of the parent container.
[0093] Update virtual DOM nodes: Based on the new visible area, update which nodes in the virtual DOM should be rendered as actual DOM elements. This usually involves determining which rows or cells are now in the visible area and marking them as "visible".
[0094] The table part that scrolls synchronously:
[0095] Determine the synchronization parts: If there are other table parts that need to scroll synchronously with the parent container, you need to determine these parts first.
[0096] Calculate new positions: Calculate the new positions of these synchronized scrolling parts based on the scroll offset of the parent container. This usually means that they also need to scroll according to the same offset.
[0097] Update virtual DOM nodes: Update the nodes in the virtual DOM corresponding to the synchronized scrolled parts to reflect their new positions.
[0098] Note:
[0099] Performance considerations: Scroll events may be triggered frequently, so when calculating offsets, updating visible areas, and synchronous scrolling, pay attention to performance optimization to avoid unnecessary calculations and rendering.
[0100] Boundary handling: When scrolling to the edge of the parent container, the boundary situation needs to be handled correctly to ensure that scroll overflow or rendering errors do not occur.
[0101] Scrolling smoothness: If necessary, you can achieve a smooth scrolling effect, that is, when scrolling to a new position, it does not jump immediately, but transitions smoothly.
[0102] In this way, the visible area of the virtual DOM can be dynamically updated according to the scroll event of the parent container, and other table parts that need to be synchronized can be scrolled synchronously. This helps to achieve an efficient scrolling experience and optimized rendering performance.
[0103] S104, using a differential algorithm to compare the virtual DOM before and after the update, determine the nodes that need to be added, deleted or updated, and then generate a differential comparison result. According to the differential comparison result, perform the minimum necessary DOM operation to update the user interface.
[0104] The virtual DOM tree before the update (called the "old tree") is structurally compared with the virtual DOM tree after the update (called the "new tree"). This comparison is recursive, starting from the root node and gradually deepening to the child nodes.
[0105] For each pair of nodes being compared, first check whether they are of the same type. If the node types are different (for example, one is an element node and the other is a text node), then that usually means that the old node needs to be deleted and the new node added.
[0106] If the nodes are of the same type, then compare the node attributes. Check which attributes have been added, deleted, or modified, and record the changes.
[0107] For element nodes, their child nodes also need to be compared. Two strategies are usually used here: one is key-based comparison, and the other is order-based comparison. Key-based comparison is suitable for situations where child nodes can be reordered, while order-based comparison assumes that the order of child nodes is fixed.
[0108] For nodes that exist in the new tree but not in the old tree, they need to be added to the DOM. Deleting nodes: For nodes that exist in the old tree but not in the new tree, they need to be deleted from the DOM. For nodes of the same type but with changed attributes or child nodes, the corresponding node in the DOM needs to be updated. This may involve modifying the attributes of the node or replacing its child nodes. If key-based comparison is used and the node's key is in a different position in the old and new trees, the node in the DOM needs to be moved to match the order of the new tree.
[0109] Based on the above comparison process, a difference comparison result is generated, which describes the add, delete, update, and move operations that need to be performed. Based on the difference comparison result, the minimum necessary DOM operation is performed to update the user interface. This usually means modifying only those nodes that really need to be changed, rather than re-rendering the entire page or container.
[0110] During the virtual DOM differential calculation process, two pointers can be set, one pointing to the root node of the old tree (old virtual DOM), and the other pointing to the root node of the new tree (new virtual DOM).
[0111] Compare the types of the two nodes pointed to by the current pointer. If the types are different, mark the old node as to be deleted and the new node as to be added, and move the new tree pointer to the next node. If the types are the same, proceed to the next step of attribute comparison.
[0112] Traverse the attribute list of the new node. For each attribute, check whether the old node also has the attribute. If the old node does not have the attribute, mark the attribute as to be added. If the old node has the attribute but the value is different, mark the attribute as to be updated. If the old node has the attribute and the value is the same, do nothing.
[0113] If the current node is an element node, compare its child nodes. You can choose an order-based or key-based comparison strategy. For each child node, recursively perform the above node type comparison, attribute comparison, and child node comparison. Record the child nodes that need to be added, deleted, moved, or updated.
[0114] After completing the comparison of the current node, collect all nodes and attributes marked as to be added, deleted, updated, or moved. These differences will constitute the final differential comparison results.
[0115] Based on the difference comparison results, perform necessary DOM operations to update the user interface. This may include adding new nodes, deleting old nodes, updating node properties, or moving nodes.
[0116] The present invention solves the defects of the prior art through customized virtual scrolling technology and responsive design. The solution can significantly improve the rendering and scrolling performance of large data tables, simplify the implementation complexity, and ensure cross-browser compatibility.
[0117] According to a specific implementation of the embodiment of the present disclosure, after performing the minimum necessary DOM operation to update the user interface according to the differential comparison result, the method further includes:
[0118] Use throttling and debouncing techniques to optimize the execution of event handling functions;
[0119] Regularly clean up virtual DOM nodes and related resources that are no longer needed;
[0120] Implement performance monitoring to identify and resolve bottlenecks in the rendering process and ensure smooth scrolling operations.
[0121] According to a specific implementation of the embodiment of the present disclosure, the step of setting a data structure to simulate a DOM structure of a table includes:
[0122] Define Table to represent the data structure of the entire table, including rows, columns, and global style attributes;
[0123] Define Row to represent a row in a table, containing a list of cells and the position, size, and style attributes of the row;
[0124] Define Column to represent a column in a table, including the position, size, and style attributes of the column;
[0125] Definition Cell represents a cell, including the cell's content, position, size, and style attributes. The cell is linked to the row and column data structures for positioning and style calculations.
[0126] According to a specific implementation of the embodiment of the present disclosure, the step of setting a data structure to simulate a DOM structure of a table includes:
[0127] Create a virtual Table node based on the size and structure of the data source. The Table node contains global style attributes.
[0128] For each row in the data source, create a virtual Row node and set its position, size, and style properties;
[0129] For each data item in Row, create a virtual Cell node, which contains the cell content, position, size, and style attributes. When the data item is a nested structure, create child Cell nodes recursively.
[0130] Add the created Cell node to the corresponding Row node, and add the Row node to the Table node.
[0131] According to a specific implementation of the embodiment of the present disclosure, the current visible area is calculated according to the size and scrolling position of the table container, and the corresponding node is selected from the virtual DOM according to the visible area, including:
[0132] It is necessary to obtain the size of the table container, which includes the width and height of the container. By querying the scroll offset of the container, the scroll position of the container is determined, and the current visible area is calculated by combining the size and scroll position of the container.
[0133] Traverse the row or cell nodes in the virtual DOM. For each node, calculate its actual position in the container based on its position information in the virtual DOM, compare the node's position with the visible area, and determine whether the node is in the current visible area.
[0134] For nodes that enter the visible area, create or update the corresponding actual DOM elements, apply the style and content of the virtual node to the actual DOM elements, and for nodes that leave the visible area, recycle or hide the corresponding actual DOM elements.
[0135] According to a specific implementation of the embodiment of the present disclosure, a scroll event listener is set on the parent container of the table. When the scroll event is triggered, the scroll offset is calculated, and the offset is used to update the visible area of the virtual DOM. For the table parts that need to be scrolled synchronously, their new positions are calculated according to the scroll offset, and the corresponding virtual DOM nodes are updated, including:
[0136] To determine the parent container element of the table, the parent container element is the object to which the scroll event listener is attached, add a scroll event listener to the parent container, and the listener is triggered when the parent container scrolls;
[0137] When a scroll event is triggered, the listener obtains the current scroll offset and calculates the scroll amount in the vertical and horizontal directions based on the scroll offset.
[0138] According to a specific implementation of the embodiment of the present disclosure, a scroll event listener is set on the parent container of the table. When the scroll event is triggered, the scroll offset is calculated, and the offset is used to update the visible area of the virtual DOM. For the table parts that need to be scrolled synchronously, their new positions are calculated according to the scroll offset, and the corresponding virtual DOM nodes are updated, including:
[0139] Recalculate the current visible area using the scroll offset and the size of the parent container. The visible area is the currently visible part of the parent container.
[0140] Based on the new visible area, update which nodes in the virtual DOM should be rendered as actual DOM elements;
[0141] According to the scroll offset of the parent container, the new position of the synchronous scrolling part is calculated, and the node corresponding to the synchronous scrolling part in the virtual DOM is updated to reflect the new position of the node corresponding to the synchronous scrolling part in the virtual DOM.
[0142] According to a specific implementation of an embodiment of the present disclosure, the virtual DOM before and after the update is compared using a differential algorithm to determine the nodes that need to be added, deleted or updated, and then generate a differential comparison result. According to the differential comparison result, the minimum necessary DOM operation is performed to update the user interface, including:
[0143] Compare the virtual DOM tree before and after the update structurally. For each pair of compared nodes, check whether their types are the same. If the node types are different, delete the old node and add the new node. If the node types are the same, compare the properties of the nodes to check which properties are added, deleted, or modified.
[0144] For nodes that exist in the new tree but not in the old tree, add them to the DOM. For nodes that exist in the old tree but not in the new tree, delete them from the DOM. For nodes of the same type but with changed attributes or child nodes, update the corresponding nodes in the DOM.
[0145] Based on the comparison between the new tree and the old tree, a differential comparison result is generated, which describes the addition, deletion, update and move operations that need to be performed. Based on the differential comparison result, only the nodes that really need to be changed are modified without re-rendering the entire page or container.
[0146] Corresponding to the above method embodiment, see Figure 3 The embodiment of the present invention further discloses a device 30 for implementing Table cascade scrolling based on DOM simulation, comprising:
[0147] Setting module 301, setting a data structure to simulate the DOM structure of a table, the data structure including row, column and cell information, as well as position, size and style attributes of the row, column and cell, initializing a virtual DOM according to the data source of the table, mapping the data to the structure of the virtual DOM, and creating a virtual node for each row, each column and each cell;
[0148] The calculation module 302 calculates the current visible area according to the size and scroll position of the table container, selects the corresponding node from the virtual DOM according to the visible area, and renders it as an actual DOM element only when the node enters the visible area;
[0149] The updating module 303 sets a scroll event listener on the parent container of the table. When the scroll event is triggered, the scroll offset is calculated, and the offset is used to update the visible area of the virtual DOM. For the table parts that need to be scrolled synchronously, their new positions are calculated according to the scroll offset, and the corresponding virtual DOM nodes are updated;
[0150] The execution module 304 uses a differential algorithm to compare the virtual DOM before and after the update, determines the nodes that need to be added, deleted or updated, and then generates a differential comparison result. According to the differential comparison result, the minimum necessary DOM operation is performed to update the user interface.
[0151] See also Figure 4 The embodiment of the present invention further provides an electronic device 60, the electronic device comprising:
[0152] at least one processor; and,
[0153] a memory communicatively connected to the at least one processor; wherein,
[0154] The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method for implementing Table cascading scrolling based on DOM simulation in the aforementioned method embodiment.
[0155] An embodiment of the present invention further provides a non-transitory computer-readable storage medium, which stores computer instructions, and the computer instructions are used to enable the computer to execute the aforementioned method embodiment.
[0156] An embodiment of the present invention also provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium. The computer program includes program instructions. When the program instructions are executed by a computer, the computer executes the method of implementing Table cascading scrolling based on DOM simulation in the aforementioned method embodiment.
[0157] Reference below Figure 4 , which shows a schematic diagram of the structure of an electronic device 60 suitable for implementing the embodiment of the present disclosure. The electronic device in the embodiment of the present disclosure may include, but is not limited to, mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle terminals (such as vehicle navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 4 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.
[0158] like Figure 4 As shown, the electronic device 60 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the electronic device 60 are also stored. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0159] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, an image sensor, a microphone, an accelerometer, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 608 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 609. The communication device 609 may allow the electronic device 60 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 4 The electronic device 60 is shown with various devices, but it should be understood that it is not required to implement or have all the devices shown. More or fewer devices may be implemented or have instead.
[0160] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program contains program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device 609, or installed from a storage device 608, or installed from a ROM 602. When the computer program is executed by the processing device 601, the above-mentioned functions defined in the method of the embodiment of the present disclosure are executed.
[0161] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions that can be easily thought of by a person skilled in the art within the technical scope disclosed by the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.
Claims
1. A method for implementing Table cascading scrolling based on DOM simulation, characterized in that: include: Setting a data structure to simulate the DOM structure of a table, wherein the data structure includes row, column, and cell information, as well as position, size, and style attributes of the row, column, and cell, initializing a virtual DOM according to a data source of the table, mapping the data to the structure of the virtual DOM, and creating a virtual node for each row, each column, and each cell; According to the size and scroll position of the table container, the current visible area is calculated. According to the visible area, the corresponding node is selected from the virtual DOM. Only when the node enters the visible area, it is rendered as an actual DOM element. Set a scroll event listener on the parent container of the table. When a scroll event is triggered, calculate the scroll offset, which will be used to update the visible area of the virtual DOM. For the table parts that need to be scrolled synchronously, calculate their new positions according to the scroll offset and update the corresponding virtual DOM nodes. Use a differential algorithm to compare the virtual DOM before and after the update to determine the nodes that need to be added, deleted, or updated, and then generate a differential comparison result. Based on the differential comparison result, perform the minimum necessary DOM operations to update the user interface; The method of calculating the current visible area according to the size and scrolling position of the table container and selecting a corresponding node from the virtual DOM according to the visible area includes: It is necessary to obtain the size of the table container, which includes the width and height of the container. By querying the scroll offset of the container, the scroll position of the container is determined, and the current visible area is calculated by combining the size and scroll position of the container. Traverse the row or cell nodes in the virtual DOM. For each node, calculate its actual position in the container based on its position information in the virtual DOM, compare the node's position with the visible area, and determine whether the node is in the current visible area. For nodes that enter the visible area, create or update the corresponding actual DOM elements, apply the style and content of the virtual nodes to the actual DOM elements, and for nodes that leave the visible area, recycle or hide the corresponding actual DOM elements; The scroll event listener is set on the parent container of the table. When the scroll event is triggered, the scroll offset is calculated. The offset will be used to update the visible area of the virtual DOM. For the table parts that need to be scrolled synchronously, their new positions are calculated according to the scroll offset, and the corresponding virtual DOM nodes are updated, including: To determine the parent container element of the table, the parent container element is the object to which the scroll event listener is attached, add a scroll event listener to the parent container, and the listener is triggered when the parent container scrolls; When a scroll event is triggered, the listener obtains the current scroll offset and calculates the scroll amount in the vertical and horizontal directions based on the scroll offset. Recalculate the current visible area using the scroll offset and the size of the parent container. The visible area is the currently visible part of the parent container. Based on the new visible area, update which nodes in the virtual DOM should be rendered as actual DOM elements; Calculate the new position of the synchronous scrolling part according to the scroll offset of the parent container, and update the node corresponding to the synchronous scrolling part in the virtual DOM to reflect the new position of the node corresponding to the synchronous scrolling part in the virtual DOM; The method uses a differential algorithm to compare the virtual DOM before and after the update, determines the nodes that need to be added, deleted or updated, and then generates a differential comparison result. According to the differential comparison result, the minimum necessary DOM operation is performed to update the user interface, including: Compare the virtual DOM tree before and after the update structurally. For each pair of compared nodes, check whether their types are the same. If the node types are different, delete the old node and add the new node. If the node types are the same, compare the properties of the nodes to check which properties are added, deleted, or modified. For nodes that exist in the new tree but not in the old tree, add them to the DOM. For nodes that exist in the old tree but not in the new tree, delete them from the DOM. For nodes of the same type but with changed attributes or child nodes, update the corresponding nodes in the DOM. Based on the comparison between the new tree and the old tree, a differential comparison result is generated, which describes the addition, deletion, update and move operations that need to be performed. Based on the differential comparison result, only the nodes that really need to be changed are modified without re-rendering the entire page or container.
2. The method according to claim 1, characterized in that After performing the minimum necessary DOM operation to update the user interface according to the differential comparison result, the method further includes: Use throttling and debouncing techniques to optimize the execution of event handling functions; Regularly clean up virtual DOM nodes and related resources that are no longer needed; Implement performance monitoring to identify and resolve bottlenecks in the rendering process and ensure smooth scrolling operations.
3. The method according to claim 2, characterized in that The data structure is set to simulate the DOM structure of the table, including: Define Table to represent the data structure of the entire table, including rows, columns, and global style attributes; Define Row to represent a row in a table, containing a list of cells and the position, size, and style attributes of the row; Define Column to represent a column in a table, including the position, size, and style attributes of the column; Definition Cell represents a cell, including the cell's content, position, size, and style attributes. The cell is linked to the row and column data structures for positioning and style calculations.
4. The method according to claim 3, characterized in that The data structure is set to simulate the DOM structure of the table, including: Create a virtual Table node based on the size and structure of the data source. The Table node contains global style attributes. For each row in the data source, create a virtual Row node and set its position, size, and style properties; For each data item in Row, create a virtual Cell node, which contains the cell content, position, size, and style attributes. When the data item is a nested structure, create child Cell nodes recursively. Add the created Cell node to the corresponding Row node, and add the Row node to the Table node.
5. A device for realizing Table cascade scrolling based on DOM simulation, characterized in that: include: A setting module is used to set a data structure to simulate the DOM structure of a table, wherein the data structure includes row, column and cell information, as well as position, size and style attributes of the row, column and cell, initialize a virtual DOM according to the data source of the table, map the data to the structure of the virtual DOM, and create a virtual node for each row, each column and each cell; The calculation module calculates the current visible area according to the size and scroll position of the table container, selects the corresponding node from the virtual DOM according to the visible area, and renders it as an actual DOM element only when the node enters the visible area; The update module sets a scroll event listener on the parent container of the table. When a scroll event is triggered, the scroll offset is calculated, and the offset is used to update the visible area of the virtual DOM. For the table parts that need to be scrolled synchronously, their new positions are calculated according to the scroll offset, and the corresponding virtual DOM nodes are updated; The execution module uses a differential algorithm to compare the virtual DOM before and after the update, determines the nodes that need to be added, deleted or updated, and then generates a differential comparison result. Based on the differential comparison result, it performs the minimum necessary DOM operation to update the user interface; The method of calculating the current visible area according to the size and scrolling position of the table container and selecting a corresponding node from the virtual DOM according to the visible area includes: It is necessary to obtain the size of the table container, which includes the width and height of the container. By querying the scroll offset of the container, the scroll position of the container is determined, and the current visible area is calculated by combining the size and scroll position of the container. Traverse the row or cell nodes in the virtual DOM. For each node, calculate its actual position in the container based on its position information in the virtual DOM, compare the node's position with the visible area, and determine whether the node is in the current visible area. For nodes that enter the visible area, create or update the corresponding actual DOM elements, apply the style and content of the virtual nodes to the actual DOM elements, and for nodes that leave the visible area, recycle or hide the corresponding actual DOM elements; The scroll event listener is set on the parent container of the table. When the scroll event is triggered, the scroll offset is calculated. The offset will be used to update the visible area of the virtual DOM. For the table parts that need to be scrolled synchronously, their new positions are calculated according to the scroll offset, and the corresponding virtual DOM nodes are updated, including: To determine the parent container element of the table, the parent container element is the object to which the scroll event listener is attached, add a scroll event listener to the parent container, and the listener is triggered when the parent container scrolls; When a scroll event is triggered, the listener obtains the current scroll offset and calculates the scroll amount in the vertical and horizontal directions based on the scroll offset. Recalculate the current visible area using the scroll offset and the size of the parent container. The visible area is the currently visible part of the parent container. Based on the new visible area, update which nodes in the virtual DOM should be rendered as actual DOM elements; Calculate the new position of the synchronous scrolling part according to the scroll offset of the parent container, and update the node corresponding to the synchronous scrolling part in the virtual DOM to reflect the new position of the node corresponding to the synchronous scrolling part in the virtual DOM; The method uses a differential algorithm to compare the virtual DOM before and after the update, determines the nodes that need to be added, deleted or updated, and then generates a differential comparison result. According to the differential comparison result, the minimum necessary DOM operation is performed to update the user interface, including: Compare the virtual DOM tree before and after the update structurally. For each pair of compared nodes, check whether their types are the same. If the node types are different, delete the old node and add the new node. If the node types are the same, compare the properties of the nodes to check which properties are added, deleted, or modified. For nodes that exist in the new tree but not in the old tree, add them to the DOM. For nodes that exist in the old tree but not in the new tree, delete them from the DOM. For nodes of the same type but with changed attributes or child nodes, update the corresponding nodes in the DOM. Based on the comparison between the new tree and the old tree, a differential comparison result is generated, which describes the addition, deletion, update and move operations that need to be performed. Based on the differential comparison result, only the nodes that really need to be changed are modified without re-rendering the entire page or container.
6. An electronic device, characterized in that: The electronic device comprises: at least one processor; and, a memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method for implementing Table cascading scrolling based on DOM simulation as described in any one of claims 1 to 4.
Citation Information
Patent Citations
Screen display method and device
CN106445284A
