0

I'm trying to sort a table by column according to the content in the 'tbody' first row (tr class='Statistics)'.

Each 'td' in the row has two 'divs' identified by classes that match the values for their respective selection in the sort dropdown. (color coded for ease, as in below image)

enter image description here

The jquery I'm using to activate the sort function is as follows:

jQuery(document).ready(function ($) {

        $("#SortBy").on('change', function() {
            var Column = $(".CompTable tr > td:nth-child(n):not(:first-child), .CompTable tr > th:nth-child(n):not(:first-child)");

            Column.sort(function(a, b) {
                a = $(a).find("tr.Statistics > td:not(:first) div."+jQuery(this).find("option:selected").val());
                b = $(b).find("tr.Statistics > td:not(:first) div."+jQuery(this).find("option:selected").val());

                return (a === 'NA')-(b === 'NA') || -(a>b)||+(a<b);
            })
            jQuery('.divResult').html(Column); // <-- Table nested inside 'div' 
        });
    });

I believe the issue is with this bit of code that I'm using to identify each column, except the 1st, in the table:

var Column = $(".CompTable tr > td:nth-child(n):not(:first-child), .CompTable tr > th:nth-child(n):not(:first-child)");

Please help me with correcting the above code. Currently when I'm selecting a sort option, the table disappears. :/

Below is the entire working code for reference:

jQuery(document).ready(function ($) {

        $("#SortBy").on('change', function() {
        var Column = $(".CompTable tr > td:nth-child(n):not(:first-child), .CompTable tr > th:nth-child(n):not(:first-child)");
            
            Column.sort(function(a, b) {
                a = $(a).find("tr.Statistics > td:not(:first) div."+jQuery(this).find("option:selected").val());
                b = $(b).find("tr.Statistics > td:not(:first) div."+jQuery(this).find("option:selected").val());

                return (a === 'NA')-(b === 'NA') || -(a>b)||+(a<b);
            })
            jQuery('.divResult').html(Column);
        });
    });
.CompTable {
  table-layout: fixed;
  width: 50%;
  position: relative;
  font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
  margin: 10px;
  border: 1px solid #222;
  text-align: center;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<div id="SortByDiv">
    Sort by: 
    <select id="SortBy">
        <option></option>
        <option value=Battery style="color: blue">Battery</option>
        <option value=ScreenSize style="color: red">ScreenSize</option>
    </select>
</br></br></br>
    <div class="divResult">
    <table class="CompTable">
    <thead>
        <th>&nbsp;</th>
        <th>Samsung</th>
        <th>Apple</th>
        <th>Motorola</th>
    </thead>
    <tbody>
        <tr class="Statistics">
            <td>Statistics</td>
            <td>
                <div style="display:flex; flex-flow:column-wrap; width: 100%;">
                    <div class="Battery" style="display:flex; color:blue; width: 100%;">3200</div>
                    <div class="ScreenSize" style="display:flex; color:red; width: 100%;">1024</div>
                </div>
            </td>
            <td>
                <div style="display:flex; flex-flow:column-wrap; width: 100%;">
                    <div class="Battery" style="display:flex; color:blue; width: 100%;">NA</div>
                    <div class="ScreenSize" style="display:flex; color:red; width: 100%;">1900</div>
                </div>
            </td>
            <td>
                <div style="display:flex; flex-flow:column-wrap; width: 100%;">
                    <div class="Battery" style="display:flex; color:blue; width: 100%;">4100</div>
                    <div class="ScreenSize" style="display:flex; color:red; width: 100%;">1500</div>
                </div>
            </td>
        </tr>
        <tr class="Color">
            <td>Color</td>
            <td>Blue</td>
            <td>Red</td>
            <td>Black</td>
        </tr>
    </tbody>
    </table>
    </div>

Edit: Corrected the mistake in the jQuery table class name 'CompTable' pointed out as in the comment below.

Mithun Uchil
  • 347
  • 1
  • 12
  • First of all the class name is CompTable and not compTable, but there are more issues after... – Vladan Nov 04 '19 at 16:55
  • Hey thanks, @Vladan, ur right!! That was a very silly and embarassing oversight by me. :P But as you said, there are other issues, as the output is still not coming. Will edit the correction u pointed out in the question. Thanks! – Mithun Uchil Nov 04 '19 at 17:02
  • Possible duplicate of [jQuery table sort](https://stackoverflow.com/questions/3160277/jquery-table-sort) – Vladan Nov 04 '19 at 17:48
  • Just a quick look: `Column=tds/ths` sort: `column.find(tr)` - your `td`s don't have `tr` *children* - so this doesn't find anything. Add some debugging `console.log(a.length, b.length)` - so nothing sorted. Second, **you replace your `table` with the `td`s**, so no wonder it breaks. – freedomn-m Nov 04 '19 at 17:59

0 Answers0