简体   繁体   English

jQuery dataTables插件-如何从列排序中忽略单词

[英]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." 在书名栏下,许多书的书名都以“ 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? 我如何在排序此列时设置jquery datatables插件中的排序以忽略第一个单词(如果它是“ The”)?

Thanks. 谢谢。

Maybe you can use something like this: 也许您可以使用如下所示的内容:

First you define a custom sorting type for your column for example "SongTitle". 首先,您为列定义自定义排序类型,例如“ 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) 记住要定义相反的函数(这是用于升序的“ asc”顺序)

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

"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. 编写标题jquery渲染器将文章(The,A,An)移动到字符串末尾可能会更容易。

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". 您可能想要对以“ A”和“ An”开头的标题执行相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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