Wednesday, November 18, 2009

[jQuery] Re: Table sorter plugin not properly sorting Distance column (ie: 17 Miles)

Thanks so much! Here is my final version of the script:

<script type="text/javascript">
$(document).ready(function() {

$.tablesorter.addParser({
id: 'distance',
is: function(s) {
return false;
},
format: function(s) {
var v = "" + s; //make sure we're working with a
string
v = v.substr(0, v.indexOf(" "));
return Number(v);
},
type: 'numeric'
});

$("#locationsTable").tablesorter( {sortList: [[1,0]],
headers: {
0: { sorter: false },
1: { sorter:'distance' },
2: { sorter: false },
3: { sorter: false }
}
}
);
});
</script>

On Nov 17, 2:32 pm, MorningZ <morni...@gmail.com> wrote:
> First, understand *why* it doesn't work:
>
> - You add "miles" to the table cell, then Tablesorter treats it as a
> string value
>
> Now that this is understoodd, the solution is to use a custom parser
> to
> strip out the label, here is an example
>
> http://jsbin.com/unepe/edit
>
> On Nov 17, 11:17 am, Yvan <yvana...@gmail.com> wrote:
>
> > I'm using the tableshorter plugin on a table which lists multiple
> > locations, and that contains a "Distance" column which displays
> > distances in the format of "17 Miles" and "5 miles", etc.
>
> > I have the following javascript in my head section, which defines the
> > sort order of the 1 column that I want to have sortable (and that
> > specifies which columns I do not want to make sortable):
>
> >         <script type="text/javascript">
> >                 $(document).ready(function() {
> >                         $("#locationsTable").tablesorter( {sortList:
> > [[1,0]],
> >                         headers: {
> >                         0: { sorter: false },
> >                         2: { sorter: false },
> >                         3: { sorter: false }
> >                                         }
> >                                 }
> >                         );
> >                 });
> >         </script>
>
> > I'm finding that I am not able to properly sort the distance column
> > when I include the word "Miles" in that table cell.  It's only when I
> > remove the word "Miles" from the table cell that the column sorts
> > properly.
>
> > How can I fix this?
>
> > Thanks,
> > - Yvan

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate