DoIt
DoIt

Reputation: 3438

Sorting and Paging Columns within a html table with jquery not working

I am working on an application in which I should be able to sort a table on clicking on the column header in ASP.NET MVC View . I have the following code

1. INDEX

 @{
    Layout = null;  }
<!DOCTYPE html>
<html>
<head>

<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />

<script src="@Url.Content("~/Scripts/jquery.tablesorter.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.tablesorter.pager.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")" type="text/javascript"></script>

<script type="text/javascript">

    $(document).ready(function () {
        $("#tablesorter").tablesorter();
    } );
    </script>
</head>
<body>
    <div>
        <table class="tablesorter">
            <thead>
            <tr>
                <th>Name</th>
                <th>Surname</th>
                <th>Email</th>
                <th>Phone</th>
                <th>Date Added</th>
            </tr>
        </thead>
        <tbody>

            <tr>
                <td>Daya</td>
                <td>AB</td>
                <td>123</td>
                <td>Phone</td>
                <td>DateAdded</td>
            </tr>
            <tr>
                <td>da</td>
                <td>AB</td>
                <td>456</td>
                <td>324</td>
                <td>243</td>
            </tr>

            <tr>
                <td>kasr</td>
                <td>43</td>
                <td>1tdf23</td>
                <td>fhdf</td>
                <td>jhrtj</td>
            </tr>

        </tbody>
        <tfoot>
            <tr>
                <th>Name</th>
                <th>Surname</th>
                <th>Email</th>
                <th>Phone</th>
                <th>Date Added</th>
            </tr>
        </tfoot>
    </table>
    <div id="pager">
        <form>
            <img src="@Url.Content("~/Content/images/first.png")" class="first" />
            <img src="@Url.Content("~/Content/images/prev.png")" class="prev" />
            <input type="text" class="pagedisplay" />
            <img src="@Url.Content("~/Content/images/next.png")" class="next" />
            <img src="@Url.Content("~/Content/images/last.png")" class="last" />
            <select class="pagesize">
                <option selected="selected" value="5">5</option>
                <option value="10">10</option>
                <option value="20">20</option>
                <option value="30">30</option>
                <option value="40">40</option>
            </select>
        </form>
    </div>
</div>
</body>
</html>

> 2. CSS

body {
    font-size: 75%;
    font-family: Verdana, Tahoma, Arial, "Helvetica Neue", Helvetica, Sans-Serif;
    color: #232323;
    background-color: #fff;
}
table { border-spacing:0; border:1px solid gray;}
table.tablesorter thead tr .header {
    background-image: url(images/bg.png);
    background-repeat: no-repeat;
    background-position: center right;
    cursor: pointer;
}
table.tablesorter tbody td {
    color: #3D3D3D;
    padding: 4px;
    background-color: #FFF;
    vertical-align: top;
}
table.tablesorter tbody tr.odd td {
    background-color:#F0F0F6;
}
table.tablesorter thead tr .headerSortUp {
    background-image: url(images/asc.png);
}
table.tablesorter thead tr .headerSortDown {
    background-image: url(images/desc.png);
}
table th { width:150px; 
           border:1px outset gray; 
           background-color:#3C78B5; 
           color:White; 
           cursor:pointer;
}
table thead th:hover { background-color:Yellow; color:Black;}
table td { width:150px; border:1px solid gray;}

When I run the above source, It displays the table with all the appropriate CSS but it never sorts the columns when I click on them. In the JavaScript Console, The exception I received was

"Uncaught ReferenceError: jQuery is not defined"  in jquery.tabelsorter.js
"Uncaught TypeError: Object [object Object] has no method 'tablesorter'  " in Index(15)

May I know where exactly I am making a mistake which resulted in those exceptions

Upvotes: 0

Views: 4917

Answers (2)

adamb
adamb

Reputation: 4883

Your JS dependency includes are out of order. jQuery is undoubtedly needed to load jquery.tablesorter.js, so you need to include it first.

correct order:

<script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.tablesorter.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.tablesorter.pager.js")" type="text/javascript"></script>

EDIT: Also, change:

$("#tablesorter").tablesorter()

to:

$(".tablesorter").tablesorter();

You need to select by the desired element's class, not id.

Upvotes: 1

mayabelle
mayabelle

Reputation: 10014

You have to include jquery before the library that builds on it:

<script src="@Url.Content("~/Scripts/jquery-1.9.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.tablesorter.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.tablesorter.pager.js")" type="text/javascript"></script>

Upvotes: 1

Related Questions