How to use JavaScript to implement drag-and-drop adjustment of table column width?
With the development of Web technology, more and more data are displayed on web pages in the form of tables. However, sometimes the column width of the table cannot meet our needs, and the content may overflow or the width may be insufficient. In order to solve this problem, we can use JavaScript to implement the drag-and-drop adjustment function of the column width of the table, so that users can freely adjust the column width according to their needs.
To implement the drag-and-drop adjustment function of table column width, the following three main steps are required:
The following will introduce the implementation methods of the above three steps in detail and give corresponding code examples.
First, add mousedown
event listening to the table. When the user clicks on the edge of a table column, they can start dragging to adjust the column width. In the mousedown
event handling function, the position of the mouse click needs to be recorded.
function tableMouseDown(event) { // 记录鼠标点击的位置 const startX = event.clientX; // ... }
Next, add the mousemove
event listener to the document
object. In the mousemove
event handler function, you need to calculate the distance moved by the mouse and dynamically change the width of the table column.
function documentMouseMove(event) { // 计算鼠标移动的距离 const distanceX = event.clientX - startX; // 动态改变表格列的宽度 // ... }
Finally, add a mouseup
event listener for the document
object. When the user releases the mouse, stop resizing the column width.
function documentMouseUp() { // 停止调整列宽 // ... }
In the mousemove
event handler, dynamically adjust the width of the table column according to the user's drag operation. First, you need to determine which column is currently being dragged, which can be determined through the th
element in the header of the table. Then, dynamically change the width of the table column based on the calculated mouse movement distance.
function documentMouseMove(event) { // 计算鼠标移动的距离 const distanceX = event.clientX - startX; // 动态改变表格列的宽度 const th = document.elementFromPoint(startX, event.clientY); const columnIndex = th.cellIndex; const table = th.parentNode.parentNode.parentNode; const cells = table.querySelectorAll(`tr th:nth-child(${columnIndex + 1}), tr td:nth-child(${columnIndex + 1})`); const newWidth = parseFloat(getComputedStyle(cells[0]).width) + distanceX; for (const cell of cells) { cell.style.width = `${newWidth}px`; } }
In order to maintain the column width adjusted by the user after the page is refreshed or reloaded, we need to record the column width status of the table. You can use localStorage
or cookie
to achieve persistent data storage.
function documentMouseUp() { // 停止调整列宽 // ... // 记录表格的列宽状态 const columnWidths = {}; const table = document.querySelector('table'); const columns = table.querySelectorAll('th'); for (const column of columns) { columnWidths[column.cellIndex] = parseFloat(getComputedStyle(column).width); } localStorage.setItem('columnWidths', JSON.stringify(columnWidths)); }
When the page loads, the saved column width state can be read from localStorage
and applied to the table.
window.addEventListener('load', function() { const columnWidths = JSON.parse(localStorage.getItem('columnWidths')); if (columnWidths) { const table = document.querySelector('table'); const columns = table.querySelectorAll('th'); for (const [index, width] of Object.entries(columnWidths)) { columns[index].style.width = `${width}px`; } } });
Through the above three steps, we can realize the drag and drop adjustment function of table column width. Users can freely adjust the column width of the table and optimize the display effect of the table according to actual needs. Hope this article is helpful to you!
The above is the detailed content of How to use JavaScript to implement drag-and-drop adjustment of table column width?. For more information, please follow other related articles on the PHP Chinese website!