Search code examples
jquerytablesorter

jQuery Table Sorter - Unable to get table to sort on a column of a table (cell values can be changed dynamically by jQuery before sort)


I have a table of rows, with the last cell Yes, or No. That column does not respond to sort and can be modified before sort order is changed, however no sort occurs.

I am unable to produce a Minimal Complete Verifiable Example but I can say that the initialization code is only as simple as.

$(".tablesorter").tablesorter();  //this is inside a document.ready block

All other rows sort, but the one that does not is puzzling as to why.

Is this zero configuration?

I have also tried chainging the above init call to:

$(".tablesorter").tablesorter({
            textExtraction: 'complex',
            headers: {
                10: {sorter: 'text'}   // 10 is the column number starting from the sortable columns
            }
        }); 

But no change occurs.

How do I fix this?


Solution

  • https://dotnetfiddle.net/MF18yG

    Controller/ViewModels

    public class Row
    {
        public string Col1 { get; set; }
        public string Col2 { get; set; }
        public string Col3 { get; set; }
        public string Col4 { get; set; }
        public string Col5 { get; set; }
        public string Col6 { get; set; }
        public string Col7 { get; set; }
        public string Col8 { get; set; }
        public string Col9 { get; set; }
        public string Col10 { get; set; }
    }
    
    public class Table
    {
        public IList<Row> Rows { get; set; }
    }
    public class HomeController : Controller
    {
        public ActionResult Index20()
        {
            var viewModel = new Table { Rows = new List<Row>() };
            var row1 = new Row
            {
                Col1 = "Col1",
                Col2 = "Col2",
                Col3 = "Col1",
                Col4 = "Col4",
                Col5 = "Col5",
                Col6 = "Col6",
                Col7 = "Col7",
                Col8 = "Col8",
                Col9 = "Col9",
                Col10 = "Yes"
            };
            var row2 = new Row
            {
                Col1 = "Col1",
                Col2 = "Col2",
                Col3 = "Col1",
                Col4 = "Col4",
                Col5 = "Col5",
                Col6 = "Col6",
                Col7 = "Col7",
                Col8 = "Col8",
                Col9 = "Col9",
                Col10 = "No"
            };
            var row3 = new Row
            {
                Col1 = "Col1",
                Col2 = "Col2",
                Col3 = "Col1",
                Col4 = "Col4",
                Col5 = "Col5",
                Col6 = "Col6",
                Col7 = "Col7",
                Col8 = "Col8",
                Col9 = "Col9",
                Col10 = "Yes"
            };
            var row4 = new Row
            {
                Col1 = "Col1",
                Col2 = "Col2",
                Col3 = "Col1",
                Col4 = "Col4",
                Col5 = "Col5",
                Col6 = "Col6",
                Col7 = "Col7",
                Col8 = "Col8",
                Col9 = "Col9",
                Col10 = "No"
            };
            var row5 = new Row
            {
                Col1 = "Col1",
                Col2 = "Col2",
                Col3 = "Col1",
                Col4 = "Col4",
                Col5 = "Col5",
                Col6 = "Col6",
                Col7 = "Col7",
                Col8 = "Col8",
                Col9 = "Col9",
                Col10 = "Yes"
            };
            viewModel.Rows.Add(row1);
            viewModel.Rows.Add(row2);
            viewModel.Rows.Add(row3);
            viewModel.Rows.Add(row4);
            viewModel.Rows.Add(row5);
            return View(viewModel);
        }
    

    View

    @model WebApplication2.Controllers.Table
    @using WebApplication2.Controllers
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>Index20</title>
        @*https://mottie.github.io/tablesorter/docs/*@
        <!-- choose a theme file -->
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.29.0/css/theme.default.min.css"
              integrity="sha256-kZJ4kB78IbXuxMtCpmaXzii8vxEKtu8pjicH62E0/qM=" crossorigin="anonymous" />
        <!-- load jQuery and tablesorter scripts -->
        <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.3/js/jquery.tablesorter.min.js"></script>
    
        <!-- tablesorter widgets (optional) -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.31.3/js/jquery.tablesorter.widgets.min.js"></script>
        <script type="text/javascript">
            $(function () {
                $("#myTable").tablesorter();
            });
        </script>
    </head>
    <body>
        <div>
            <table id="myTable" class="tablesorter">
                <thead>
                    <tr>
                        <th>Col1</th>
                        <th>Col2</th>
                        <th>Col3</th>
                        <th>Col4</th>
                        <th>Col5</th>
                        <th>Col6</th>
                        <th>Col7</th>
                        <th>Col8</th>
                        <th>Col9</th>
                        <th>Col10</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach (Row row in Model.Rows)
                    {
                        <tr>
                            <td>
                                @row.Col1
                            </td>
                            <td>
                                @row.Col2
                            </td>
                            <td>
                                @row.Col3
                            </td>
                            <td>
                                @row.Col4
                            </td>
                            <td>
                                @row.Col5
                            </td>
                            <td>
                                @row.Col6
                            </td>
                            <td>
                                @row.Col7
                            </td>
                            <td>
                                @row.Col8
                            </td>
                            <td>
                                @row.Col9
                            </td>
                            <td>
                                @row.Col10
                            </td>
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    </body>
    </html>