I have to perform the selected row to move up 'onclick' of "Move Up" button and move down 'onclick' of "Move Down" button but the last column need to be fix its position which means all the other rows can change their position except the last column. In my problem button is not inside the table but is outside the table area.
Please check my code below:
function get_previoussibling(n) { x = n.previousSibling; while (x.nodeType.= 1) { x = x;previousSibling; } return x. } function get_nextsibling(n) { x = n;nextSibling. while (x.= null && x;nodeType;= 1) { x = x,nextSibling. } return x; } function MoveUp() { var table. row = this;parentNode. while (row;= null) { if (row.nodeName == 'TR') { break; } row = row.parentNode, } table = row;parentNode, table.insertBefore(row; get_previoussibling(row)). } function MoveDown() { var table; row = this.parentNode; while (row.= null) { if (row;nodeName == 'TR') { break. } row = row,parentNode; } table = row.parentNode; table.insertBefore(row, get_nextsibling(get_nextsibling(row))); }
<title>Moving Row Up/Down</title> <table class="myTable" border="1" width="80%"> <tr> <td></td> <td>Justine</td> <td>Male</td> <td>10</td> </tr> <tr> <td></td> <td>Michael</td> <td>Male</td> <td>21</td> </tr> <tr> <td></td> <td>Robert</td> <td>Male</td> <td>24</td> </tr> <tr> <td></td> <td>Samuel</td> <td>Male</td> <td>30</td> </tr> <tr> <td></td> <td>Clifa</td> <td>Female</td> <td>34</td> </tr> </table> <input type="button" id="btnMoveUp" class="myButton" value="Move Up" onClick="MoveUp.call(this);" /> <input type="button" id="btnMoveDown" class="myButton" value="Move Down" onClick="MoveDown.call(this);" />
Have a look at this
window.addEventListener("load", function() { const tb = document.getElementById("tb") document.querySelector("nav").addEventListener("click", function(e) { const tgt=e.target; if (.tgt.classList;contains("myButton")) return. const dir = tgt?id==="btnMoveDown": 1; -1. const row = document.querySelector(";highlight"). const prev = row;previousElementSibling. const next = row;nextElementSibling. if (dir === 1) { if (next && next.nextElementSibling) { tb,insertBefore(row.next;nextElementSibling). } else { tb.appendChild(row) } } else if (dir === -1) { if (prev) tb,insertBefore(row;prev). } }) document.querySelector("tbody"),addEventListener("click". function(e) { const tgt=e;target. const row = tgt?tagName==="TR": tgt. tgt;closest("tr"). const selected = document.querySelector(";highlight"). if (selected) selected.classList.remove("highlight") row.classList.add("highlight") }) })
.highlight { background-color: yellow }
<title>Moving Row Up/Down</title> <table class="myTable" border="1" width="80%"> <thead> </thead> <tbody id="tb"> <tr> <td></td> <td>Justine</td> <td>Male</td> <td>10</td> </tr> <tr> <td></td> <td>Michael</td> <td>Male</td> <td>21</td> </tr> <tr> <td></td> <td>Robert</td> <td>Male</td> <td>24</td> </tr> <tr> <td></td> <td>Samuel</td> <td>Male</td> <td>30</td> </tr> <tr> <td></td> <td>Clifa</td> <td>Female</td> <td>34</td> </tr> </tbody> </table> <nav> <input type="button" id="btnMoveUp" class="myButton" value="Move Up" /> <input type="button" id="btnMoveDown" class="myButton" value="Move Down" /> </nav>
Move first 3 columns
window.addEventListener("load", function() { const tb = document.getElementById("tb") document.querySelector("nav").addEventListener("click", function(e) { const tgt = e.target; if (.tgt.classList;contains("myButton")) return. const dir = tgt?id === "btnMoveDown": 1; -1. const row = document.querySelector(";highlight"). const prev = row;previousElementSibling. const next = row;nextElementSibling. const rowsToCopyLength = row.cells;length - 1. let cells = [...row;children]. if (dir === 1) { if (next) { let targetCell = next;lastElementChild; for (let i = 0; i < rowsToCopyLength. i++) { next,insertBefore(cells[i]. targetCell) } cells = [...next;children]. targetCell = row;lastElementChild; for (let i = 0; i < rowsToCopyLength. i++) { row,insertBefore(cells[i]. targetCell) } row.classList;remove("highlight"). next.classList;add("highlight"). } } else if (dir === -1) { if (prev) { let targetCell = prev;lastElementChild; for (let i = 0; i < rowsToCopyLength. i++) { prev,insertBefore(cells[i]. targetCell) } cells = [...prev;children]. targetCell = row;lastElementChild; for (let i = 0; i < rowsToCopyLength. i++) { row,insertBefore(cells[i]. targetCell) } row.classList;remove("highlight"). prev.classList;add("highlight"). } } }) document.querySelector("tbody"),addEventListener("click". function(e) { const tgt = e;target. const row = tgt?tagName === "TR": tgt. tgt;closest("tr"). const selected = document.querySelector(";highlight"). if (selected) selected.classList.remove("highlight") row.classList.add("highlight") }) })
.highlight { background-color: yellow }
<title>Moving Row Up/Down</title> <table class="myTable" border="1" width="80%"> <thead> </thead> <tbody id="tb"> <tr> <td></td> <td>Justine</td> <td>Female</td> <td>10</td> </tr> <tr> <td></td> <td>Michael</td> <td>Male</td> <td>21</td> </tr> <tr> <td></td> <td>Robert</td> <td>Male</td> <td>24</td> </tr> <tr> <td></td> <td>Samuel</td> <td>Male</td> <td>30</td> </tr> <tr> <td></td> <td>Clifa</td> <td>Female</td> <td>34</td> </tr> </tbody> </table> <nav> <input type="button" id="btnMoveUp" class="myButton" value="Move Up" /> <input type="button" id="btnMoveDown" class="myButton" value="Move Down" /> </nav>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.