Is there a possibility to shuffle table rows and make them appear at random each time we click a button/icon (except for the first one with a header)? Something like w3schools' "How To Sort a Table" (.asp) but so that the table would be sorted randomly.
The other possibility is to use JavaScript Array Sort but I wouldn't know how to make table rows appear as contents of an array.
/
I'd prefer vanilla JS solutions rather than jQuery.
Here is an exemplary table that we could work on:
.table-div {
padding-top: 1rem;
}
<div class="table-div">
<table id="myTable">
<tr>
<th class="button"><button class="my-btn" type="button" onclick="sortTable()">
shuffle</button></th>
<th>Text:</th>
<th></th>
</tr>
<tr>
<td class="left">Some text 1</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 2</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 3</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 4</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 5</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 6</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 7</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
Is there a possibility to shuffle table rows and make them appear at random each time we click a button/icon (except for the first one with a header)? Something like w3schools' "How To Sort a Table" (https://www.w3schools./howto/howto_js_sort_table.asp) but so that the table would be sorted randomly.
The other possibility is to use JavaScript Array Sort but I wouldn't know how to make table rows appear as contents of an array.
https://jsfiddle/17bjxgfa/1/
I'd prefer vanilla JS solutions rather than jQuery.
Here is an exemplary table that we could work on:
.table-div {
padding-top: 1rem;
}
<div class="table-div">
<table id="myTable">
<tr>
<th class="button"><button class="my-btn" type="button" onclick="sortTable()">
shuffle</button></th>
<th>Text:</th>
<th></th>
</tr>
<tr>
<td class="left">Some text 1</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 2</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 3</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 4</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 5</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 6</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 7</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
You can do the following:
document.getElementsByTagName
or if you want to have a bit more specificity - document.querySelectorAll
.HTMLCollection
, so you can just convert it into an array.
function sortTable() {
//get the parent table for convenience
let table = document.getElementById("myTable");
//1. get all rows
let rowsCollection = table.querySelectorAll("tr");
//2. convert to array
let rows = Array.from(rowsCollection)
.slice(1); //skip the header row
//3. shuffle
shuffleArray(rows);
//4. add back to the DOM
for (const row of rows) {
table.appendChild(row);
}
}
/**
* Randomize array element order in-place.
* Using Durstenfeld shuffle algorithm.
* from: https://stackoverflow./questions/2450954/how-to-randomize-shuffle-a-javascript-array/12646864#12646864
*/
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
.table-div {
padding-top: 1rem;
}
<div class="table-div">
<table id="myTable">
<tr>
<th class="button"><button class="my-btn" type="button" onclick="sortTable()">
shuffle</button></th>
<th>Text:</th>
<th></th>
</tr>
<tr>
<td class="left">Some text 1</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 2</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 3</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 4</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 5</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 6</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
<tr>
<td class="left">Some text 7</td>
<td><input type="text"></td>
<td class="right">more text.</td>
<td class="button"><button class="my-btn" type="button">
check</button></td>
</tr>
Well, as far I could help without making all your code, is to get you going with a simple and legible logic...
Like you said, you want to shuffle the rows, so, for that you need to get all your rows. You can do it with var tableElms = document.getElementById("myTable").children;
and filter it to get only the rows that you want.
After that you can create a list with the results and then sort it with var sortedList = filteredList.sort();
after that change your table dom (clean it) and append the new rows document.getElementById('myTable').appendChild(sortedList);
Hope I could help you :)