tags:

views:

520

answers:

3

I am using Tablesorter on a table which uses links in the first column (of 4). The problem is that in FF and Chrome it orders the first column when clicked by url not the content of the link. For example

<tr><td><a href="http://abc.com"&gt;zzz&lt;/a&gt;&lt;/td&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;22&lt;/td&gt;&lt;td&gt;33&lt;/td&gt;&lt;/tr&gt;
<tr><td><a href="http://cba.com"&gt;aaa&lt;/a&gt;&lt;/td&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;22&lt;/td&gt;&lt;td&gt;33&lt;/td&gt;&lt;/tr&gt;
<tr><td><a href="http://bbb.com"&gt;ccc&lt;/a&gt;&lt;/td&gt;&lt;td&gt;11&lt;/td&gt;&lt;td&gt;22&lt;/td&gt;&lt;td&gt;33&lt;/td&gt;&lt;/tr&gt;

It will order

zzz
ccc
aaa

instead of alphabetical. Is this meant to be the case? Is there a fix anyone can suggest?

Thanks

A: 

I fixed it by inserting a span with style display:none before the link. In the span put the link text.

e.g.

<td><span style="display:none"><%= Html.Encode(item.Name) %></span>
                <a href='<%= Url.Action("Edit", new {id=item.Id}) %>'>
                    <%= Html.Encode(item.Name) %></a>
            </td>
PG
A: 

Thanks a lot! It worked very well

SM
+2  A: 

I have got the same problem. Found solution in the Documentation. Need to add a parser for the links which removes <a> tags from the beginning of the text in a column while sorting.

Here is code which should solve your problem:

 <script type="text/javascript">
    // add parser through the tablesorter addParser method 
    $.tablesorter.addParser({
        // set a unique id 
        id: 'links',
        is: function(s)
        {
            // return false so this parser is not auto detected 
            return false;
        },
        format: function(s)
        {
            // format your data for normalization 
            return s.replace(new RegExp(/<.*?>/),"");
        },
        // set type, either numeric or text
        type: 'text'
    }); 


    // Apply "links" parser to the appropriate column
    $(document).ready(function()
    {
        $("#MyTable").tablesorter({
            headers: {
                0: {
                    sorter: 'links'
                }
            }
    });
</script>
Kirill
Thanks -- this worked perfectly.
Cymen
+1 This is exactly what I need. Thanks!
Peter Stegnar