1

I'm trying to make a simplest possible table sorting for my MVC project, and found this nice reference here jQuery table sort, although it doesn't seem to work for me :( There's a JsFiddle test there as well which works of course ( http://jsfiddle.net/gFzCk ) but mine won't.

This is the table I was trying to sort.

<table id="projectTable">
    <thead>
        <tr>
            <th id="name_header">
                Name
            </th>
            <th id="author_header">
                Author
            </th>
            <th>
                Date Created
            </th>
            <th>
                Date Edited
            </th>
            <th style="text-align:right;"> <a href="@Url.Action("Create", "Project")"><button>&nbsp;Create&nbsp;</button></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model) {
            var id = item.ProjectID;

            <tr id='@id' class="trow @if (System.Web.HttpContext.Current.Session["_SelectedProject"].ToString() == "System.Object") { <text>none</text> }
                            else
                            { if (id == (int)System.Web.HttpContext.Current.Session["_SelectedProjectID"]) {<text>highlighted</text> }}">
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Author)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.DateCreated)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.DateEdited)
                </td>
                <td>
                    <a href='@Url.Action("Edit", "Project", new { id = item.ProjectID })'><img src='@Url.Content("~/Content/images/Edit16x16.png")' title="Edit"/></a> &nbsp;
                    <a href='@Url.Action("Details", "Project", new { id = item.ProjectID })'><img src='@Url.Content("~/Content/images/Details16x16.png")' title="Details" /></a> &nbsp;
                    <a href='@Url.Action("Delete", "Project", new { id = item.ProjectID })'><img src='@Url.Content("~/Content/images/Delete16x16.png")' title="Delete" /></a> &nbsp;
                    <a href='@Url.Action("Select", "Project", new { id = item.ProjectID })' class="select"  ><img src='@Url.Content("~/Content/images/Select16x16.png")' title="Select"  /></a> 
                </td>
            </tr>
         }
    </tbody>
</table>

And this is JQuery I used.

<script type="text/javascript">
    var table = $('#projectTable');

    $('#name_header, #author_header')
        .wrapInner('<span title="sort this column"/>')
        .each(function () {

            var th = $(this),
            thIndex = th.index(),
            inverse = false;

            th.click(function () {

                table.find('td').filter(function () {

                    return $(this).index() === thIndex;

                }).sortElements(function (a, b) {

                    return $.text([a]) > $.text([b]) ?
                        inverse ? -1 : 1
                        : inverse ? 1 : -1;

                }, function () {

                    return this.parentNode;

                });

                inverse = !inverse;

            });

        });
</script>

I've already tested whether it runs the script or not, and it does, it just won't sort it...

HTML Markup

I removed tbody and thead.

<table id="projectTable">
    <tr>
        <th id="name_header">
            Name
        </th>
        <th id="author_header">
            Author
        </th>

        <th>
            Date Created
        </th>
        <th>
            Date Edited
        </th>
        <th style="text-align:right;"> <a href="/Project/Create"><button>&nbsp;Create&nbsp;</button></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
    </tr>
    <tr id='1' class="trow  none ">
        <td>
            Project 1
        </td>
        <td>
            Me
        </td>

        <td>
            8/24/2012 7:08:49 PM
        </td>
        <td>
             8/24/2012 7:08:49 PM
        </td>
        <td>
            <a href='/Project/Edit/1'><img src='/Content/images/Edit16x16.png' title="Edit"/></a> &nbsp;
            <a href='/Project/Details/1'><img src='/Content/images/Details16x16.png' title="Details" /></a> &nbsp;
            <a href='/Project/Delete/1'><img src='/Content/images/Delete16x16.png' title="Delete" /></a> &nbsp;
            <a href='/Project/Select/1' class="select"  ><img src='/Content/images/Select16x16.png' title="Select"  /></a> 
        </td>
    </tr>
    <tr id='2' class="trow  none ">
        <td>
            Test Project 2
        </td>
        <td>
            Me
        </td>

        <td>
            8/27/2012 5:42:32 PM
        </td>
        <td>
            8/27/2012 5:42:32 PM
        </td>
        <td>
            <a href='/Project/Edit/2'><img src='/Content/images/Edit16x16.png' title="Edit"/></a> &nbsp;
            <a href='/Project/Details/2'><img src='/Content/images/Details16x16.png' title="Details" /></a> &nbsp;
            <a href='/Project/Delete/2'><img src='/Content/images/Delete16x16.png' title="Delete" /></a> &nbsp;
            <a href='/Project/Select/2' class="select"  ><img src='/Content/images/Select16x16.png' title="Select"  /></a> 
        </td>
    </tr>
</table>
Community
  • 1
  • 1
rexdefuror
  • 573
  • 2
  • 13
  • 33
  • Have you made sure there are no script errors in debug console (Internet Explorer = F12 and Chrome = CTRL+SHIFT+J), also try viewing source on your rendered markup and replace your jsFiddle with the produced markup. – Paul Aldred-Bann Aug 28 '12 at 09:48
  • Yeah, I guess you are on to something. Debug console showed this. `Uncaught TypeError: Object [object Object] has no method 'sortElements' ` Also, I've already tried with my markup, works fine. – rexdefuror Aug 28 '12 at 09:56
  • I might add, I'm using latest JQuery. – rexdefuror Aug 28 '12 at 10:03

1 Answers1

0

In that case it sounds like you're missing a reference to the sort library, try adding this before your main script tag with your jQuery usage:

<script type="text/javascript" src="https://raw.github.com/padolsey/jQuery-Plugins/master/sortElements/jquery.sortElements.js"></script>
Paul Aldred-Bann
  • 5,840
  • 4
  • 36
  • 55
  • @user1407758: I think your `thead` and `tbody` might be messing that library up then in your markup, try removing these to reflect the markup you have in your jsFiddle example. Also, can you paste your produced markup into an edit on your question. – Paul Aldred-Bann Aug 28 '12 at 10:23
  • I've provided markup in edit, also I've removed `thead` and `tbody` still nothing. – rexdefuror Aug 28 '12 at 10:37
  • @user1407758: I've just pasted your produced markup and script into your jsFiddle and it works fine... http://jsfiddle.net/gFzCk/868/ - you must have a script error somewhere else on the page. Try `alert`ing when you click on the `` to check your script is getting that far. – Paul Aldred-Bann Aug 28 '12 at 10:41
  • Already did `alert` debugging. Almost everywhere. Will try again I guess. – rexdefuror Aug 28 '12 at 10:43
  • Now it won't initiate alert on click even... I'll redo the script and try again. – rexdefuror Aug 28 '12 at 10:47
  • Ok. Found the error, my `@section` part was bugging it. All works fine now. Thanks :D – rexdefuror Aug 28 '12 at 10:59