views:

66

answers:

2

I have a table of books with titles, authors, publishers, and dates in it. Under the title column a lot of the book titles start with the word "The." How can I setup the sort within the jquery datatables plugin to ignore the first word if it is "The" when sorting this column?

Thanks.

A: 

It would probably be easier to write a title jquery renderer that moves articles (The, A, An) to the end of the string.

The Sound of Music -> Sound of Music, The

You'd probably want to do the same thing for titles that begin with "A" and "An".

Gilbert Le Blanc
how would you go about this? did some searches and couldn't find any examples.
Jeffrey
I'm not a jquery wizard, but JavaScript has an indexOf method. If the first characters are an article, substring the rest of the title string, and add the article to the end of the new title string.
Gilbert Le Blanc
A: 

Maybe you can use something like this:

First you define a custom sorting type for your column for example "SongTitle". With datatables you can define new sorting type by specifying the comparison function:

$.fn.dataTableExt.oSort['SongTitle-asc']  = function(a,b) {
    // Modify your title a and your title b by putting "The" in the end
    // Return 1 if a > b
    // Return -1 if a < b
    // Return 0 if a == b
}

Remember to define the opposite function (this was for Ascending "asc" order)

$.fn.dataTableExt.oSort['SongTitle-desc']  = function(a,b) {
    return -1 * $.fn.dataTableExt.oSort['SongTitle-asc'](a,b);
}

Now to tell DataTables to use your sorting you pass the new value to aoColumns

"aoColumns": [
    { "sType": "SongTitle" },    // Title
    { "sType": "html" }          // for the next column
],
wezzy