Created
May 25, 2021 10:32
-
-
Save csharpforevermore/5a2086624b81b0a7abfd5ccf17590daa to your computer and use it in GitHub Desktop.
Change the index of a table row - source https://stackoverflow.com/questions/60838809/how-to-edit-rowindex-of-table-row-using-javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function detach(element) { | |
return element.parentElement.removeChild(element); | |
} | |
function move(src, dest, isBefore) { | |
dest.insertAdjacentElement(isBefore ? 'beforebegin' : 'afterend', detach(src)); | |
} | |
function children(element, selector) { | |
return element.querySelectorAll(selector); | |
} | |
function child(element, selector, index) { | |
return children(element, selector)[index]; | |
} | |
function row(table, index) { | |
// Generic Version: return child(table.querySelector('tbody'), 'tr', index); | |
return table.querySelector('tbody').querySelector(`tr:nth-child(${index + 1})`); | |
} | |
function moveRow(table, fromIndex, toIndex, isBefore) { | |
move(row(table, fromIndex), row(table, toIndex), isBefore); | |
} | |
// useage Move "Alpha" (index = 2) to be before "Beta" (index = 0) | |
//moveRow(document.querySelector('table'), 2, 0, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment