html - Javascript: Shuffle table rows - Stack Overflow

admin2025-04-19  0

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>

Share edited Sep 12, 2019 at 13:46 mplungjan 179k28 gold badges182 silver badges240 bronze badges asked Sep 12, 2019 at 13:37 Krzysztof DelestowiczKrzysztof Delestowicz 1292 silver badges11 bronze badges 1
  • 1. have the content in a JS array. 2. Shuffle it. 3. Render the table from the array – mplungjan Commented Sep 12, 2019 at 13:48
Add a ment  | 

2 Answers 2

Reset to default 6

You can do the following:

  1. Get all of the rows of the table using lookups like document.getElementsByTagName or if you want to have a bit more specificity - document.querySelectorAll.
  2. These return an HTMLCollection, so you can just convert it into an array.
  3. Shuffle the array.
  4. Add it back to the table - since a node cannot appear twice in the DOM, that will move them, thus you don't have to manually remove them first.

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 :)

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745071834a283330.html

最新回复(0)