Method and device for setting scroll bar position in table custom in React and electronic equipment
By using six Grid list components and the overflow-x:auto style attribute in the React table, the first column is fixed and its position is updated synchronously, which solves the problem of the scroll bar starting from the second column in the existing technology and achieves customization Effects on scrollbar position and visual consistency.
Patent Information
- Application Number
- CN202411258928.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-10
- Publication Date
- 2024-10-15
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
When the existing React table component has a horizontal scroll bar, the scroll bar starts from the second column, which cannot meet business needs. Especially when the first column is name and statistics, the subsequent fields require a horizontal scroll bar from the second column. Column starts.
Build the different parts of the table by using six Grid list components, setting the bottomRight component's first container with the overflow-x:auto style attribute, fixing the first column to the left, and placing the bottomRight component's first container with a fixed width next to it. The second container listens to scroll events and updates the position of the first column to maintain visual consistency.
A table with customized scroll bar position is implemented to meet business needs and improve the flexibility and display effect of the table.
Smart Images

Figure CN118778960A_ABST
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 customizing a scroll bar position of a table in React. Background Art
[0002] React Table refers to a table component used in React applications. React is a JavaScript library for building user interfaces, especially for building single-page applications (SPA). In React, a table is usually composed of multiple 、 、 、 、 and HTML elements are composed of components.
[0003] React table components can be reused in multiple places to reduce code duplication. The table can be encapsulated into a separate React component and different attributes can be passed to display different data. The state of the React component can be used to control the behavior of the table, such as sorting, filtering, etc. The data of the table can be stored in the state of the component, and the displayed content can be dynamically changed by updating the state.
[0004] Currently, all table applications in React use the antd Table component solution. Due to the DOM structure encapsulation problem of the antd Table component itself, it does not support the scroll bar starting from the second column when there is a horizontal scroll bar. When the first column is the name and statistics, the following fields are indicators, and the horizontal scroll bar needs to start from the second column. In this way, the displayed data effect is more in line with business needs, but the antd Table component solution cannot meet the needs.
[0005] The above problems become technical problems that need to be solved. Summary of the invention
[0006] In view of this, an embodiment of the present invention provides a method, device and electronic device for customizing the scroll bar position of a table in React, which at least partially solves the problems existing in the prior art.
[0007] In a first aspect, an embodiment of the present invention provides a method for customizing a scroll bar position of a table in React, comprising:
[0008] Receive data from the parent component through props, including the table content and title information, and split the received data into segmented data matching different Grid areas. The segmented data is used by the Grid list component;
[0009] Six Grid list components are used to construct different parts of the table. The six Grid list components are: topLeft, topRight, middleLeft, middleRight, bottomLeft and bottomRight. topLeft and topRight represent the top left and top right of the table respectively, middleLeft and middleRight represent the middle part of the table, and bottomLeft and bottomRight represent the bottom left and right parts respectively;
[0010] A first container is set for the bottomRight component, the first container has an overflow-x:auto style attribute, when the table content exceeds the width of the first container, a horizontal scroll bar is displayed in the table, the content of bottomRight is set starting from the second column, and the first column of the table is fixed to the left, so that when the table is scrolled horizontally, the first column does not scroll;
[0011] A second container with a fixed width is placed next to the first container in bottomRight. The second container is used to display the content of the first column. The second container is vertically aligned with the first container. The width of the second container matches the width of the first column in bottomRight.
[0012] By listening to scroll events and updating the position of the first column accordingly, when the scroll bar in the bottomRight container moves, the fixed first column moves synchronously to maintain visual consistency.
[0013] According to a specific implementation of the embodiment of the present disclosure, before receiving data from the parent component through props, the method further includes:
[0014] After the basic settings of the React project are completed, introduce the react-virtualized library and install and configure the corresponding npm package;
[0015] When creating a React component, use react-virtualized to provide WindowScroller, AutoSizer, ScrollSync, and Grid components to implement a table with custom scroll bar positions.
[0016] According to a specific implementation of the embodiment of the present disclosure, the method further includes:
[0017] Use React's useRef to get the Grid component dom, and use the gridforceUpdate() method to update the table;
[0018] Adjust the properties of the react-virtualized component to customize the scrolling behavior and rendering effects according to your specific needs.
[0019] According to a specific implementation of the embodiment of the present disclosure, the method further includes:
[0020] Design data loading logic, load data in advance by scrolling, and adjust the use and configuration of components when using react-virtualized.
[0021] According to a specific implementation of the embodiment of the present disclosure, the method further includes:
[0022] When the data from the parent component is empty, a prompt is displayed on the interface and the indicator name of the first row is retained.
[0023] According to a specific implementation of the embodiment of the present disclosure, by monitoring the scroll event and updating the position of the first column accordingly, when the scroll bar in the bottomRight container moves, the fixed first column is synchronously moved, including:
[0024] Add a scroll listener to the bottomRight container. When the scroll event is triggered, the listener function obtains the current scroll position of the bottomRight container.
[0025] Use the obtained scroll position to update the scroll positions of topRight and middleRight to ensure that they are consistent with the scroll position of bottomRight;
[0026] If the width of the first column does not exactly match the width of the first column in bottomRight, fine-tune the widths of topLeft, middleLeft, and bottomLeft to make sure they align with the first column in bottomRight.
[0027] According to a specific implementation of the embodiment of the present disclosure, by monitoring the scroll event and updating the position of the first column accordingly, when the scroll bar in the bottomRight container moves, the fixed first column is moved synchronously, and further includes:
[0028] Assume bottomRight is a scrollable container, leftColumn is the first column that scrolls synchronously with bottomRight, scrollPosition represents the current scroll position of bottomRight, scrollSpeed represents the unit speed of each scroll, and scrollDelta represents the change in the scroll position;
[0029] Initialize scrollPosition to 0. When the scroll event of bottomRight is triggered, record the current scroll position scrollPosition and calculate the scroll change scrollDelta:
[0030] scrollDelta=scrollPosition-previousScrollPosition
[0031] previousScrollPosition is the scroll position when the previous scroll event was triggered;
[0032] Update the scroll position of leftColumn leftColumscrollLeft+=scrollDelta.
[0033] According to a specific implementation of the embodiment of the present disclosure, by monitoring the scroll event and updating the position of the first column accordingly, when the scroll bar in the bottomRight container moves, the fixed first column is moved synchronously, and further includes:
[0034] Create a function called syncScrollPosition. The function receives the latest scroll position of the bottomRight container as a parameter and updates the scroll positions of other related containers.
[0035] Calculate the difference between the current scroll position of the bottomRight container and the last recorded scroll position, and use the calculated scroll offset to update the scroll positions of topRight, middleRight, topLeft, middleLeft, and bottomLeft;
[0036] When scrolling stops, cancel the call to requestAnimationFrame.
[0037] In a second aspect, the embodiments of the present invention provide
[0038] A device for customizing the scroll bar position of a table in React, comprising:
[0039] The receiving module receives data from the parent component through props, including the content and title information of the table, and divides the received data into segmented data matching different Grid areas. The segmented data is used by the Grid list component.
[0040] The construction module uses six Grid list components to construct different parts of the table. The six Grid list components are: topLeft, topRight, middleLeft, middleRight, bottomLeft and bottomRight. topLeft and topRight represent the top left and top right of the table respectively, middleLeft and middleRight represent the middle part of the table, and bottomLeft and bottomRight represent the bottom left and right parts respectively;
[0041] A setting module is used to set a first container for the bottomRight component. The first container has an overflow-x:auto style attribute. When the table content exceeds the width of the first container, a horizontal scroll bar is displayed in the table. The content of bottomRight is set starting from the second column, and the first column of the table is fixed to the left, so that when the table is scrolled horizontally, the first column does not scroll;
[0042] Place the module, place a second container with a fixed width next to the first container in bottomRight, the second container is used to display the content of the first column, the second container is vertically aligned with the first container, and the width of the second container matches the width of the first column in bottomRight;
[0043] Update the module to monitor scroll events and update the position of the first column accordingly. When the scroll bar in the bottomRight container moves, the fixed first column is moved synchronously to maintain visual consistency.
[0044] In a third aspect, an embodiment of the present invention further provides an electronic device, the electronic device comprising:
[0045] at least one processor; and,
[0046] a memory communicatively connected to the at least one processor; wherein,
[0047] 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 customizing the scroll bar position of a table in React in any of the aforementioned first aspects or any of the implementations of the first aspect.
[0048] 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 for customizing the scroll bar position of a table in React in the aforementioned first aspect or any implementation of the first aspect.
[0049] In a 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 for customizing the scroll bar position of a table in React in the aforementioned first aspect or any implementation of the first aspect.
[0050] The solution for customizing the scroll bar position of a table in React in an embodiment of the present invention includes: receiving data from a parent component through props, the data including the content and title information of the table, dividing the received data into divided data matching different Grid areas, the divided data being used by a Grid list component; using six Grid list components to construct different parts of the table, the six Grid list components being: topLeft, topRight, middleLeft, middleRight, bottomLeft and bottomRight, topLeft and topRight respectively represent the top left and top right of the table, middleLeft and middleRight represent the middle part of the table, bottomLeft and bottomRight respectively represent the left side of the bottom and the right part; set a first container for the bottomRight component, the first container has the overflow-x:auto style attribute, when the table content exceeds the width of the first container, display a horizontal scroll bar in the table, set the content of bottomRight from the second column, and fix the first column of the table to the left, so that when the table scrolls horizontally, the first column will not scroll; place a second container with a fixed width next to the first container of bottomRight, the second container is used to display the content of the first column, the second container is vertically aligned with the first container, and the width of the second container matches the width of the first column in bottomRight; by listening to the scroll event and updating the position of the first column accordingly, when the scroll bar in the bottomRight container moves, the fixed first column is moved synchronously to maintain visual consistency. Through the scheme of the present application, the flexibility of React table settings can be improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0051] 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.
[0052] Figure 1 A schematic flow chart of a method for customizing the scroll bar position of a table in React provided by an embodiment of the present invention;
[0053] Figure 2 A schematic flow chart of another method for customizing the scroll bar position of a table in React provided by an embodiment of the present invention;
[0054] Figure 3 A schematic diagram of the structure of a device for customizing the scroll bar position of a table in React provided by an embodiment of the present invention;
[0055] Figure 4 A schematic diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0056] The embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0057] 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.
[0058] 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.
[0059] 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.
[0060] 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.
[0061] The embodiment of the present disclosure provides a method for customizing the scroll bar position of a table in React. The method for customizing the scroll bar position of a table in React 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, a terminal device, etc.
[0062] See also Figure 1 , the embodiment of the present disclosure provides a method for customizing the scroll bar position of a table in React, including:
[0063] S101, receiving data from a parent component through props, the data including table content and title information, dividing the received data into divided data matching different Grid areas, the divided data being used by the Grid list component.
[0064] The parent component is responsible for preparing the table content and title information, and passing this data as props to the child component. The data structure can be nested, such as an object containing an array of title information and table data.
[0065] The child component receives data from the parent component through its props, verifies the integrity of the props data, and ensures that the data conforms to the expected structure. Inside the child component, the received data is processed according to the layout requirements of the Grid (for example, display in columns, regions, etc.). This may include filtering, sorting, mapping, or simply splitting the data so that the data can be assigned to different parts of the Grid.
[0066] S102, use six Grid list components to construct different parts of the table, and the six Grid list components are: topLeft, topRight, middleLeft, middleRight, bottomLeft and bottomRight, topLeft and topRight respectively represent the top left and top right of the table, middleLeft and middleRight represent the middle part of the table, bottomLeft and bottomRight respectively represent the left and right parts of the bottom.
[0067] As a specific example, the table is divided into a 3x2 grid, where each row contains two regions (left and right). Each region is responsible for rendering its content by a Grid list component.
[0068] topLeft: Located at the top left of the table, it is used to display the table title, description, or some summary information.
[0069] topRight: Located at the top right of the table, it can contrast or complement topLeft, such as displaying filter conditions, operation buttons, etc.
[0070] middleLeft and middleRight: These two areas occupy the middle part of the table and are the main display area of the table data. middleLeft may display column headers or specific parts of the data, while middleRight displays the corresponding data rows or detailed information.
[0071] bottomLeft and bottomRight: Located at the bottom of the table, these areas can be used to display additional information, such as paging controls, totals, notes, etc. bottomLeft and bottomRight can be flexibly arranged according to actual needs.
[0072] The parent component is responsible for preparing all the data to be displayed and passing it as props to the child components of the six Grid list components. Each Grid list component renders its corresponding content based on the received data. Components can communicate with each other through the state management of the parent component or React's Context API to achieve interactive operations such as data update and state synchronization.
[0073] S103, set a first container for the bottomRight component, the first container has an overflow-x:auto style attribute, when the table content exceeds the width of the first container, display a horizontal scroll bar in the table, set the content of bottomRight starting from the second column, and fix the first column of the table to the left, so that when the table is scrolled horizontally, the first column will not scroll.
[0074] Inside the bottomRight component, first create a container element (such as a div) and use this div as the first container. Set the overflow-x:auto; style attribute to this first container so that when the content exceeds its width, a horizontal scroll bar will appear.
[0075] Inside the first container, build a table ( ). Make sure that each row of the table ( ) contain enough cells ( or ) to match the data columns.
[0076] When you fix the first column, create an additional table or div structure to simulate the effect of a fixed column. For example, you can put the first column in a separate container that displays side by side with the rest of the table but is not affected by scroll bars.
[0077] S104, placing a second container with a fixed width next to the first container in bottomRight, the second container is used to display the content of the first column, the second container is vertically aligned with the first container, and the width of the second container matches the width of the first column in bottomRight.
[0078] The bottomRight component contains two main containers: the first container and the second container.
[0079] First container: used to display the table contents except the first column, and set overflow-x:auto; to support horizontal scrolling.
[0080] The second container has a fixed width and is placed next to the first container. It is used to display the first column of the table.
[0081] The height of the second container is aligned vertically with the content area of the first container, ensuring that no matter how the content in the first container scrolls, the second container remains in sync with it. The two containers have a common parent element or use CSS layout techniques (such as Flexbox or Grid) to ensure their alignment.
[0082] When the content in the first container scrolls horizontally, the second container remains fixed and does not scroll with it. Since it is fixed width and placed next to the first container, it will not be affected by the scroll bar of the first container.
[0083] S105, by monitoring the scroll event and updating the position of the first column accordingly, when the scroll bar in the bottomRight container moves, the fixed first column is moved synchronously to maintain visual consistency.
[0084] Add a scroll event listener to the bottomRight container (the container containing the scroll bar). When the user scrolls this container, the scroll event will be triggered.
[0085] In the scroll event processing function, get the scroll offset of the current container (usually the horizontal offset). This offset indicates how far the user has scrolled, which is the key information for synchronously moving the fixed first column.
[0086] Use the calculated scroll offset to update the position of the fixed first column. The translateX value in the fixed column's CSS transform property can be set to the inverse of the scroll offset (or may need to be adjusted depending on the layout direction).
[0087] In this way, when the scrollbar in the bottomRight container moves, the fixed column will move accordingly, thus maintaining visual consistency with the scrolling content.
[0088] See also Figure 2 , the method of the present invention further comprises the steps of:
[0089] Data loading: Receive data passed by the upper-level component through props.
[0090] Data processing: Process the data so that it can be used by the list component. Pass the processed data to the next step.
[0091] Data rendering: Data rendering is divided into two cases: with data and without data. No data case: When there is no data available, render a "no data" module. With data case: Use the react-virtualized library to implement a table with a custom scroll bar to meet business needs.
[0092] Use 6 Grid list components to create the DOM structure of a custom table, with the structure of topLeft, topRight, middleLeft, middleRight, bottomLeft, and bottomRight. By setting a horizontal scroll bar on the bottomRight Grid list component, you can implement a horizontal scroll bar starting from the second column. Encapsulate the table component created by the 6 Grid list components into a custom table component, and call it where needed to meet business needs.
[0093] Call the encapsulated custom table component: call our custom table component by setting the number of fixed columns on the left, the width of the column, the total number of columns, the rendering data required for the table, the number of rows, the height of the row and other parameters. A successful call can meet business needs.
[0094] According to a specific implementation of the embodiment of the present disclosure, before receiving data from the parent component through props, the method further includes:
[0095] After the basic settings of the React project are completed, introduce the react-virtualized library and install and configure the corresponding npm package;
[0096] When creating a React component, use react-virtualized to provide WindowScroller, AutoSizer, ScrollSync, and Grid components to implement a table with custom scroll bar positions.
[0097] According to a specific implementation of the embodiment of the present disclosure, the method further includes:
[0098] Use React's useRef to get the Grid component dom, and use the gridforceUpdate() method to update the table;
[0099] Adjust the properties of the react-virtualized component to customize the scrolling behavior and rendering effects according to your specific needs.
[0100] According to a specific implementation of the embodiment of the present disclosure, the method further includes:
[0101] Design data loading logic, load data in advance by scrolling, and adjust the use and configuration of components when using react-virtualized.
[0102] According to a specific implementation of the embodiment of the present disclosure, the method further includes:
[0103] When the data from the parent component is empty, a prompt is displayed on the interface and the indicator name of the first row is retained.
[0104] According to a specific implementation of the embodiment of the present disclosure, by monitoring the scroll event and updating the position of the first column accordingly, when the scroll bar in the bottomRight container moves, the fixed first column is synchronously moved, including:
[0105] Add a scroll listener to the bottomRight container. When the scroll event is triggered, the listener function gets the current scroll position of the bottomRight container.
[0106] Use the obtained scroll position to update the scroll positions of topRight and middleRight to ensure that they are consistent with the scroll position of bottomRight;
[0107] If the width of the first column does not exactly match the width of the first column in bottomRight, fine-tune the widths of topLeft, middleLeft, and bottomLeft to make sure they align with the first column in bottomRight.
[0108] According to a specific implementation of the embodiment of the present disclosure, by monitoring the scroll event and updating the position of the first column accordingly, when the scroll bar in the bottomRight container moves, the fixed first column is moved synchronously, and further includes:
[0109] Assume bottomRight is a scrollable container, leftColumn is the first column that scrolls synchronously with bottomRight, scrollPosition represents the current scroll position of bottomRight, scrollSpeed represents the unit speed of each scroll, and scrollDelta represents the change in the scroll position;
[0110] Initialize scrollPosition to 0. When the scroll event of bottomRight is triggered, record the current scroll position scrollPosition and calculate the scroll change scrollDelta:
[0111] scrollDelta=scrollPosition-previousScrollPosition
[0112] previousScrollPosition is the scroll position when the previous scroll event was triggered;
[0113] Update the scroll position of leftColumn leftColumscrollLeft+=scrollDelta.
[0114] According to a specific implementation of the embodiment of the present disclosure, by monitoring the scroll event and updating the position of the first column accordingly, when the scroll bar in the bottomRight container moves, the fixed first column is moved synchronously, and further includes:
[0115] Create a function called syncScrollPosition. The function receives the latest scroll position of the bottomRight container as a parameter and updates the scroll positions of other related containers.
[0116] Calculate the difference between the current scroll position of the bottomRight container and the last recorded scroll position, and use the calculated scroll offset to update the scroll positions of topRight, middleRight, topLeft, middleLeft, and bottomLeft;
[0117] When scrolling stops, cancel the call to requestAnimationFrame.
[0118] Corresponding to the above method embodiment, see Figure 3 The embodiment of the present invention further discloses a device 30 for customizing the scroll bar position of a table in React, comprising:
[0119] The receiving module 301 receives data from the parent component through props, the data including the content and title information of the table, and divides the received data into divided data matching different Grid areas, the divided data is used by the Grid list component;
[0120] A construction module 302 uses six Grid list components to construct different parts of the table. The six Grid list components are: topLeft, topRight, middleLeft, middleRight, bottomLeft and bottomRight. topLeft and topRight represent the top left and top right of the table, middleLeft and middleRight represent the middle part of the table, and bottomLeft and bottomRight represent the bottom left and right parts, respectively.
[0121] The setting module 303 sets a first container for the bottomRight component, wherein the first container has an overflow-x:auto style attribute. When the table content exceeds the width of the first container, a horizontal scroll bar is displayed in the table, and the content of bottomRight is set starting from the second column, and the first column of the table is fixed to the left, so that when the table is scrolled horizontally, the first column does not scroll;
[0122] A placement module 304 is provided to place a second container with a fixed width next to the first container in bottomRight, wherein the second container is used to display the content of the first column, the second container is vertically aligned with the first container, and the width of the second container matches the width of the first column in bottomRight;
[0123] The updating module 305 monitors the scrolling event and updates the position of the first column accordingly. When the scroll bar in the bottomRight container moves, the fixed first column is moved synchronously to maintain visual consistency.
[0124] See also Figure 4 The embodiment of the present invention further provides an electronic device 60, the electronic device comprising:
[0125] at least one processor; and,
[0126] a memory communicatively connected to the at least one processor; wherein,
[0127] 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 customizing the scroll bar position of a table in React in the aforementioned method embodiment.
[0128] 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.
[0129] 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 for customizing the scroll bar position of a table in React in the aforementioned method embodiment.
[0130] 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.
[0131] 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.
[0132] 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, a gage, 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.
[0133] 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 includes a 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.
[0134] 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 customizing the scroll bar position of a table in React, characterized in that: include: Receive data from the parent component through props, including the table content and title information, and split the received data into segmented data matching different Grid areas. The segmented data is used by the Grid list component; Six Grid list components are used to construct different parts of the table. The six Grid list components are: topLeft, topRight, middleLeft, middleRight, bottomLeft and bottomRight. topLeft and topRight represent the top left and top right of the table respectively, middleLeft and middleRight represent the middle part of the table, and bottomLeft and bottomRight represent the bottom left and right parts respectively; A first container is set for the bottomRight component, the first container has an overflow-x:auto style attribute, when the table content exceeds the width of the first container, a horizontal scroll bar is displayed in the table, the content of bottomRight is set starting from the second column, and the first column of the table is fixed to the left, so that when the table is scrolled horizontally, the first column does not scroll; A second container with a fixed width is placed next to the first container in bottomRight. The second container is used to display the content of the first column. The second container is vertically aligned with the first container. The width of the second container matches the width of the first column in bottomRight. By listening to scroll events and updating the position of the first column accordingly, when the scroll bar in the bottomRight container moves, the fixed first column moves synchronously to maintain visual consistency.
2. The method according to claim 1, characterized in that Before receiving data from the parent component through props, the method further includes: After the basic settings of the React project are completed, introduce the react-virtualized library and install and configure the corresponding npm package; When creating a React component, use react-virtualized to provide WindowScroller, AutoSizer, ScrollSync, and Grid components to implement a table with custom scroll bar positions.
3. The method according to claim 2, characterized in that The method further comprises: Use React's useRef to get the Grid component dom, and use the grid.forceUpdate() method to update the table; Adjust the properties of the react-virtualized component to customize the scrolling behavior and rendering effects according to your specific needs.
4. The method according to claim 3, characterized in that The method further comprises: Design data loading logic, load data in advance by scrolling, and adjust the use and configuration of components when using react-virtualized.
5. The method according to claim 4, characterized in that The method further comprises: When the data from the parent component is empty, a prompt is displayed on the interface and the indicator name of the first row is retained.
6. The method according to claim 5, characterized in that By monitoring the scroll event and updating the position of the first column accordingly, when the scroll bar in the bottomRight container moves, the fixed first column is moved synchronously, including: Add a scroll listener to the bottomRight container. When the scroll event is triggered, the listener function gets the current scroll position of the bottomRight container. Use the obtained scroll position to update the scroll positions of topRight and middleRight to ensure that they are consistent with the scroll position of bottomRight; If the width of the first column does not exactly match the width of the first column in bottomRight, fine-tune the widths of topLeft, middleLeft, and bottomLeft to make sure they align with the first column in bottomRight.
7. The method according to claim 6, characterized in that The method monitors the scroll event and updates the position of the first column accordingly, and when the scroll bar in the bottomRight container moves, the fixed first column is moved synchronously, and further includes: Assume bottomRight is a scrollable container, leftColumn is the first column that scrolls synchronously with bottomRight, scrollPosition represents the current scroll position of bottomRight, scrollSpeed represents the unit speed of each scroll, and scrollDelta represents the change in the scroll position; Initialize scrollPosition to 0. When the scroll event of bottomRight is triggered, record the current scroll position scrollPosition and calculate the scroll change scrollDelta: scrollDelta=scrollPosition-previousScrollPosition previousScrollPosition is the scroll position when the previous scroll event was triggered; Update the scroll position of leftColumn leftColumn.scrollLeft+=scrollDelta.
8. The method according to claim 7, characterized in that The method monitors the scroll event and updates the position of the first column accordingly, and when the scroll bar in the bottomRight container moves, the fixed first column is moved synchronously, and further includes: Create a function called syncScrollPosition. The function receives the latest scroll position of the bottomRight container as a parameter and updates the scroll positions of other related containers. Calculate the difference between the current scroll position of the bottomRight container and the last recorded scroll position, and use the calculated scroll offset to update the scroll positions of topRight, middleRight, topLeft, middleLeft, and bottomLeft; When scrolling stops, cancel the call to requestAnimationFrame.
9. A device for customizing the scroll bar position of a table in React, characterized in that: include: The receiving module receives data from the parent component through props, including the content and title information of the table, and divides the received data into segmented data matching different Grid areas. The segmented data is used by the Grid list component. The construction module uses six Grid list components to construct different parts of the table. The six Grid list components are: topLeft, topRight, middleLeft, middleRight, bottomLeft and bottomRight. topLeft and topRight represent the top left and top right of the table respectively, middleLeft and middleRight represent the middle part of the table, and bottomLeft and bottomRight represent the bottom left and right parts respectively; A setting module is used to set a first container for the bottomRight component. The first container has an overflow-x:auto style attribute. When the table content exceeds the width of the first container, a horizontal scroll bar is displayed in the table. The content of bottomRight is set starting from the second column, and the first column of the table is fixed to the left, so that when the table is scrolled horizontally, the first column does not scroll; Place the module, place a second container with a fixed width next to the first container in bottomRight, the second container is used to display the content of the first column, the second container is vertically aligned with the first container, and the width of the second container matches the width of the first column in bottomRight; Update the module to monitor scroll events and update the position of the first column accordingly. When the scroll bar in the bottomRight container moves, the fixed first column is moved synchronously to maintain visual consistency.
10. 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 customizing the scroll bar position of a table in React as described in any one of claims 1 to 8.
Citation Information
Patent Citations
Method for displaying tables in page and mobile terminal
CN111931093A
Method and device for rendering page table data
CN114817815A
Gauge head ceiling method and device
CN118228705A
User interface models providing dynamic analytical capability
US12039348B1
