当前位置:网站首页>Drag and drop table rows
Drag and drop table rows
2022-07-26 21:27:00 【Ziwei front end】
- When the user starts moving table rows , We create a list of items . Each item is cloned from each row of the table .
- We display the list in the same place as the table , And hide the table .
- In this step , Moving a row is actually moving a list item .
- When a user drags an item , We determine the index of the target item in the list . And move the original dragged row before or after the row associated with the end index .
<table id="table">
...
</table>Basic settings
mousedownFor the first cell of any row , So the user can click and drag the first cell in each rowmousemovefordocument: This event is triggered when the user moves a row , We will follow the direction ( Up or down ) Create and insert placeholder rowsmouseupfordocument: This event occurs when the user drags a row .
// Query the table
const table = document.getElementById('table');
const mouseDownHandler = function(e) {
...
// Attach the listeners to `document`
document.addEventListener('mousemove', mouseMoveHandler);
document.addEventListener('mouseup', mouseUpHandler);
};
const mouseMoveHandler = function(e) {
...
};
const mouseUpHandler = function() {
...
// Remove the handlers of `mousemove` and `mouseup`
document.removeEventListener('mousemove', mouseMoveHandler);
document.removeEventListener('mouseup', mouseUpHandler);
};
// Query all rows
table.querySelectorAll('tr').forEach(function(row, index) {
// Ignore the header
// We don't want user to change the order of header
if (index === 0) {
return;
}
// Get the first cell of row
const firstCell = row.firstElementChild;
firstCell.classList.add('draggable');
// Attach event handler
firstCell.addEventListener('mousedown', mouseDownHandler);
});Clone the table when the user moves a row
let isDraggingStarted = false;
const mouseMoveHandler = function(e) {
if (!isDraggingStarted) {
isDraggingStarted = true;
cloneTable();
}
...
};cloneTable Create an element with the same location as the table , And display before the table :
let list;
const cloneTable = function () {
// Get the bounding rectangle of table
const rect = table.getBoundingClientRect();
// Get the width of table
const width = parseInt(window.getComputedStyle(table).width);
// Create new element
list = document.createElement('div');
// Set the same position as table
list.style.position = 'absolute';
list.style.left = `${rect.left}px`;
list.style.top = `${rect.top}px`;
// Insert it before the table
table.parentNode.insertBefore(list, table);
// Hide the table
table.style.visibility = 'hidden';
}; Imagine , it list Consists of items cloned from table rows :
const cloneTable = function() {
...
// Loop over the rows
table.querySelectorAll('tr').forEach(function(row) {
const item = document.createElement('div');
const newTable = document.createElement('table');
const newRow = document.createElement('tr');
// Query the cells of row
const cells = [].slice.call(row.children);
cells.forEach(function(cell) {
const newCell = cell.cloneNode(true);
newRow.appendChild(newCell);
});
newTable.appendChild(newRow);
item.appendChild(newTable);
list.appendChild(item);
});
}; After this step , We have the following list:
<!-- The list -->
<div>
<!-- First item -->
<div>
<table>
<!-- The first row of original table -->
<tr>
...
</tr>
</table>
</div>
<!-- Second item -->
<div>
<table>
<!-- The second row of original table -->
<tr>
...
</tr>
</table>
</div>
<!-- ... -->
</div>
<!-- The original table -->
<table>
...
</table>It is worth noting that , When cloning cells in each item , We must set the cell width to be the same as the original cell . So the project looks exactly like the original line :
cells.forEach(function (cell) {
const newCell = cell.cloneNode(true);
// Set the width as the original cell
newCell.style.width = `${parseInt(window.getComputedStyle(cell).width)}px`;
newRow.appendChild(newCell);
});Determine the index of the drag row and the target row
let draggingEle; // The dragging element
let draggingRowIndex; // The index of dragging row
const mouseDownHandler = function (e) {
// Get the original row
const originalRow = e.target.parentNode;
draggingRowIndex = [].slice.call(table.querySelectorAll('tr')).indexOf(originalRow);
};
const mouseMoveHandler = function (e) {
if (!isDraggingStarted) {
cloneTable();
// Query the dragging element
draggingEle = [].slice.call(list.children)[draggingRowIndex];
}
};
const mouseUpHandler = function () {
// Get the end index
const endRowIndex = [].slice.call(list.children).indexOf(draggingEle);
};draggingRowIndex Same as endRowIndex, It is now easy to check whether the user has fallen to the top or bottom of the table .
const mouseUpHandler = function () {
// Move the dragged row to `endRowIndex`
let rows = [].slice.call(table.querySelectorAll('tr'));
draggingRowIndex > endRowIndex
? // User drops to the top
rows[endRowIndex].parentNode.insertBefore(rows[draggingRowIndex], rows[endRowIndex])
: // User drops to the bottom
rows[endRowIndex].parentNode.insertBefore(rows[draggingRowIndex], rows[endRowIndex].nextSibling);
};边栏推荐
- In the era of Web3.0, the technical theory of implementing a DAPP based on P2P DB
- word-break: break-all VS word-wrap: break-word
- CFdiv1+2-Pathwalks-(树状数组+线性dp)
- Summary of 4 years of software testing experience and interviews with more than 20 companies after job hopping
- 使用 LSTM 进行多变量时间序列预测--问题汇总
- 【HCIE安全】双机热备-主备备份
- Basic use of livedatade
- In addition to "adding machines", in fact, your micro service can be optimized like this
- Deepfake pinches his face. It's hard to tell whether it's true or false. Tom Cruise is more like himself than himself!
- Ros2 method of obtaining current system time
猜你喜欢

Detailed illustration of B-tree and its implementation in C language

关于:获取当前客户端登录的域控

APaaS低代码平台(一) | 把复杂留给自己,把简单留给用户

Practice of microservice in solving Library Download business problems

DevSecOps,让速度和安全兼顾
HTTP cache browser cache that rabbits can understand

【HCIA安全】双向NAT

【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用的数据恢复
![[HCIA security] user authentication](/img/6f/0ab6287eac36a4647dc2a0646ba969.png)
[HCIA security] user authentication

Retrieve the parameters in this method in idea for our use -- 1. Class diagram. 2. Double click shift
随机推荐
Number() VS parseInt()
In addition to "adding machines", in fact, your micro service can be optimized like this
使用 LSTM 进行多变量时间序列预测--问题汇总
Retrieve the parameters in this method in idea for our use -- 1. Class diagram. 2. Double click shift
Devsecops, speed and security
In the era of Web3.0, the technical theory of implementing a DAPP based on P2P DB
苹果官网罕见打折,iPhone13全系优惠600元;国际象棋机器人弄伤对弈儿童手指;国内Go语言爱好者发起新编程语言|极客头条
记一次invalid bound statement xxxxxx 问题解决思路
调整表格列的大小
Summary of common interview questions on computer network, including answers
Modify excel default code
Why didn't Tencent create a game like "original God"
Live broadcast appointment award | senior consultant xuyanfei: how does efficiency measurement help efficient and sophisticated outsourcing management
Deepfake pinches his face. It's hard to tell whether it's true or false. Tom Cruise is more like himself than himself!
js点击图片打印图像
Rare discounts on Apple's official website, with a discount of 600 yuan for all iphone13 series; Chess robot injured the fingers of chess playing children; Domestic go language lovers launch a new pro
Why does it system need observability?
JDBC connection
Mysql -count :count(1)、count(*)、count(列名)的区别
golang版本管理gvm