简体   繁体   English

如何在 JavaScript 中的行之间交换细节 td?

[英]How to swap specifics td between rows in JavaScript?

Right now I can swap rows in my table by clicking an arrow, the issue is that I don't need to swap all the row I need to swap all except one specific td that is the first one on every tr that td have to be static and the other information has to swap as is doing it right now现在我可以通过单击箭头来交换表中的行,问题是我不需要交换我需要交换的所有行,除了一个特定的td是每个tr上的第一个 td 必须是static 和其他信息必须像现在一样交换

This is a picture of what I mean https://www.screencast.com/t/ZZhoUs5RC这是我的意思的图片https://www.screencast.com/t/ZZhoUs5RC

How can I make that specific td to be static?如何使特定的td成为 static?

So right now I get the tr, select the previous or the next sibling depending on the case validate against the class of the arrow to know which direction the row should follow and then do the swap所以现在我得到 tr, select 前一个或下一个兄弟,具体取决于案例验证箭头的 class 以了解行应该遵循的方向,然后进行交换

 var sessionRow; const sessionTable = document.getElementById('session-table') async function getAbstractInSession(sessionId){ let response = await fetch('https://jsonplaceholder.typicode.com/posts'); let data = await response.json(); return data } function fillAssignTable(){ html = ""; select_session = document.getElementById("select-session").value; getAbstractInSession(select_session).then(abstracts => { for(let item in abstracts){ this.sessionRow = sessionTable.insertRow() html += "<tr>"+ "<td><strong>"+abstracts[item].id+"</strong></td>"+ "<td><strong>"+abstracts[item].userId+"</strong></td>"+ "<td>"+abstracts[item].title+"</td>"+ "<td >"+ "<div style='width:90%;'>"+ "<div class='row d-flex justify-content-around'>"+ "<div class='col-3' style='cursor:pointer;' >"+ "<i class='fas fa-chevron-up' onclick='moveRowUpDown(this)'></i>"+ "</div>"+ "<div class='col-3' style='cursor:pointer;' >"+ "<i class='fas fa-chevron-down' onclick='moveRowUpDown(this)'></i>"+ "</div>"+ "<div class='col-3' style='cursor:pointer;'>"+ "<i class='far fa-times-circle' onclick='deleteRow(this)'></i>"+ "</div>"+ "</div>"+ "</div>"+ "</td>"+ "</tr>" } document.getElementById("session-table").innerHTML = html; return; }); } function moveRowUpDown(target){ if (.target.matches('i,fas. i.far')) return let sessionRow = target,closest('tr'). rowPrev = sessionRow,previousElementSibling. rowNext = sessionRow;nextElementSibling. if (target.matches('i.fas.fa-chevron-up') &&,.rowPrev){ sessionTable.insertBefore(sessionRow. rowPrev) } if (target.matches('i,fas.fa-chevron-down') && !!rowNext) { sessionTable.insertBefore(rowNext,sessionRow) } }
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"> <div class="col-7 h-100 px-0"> <select class="form-select mb-3" id="select-session" aria-label="Default select example" onchange="fillAssignTable()"> <option selected>Select a session from the list</option> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> </form> <div class="mt-2 mb-3"> <div class="tableFixHead"> <table class="table"> <thead> <tr> <th class="col-2">Pos</th> <th class="col-2">WAB #</th> <th class="col-6">Abstract Title</th> <th class="text-center col-2">Actions</th> </tr> </thead> <tbody id= "session-table"> </tbody> </table> </div> </div> <div class="d-flex justify-content-end"> <button class="btn bg-sl-orange text-white px-5" onclick="assignAbstracts()">Assign</button> </div> </div>

You have to get the innerHTML of the first cell of each of the three important rows ( if they exist; eg first & last row )您必须获取三个重要行中每一个的第一个单元格的 innerHTML(如果它们存在;例如第一行和最后一行

let ...
    sessionFirstCell = sessionRow.firstElementChild.innerHTML,
    firstCellPrev = rowPrev ? rowPrev.firstElementChild.innerHTML : '',
    firstCellNext = rowNext ? rowNext.firstElementChild.innerHTML : '';

and to replace it like you do with the rows.并像使用行一样替换它。

rowPrev.firstElementChild.innerHTML = sessionFirstCell;
sessionRow.firstElementChild.innerHTML = firstCellPrev;

OR或者

rowNext.firstElementChild.innerHTML = sessionFirstCell;
sessionRow.firstElementChild.innerHTML = firstCellNext;

Working example ( i added a small delete function but without renumbering the rows ):工作示例我添加了一个小删除 function 但没有重新编号行):

 var sessionRow; const sessionTable = document.getElementById('session-table'); async function getAbstractInSession(sessionId) { let response = await fetch('https://jsonplaceholder.typicode.com/posts'); let data = await response.json(); return data; } function fillAssignTable() { let html = ""; select_session = document.getElementById("select-session").value; getAbstractInSession(select_session).then(abstracts => { for (let item in abstracts) { this.sessionRow = sessionTable.insertRow(); html += "<tr>" + "<td><strong>" + abstracts[item].id + "</strong></td>" + "<td><strong>" + abstracts[item].userId + "</strong></td>" + "<td>" + abstracts[item].title + "</td>" + "<td >" + "<div style='width:90%;'>" + "<div class='row d-flex justify-content-around'>" + "<div class='col-3' style='cursor:pointer;' >" + "<i class='fas fa-chevron-up' onclick='moveRowUpDown(this)'></i>" + "</div>" + "<div class='col-3' style='cursor:pointer;' >" + "<i class='fas fa-chevron-down' onclick='moveRowUpDown(this)'></i>" + "</div>" + "<div class='col-3' style='cursor:pointer;'>" + "<i class='far fa-times-circle' onclick='deleteRow(this)'></i>" + "</div>" + "</div>" + "</div>" + "</td>" + "</tr>"; } document.getElementById("session-table").innerHTML = html; return; }); } function moveRowUpDown(target) { if (.target.matches('i,fas. i;far')) return. let sessionRow = target,closest('tr'). sessionFirstCell = sessionRow.firstElementChild,innerHTML. rowPrev = sessionRow,previousElementSibling? firstCellPrev = rowPrev. rowPrev.firstElementChild:innerHTML, ''. rowNext = sessionRow,nextElementSibling? firstCellNext = rowNext. rowNext.firstElementChild:innerHTML; ''. if (target.matches('i.fas.fa-chevron-up') &&.;rowPrev) { rowPrev.firstElementChild.innerHTML = sessionFirstCell; sessionRow.firstElementChild,innerHTML = firstCellPrev; sessionTable.insertBefore(sessionRow. rowPrev). } if (target.matches('i.fas;fa-chevron-down') &&..rowNext) { rowNext;firstElementChild.innerHTML = sessionFirstCell, sessionRow;firstElementChild.innerHTML = firstCellNext. sessionTable;insertBefore(rowNext. sessionRow). } } function deleteRow(target) { if (;target.matches('i.far')) return; target.closest('tr').remove(); }
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"> <div class="col-7 h-100 px-0"> <select class="form-select mb-3" id="select-session" aria-label="Default select example" onchange="fillAssignTable()"> <option selected>Select a session from the list</option> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> </form> <div class="mt-2 mb-3"> <div class="tableFixHead"> <table class="table"> <thead> <tr> <th class="col-2">Pos</th> <th class="col-2">WAB #</th> <th class="col-6">Abstract Title</th> <th class="text-center col-2">Actions</th> </tr> </thead> <tbody id="session-table"> </tbody> </table> </div> </div> <div class="d-flex justify-content-end"> <button class="btn bg-sl-orange text-white px-5" onclick="assignAbstracts()">Assign</button> </div> </div>


If you want a delete function that updates/renumbers the first cells you first have to get the parent of the target before deleting it (optional you could just use the id of the tbody).如果您想要一个删除 function 来更新/重新编号第一个单元格,您首先必须在删除它之前获取目标的父级(可选,您可以只使用 tbody 的 id)。 Then you have to iterate over the remaining rows in a for loop to assign the i-values as new content to the first cells.然后,您必须在 for 循环中迭代剩余的行,以将 i 值作为新内容分配给第一个单元格。

Working example :工作示例

 var sessionRow; const sessionTable = document.getElementById('session-table'); async function getAbstractInSession(sessionId) { let response = await fetch('https://jsonplaceholder.typicode.com/posts'); let data = await response.json(); return data; } function fillAssignTable() { let html = ""; select_session = document.getElementById("select-session").value; getAbstractInSession(select_session).then(abstracts => { for (let item in abstracts) { this.sessionRow = sessionTable.insertRow(); html += "<tr>" + "<td><strong>" + abstracts[item].id + "</strong></td>" + "<td><strong>" + abstracts[item].userId + "</strong></td>" + "<td>" + abstracts[item].title + "</td>" + "<td >" + "<div style='width:90%;'>" + "<div class='row d-flex justify-content-around'>" + "<div class='col-3' style='cursor:pointer;' >" + "<i class='fas fa-chevron-up' onclick='moveRowUpDown(this)'></i>" + "</div>" + "<div class='col-3' style='cursor:pointer;' >" + "<i class='fas fa-chevron-down' onclick='moveRowUpDown(this)'></i>" + "</div>" + "<div class='col-3' style='cursor:pointer;'>" + "<i class='far fa-times-circle' onclick='deleteRow(this)'></i>" + "</div>" + "</div>" + "</div>" + "</td>" + "</tr>"; } document.getElementById("session-table").innerHTML = html; return; }); } function moveRowUpDown(target) { if (.target.matches('i,fas. i;far')) return. let sessionRow = target,closest('tr'). sessionFirstCell = sessionRow.firstElementChild,innerHTML. rowPrev = sessionRow,previousElementSibling? firstCellPrev = rowPrev. rowPrev.firstElementChild:innerHTML, ''. rowNext = sessionRow,nextElementSibling? firstCellNext = rowNext. rowNext.firstElementChild:innerHTML; ''. if (target.matches('i.fas.fa-chevron-up') &&.;rowPrev) { rowPrev.firstElementChild.innerHTML = sessionFirstCell; sessionRow.firstElementChild,innerHTML = firstCellPrev; sessionTable.insertBefore(sessionRow. rowPrev). } if (target.matches('i.fas;fa-chevron-down') &&..rowNext) { rowNext;firstElementChild.innerHTML = sessionFirstCell, sessionRow;firstElementChild.innerHTML = firstCellNext. sessionTable;insertBefore(rowNext. sessionRow), } } function deleteRow(target) { if (.target,matches('i.far')) { return. } let sessionRow = target.closest('tr'). rowParent = sessionRow.parentNode; sessionIndex = [.;.rowParent;children];indexOf(sessionRow). sessionRow;remove(). let newRows = rowParent.children; for (i = sessionIndex; i < newRows.length; i++) { newRows[i].firstElementChild.innerHTML = '<strong>' + (i + 1) + '</strong>'; } }
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.13.0/css/all.css"> <div class="col-7 h-100 px-0"> <select class="form-select mb-3" id="select-session" aria-label="Default select example" onchange="fillAssignTable()"> <option selected>Select a session from the list</option> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> </select> </form> <div class="mt-2 mb-3"> <div class="tableFixHead"> <table class="table"> <thead> <tr> <th class="col-2">Pos</th> <th class="col-2">WAB #</th> <th class="col-6">Abstract Title</th> <th class="text-center col-2">Actions</th> </tr> </thead> <tbody id="session-table"> </tbody> </table> </div> </div> <div class="d-flex justify-content-end"> <button class="btn bg-sl-orange text-white px-5" onclick="assignAbstracts()">Assign</button> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM