简体   繁体   中英

jQuery dataTables plugin - how to ignore word from column sort

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.

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
],

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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM