How can I sort my table that accesses a sql database (using the sorting method describeed on here)

How can I sort my table that accesses a sql database (using the sorting method describeed on here)

richphilrichphil Posts: 1Questions: 1Answers: 0

The main part of my code is shown below. What happens is that I think using a for each, means that the rows and columns are not treated separately so cant be sorted, but don't know if this is the case and how to solve it?

For example, it always says "Showing 1 to 1 of 1 entries", only every highlighting the top row.

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/jquery.dataTables.min.css ">
<script src="//code.jquery.com/jquery-1.12.3.js "></script>
<script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js "></script>

</head>
<body>

    <script>
        $(document).ready(function() {

            $('#customers').DataTable( {

                "order":Option [[ 2, "desc" ]]

        } );

        } );</script>
    <table class="display"  id="customers"  cellspacing="0">

        <thead>
            <tr>
                <th>CustomerID</th>
                <th>CompanyName</th>
                <th>ContactName</th>
                <th>Phone</th>
            </tr>
        </thead>

            <tfoot>
                <tr>
                    <th>CustomerID</th>
                    <th>CompanyName</th>
                    <th>ContactName</th>
                    <th>Phone</th>
                </tr>
                </tfoot>
        @foreach (var row in Database.Open("Database").Query("SELECT*FROM Customers"))
        {
                <tbody>
                    <tr>
                        <td>@row.CustomerID</td>
                        <td>@row.CompanyName</td>
                        <td>@row.ContactName</td>
                        <td>@row.Phone</td>
                    </tr>
                    </tbody>

                }

</table>
</body>
</html>

Answers

  • F12MagicF12Magic Posts: 109Questions: 0Answers: 28

    Hi @richphil,

    at line 9 of the code above, try changing it to:

    order: [[ 2, "desc" ]]

  • allanallan Posts: 61,864Questions: 1Answers: 10,136 Site admin

    The tbody is inside of the foreach loop. Move it outside and it will work as expected.

    At the moment every row has a tbody, rather than there just being a single tbody (which currently is all DataTables supports).

    Allan

This discussion has been closed.