- NOTE! -
-
-
- This parser can not be applied to the original plugin. -
- Set the language to use by adding any of the following (they all do the same thing), set in order of priority (v2.8.2):
-
-
-
- jQuery data
data-ignore-articles="fr"
.
- - metadata
class="{ ignoreArticles : 'fr' }"
. This requires the metadata plugin.
- - headers option
headers : { 0 : { ignoreArticles : 'fr' } }
.
- - header class name
class="ignoreArticles-fr"
.
-
- - jQuery data
- To add a custom language to the parser, use the following code:
-
$.extend( $.tablesorter.ignoreArticles, { + +
++ +Notes
+++ +-
+
- In v2.17.8, added a new option
ignoreArticlesExcept
option which allows ignoring a set pattern.
+ - This parser can not be applied to the original plugin. +
Setup
+++ +-
+
- Set the language (French shown below; English is set by default) to use by adding any of the following (they all do the same thing), set in order of priority (v2.8.2):
+
-
+
- jQuery data
data-ignore-articles="fr"
.
+ - metadata
class="{ ignoreArticles : 'fr' }"
. This requires the metadata plugin.
+ - headers option
headers : { 0 : { ignoreArticles : 'fr' } }
.
+ - header class name
class="ignoreArticles-fr"
.
+
+
+ - jQuery data
- In v2.17.8, you can set up a string to ignore, e.g. keep a title intact like "A to Z", as follows:
+
-
+
- jQuery data
data-ignore-articles-except="a to"
.
+ - metadata
class="{ ignoreArticlesExcept : 'a to' }"
. This requires the metadata plugin.
+ - headers option
headers : { 0 : { ignoreArticlesExcept : 'a to' } }
.
+
+ - jQuery data
- To add more than one title to ignore, separate them using a vertical bar (shift+\)
+
headers: { + 0: { + ignoreArticles : 'en', + // no special parsing for titles like "A to Z" or "A B C" or "A One" + ignoreArticlesExcept : 'a to|a b |a one' + } +}
Note If your title needs a space (e.g. "A B C") make sure it isn't at the end of theignoreArticlesExcept
string; spaces on either end get stripped away. +
+
Customizing
+++ +-
+
- To add a custom language to the parser, use the following code:
+
$.extend( $.tablesorter.ignoreArticles, { "xx" : "a, l'_, de la" }); @@ -66,21 +103,22 @@ $('table').tablesorter({ headers: { 2: { sorter: 'ignoreArticles', - ignoreArticles : 'xx' + ignoreArticles : 'xx', + ignoreArticlesExcept : 'a to' } } });
--
-
- where
xx
is theignoreArticles
id (abbreviation for the language)
- - Each leading article must be separated by a comma and will be case-insensitive. -
- Leading articles will only be removed if they are followed by a space; spaces are automatically included when parsed unless the article definition is followed by an underscore. See the
l'_
note below.
- a
is an example of a leading article which will be removed. So, "A Fistfull of Dollars" will only parse "Fistfull of Dollars" and will sort with the other words that start with an "F".
- l'_
includes an underscore (_
); this tells the parser to not add a space after the word. So a title like "l'enfant" will parse as "enfant".
- de la
is an example showing that the parser can remove a multiple-word article.
-
- - where
- Some languages have been included by default. The information below was obtained from this wikipedia page, so if you discover any errors, or if you want to contribute another language, please open an issue.
-
$.tablesorter.ignoreArticles = { +
-
+
- where
xx
is theignoreArticles
id (abbreviation for the language)
+ - Each leading article must be separated by a comma and will be case-insensitive. +
- Leading articles will only be removed if they are followed by a space; spaces are automatically included when parsed unless the article definition is followed by an underscore. See the
l'_
note below.
+ a
is an example of a leading article which will be removed. So, "A Fistfull of Dollars" will only parse "Fistfull of Dollars" and will sort with the other words that start with an "F".
+ l'_
includes an underscore (_
); this tells the parser to not add a space after the word. So a title like "l'enfant" will parse as "enfant".
+ de la
is an example showing that the parser can remove a multiple-word article.
+
+ - where
- Some languages have been included by default. The information below was obtained from this wikipedia page, so if you discover any errors, or if you want to contribute another language, please open an issue.
+
$.tablesorter.ignoreArticles = { "en" : "the, a, an", "de" : "der, die, das, des, dem, den, ein, eine, einer, eines, einem, einen", "nl" : "de, het, de, een", @@ -90,8 +128,10 @@ $('table').tablesorter({ "it" : "il, lo, la, l'_, i, gli, le, un', uno, una, un", "hu" : "a, az, egy" };
-
+ +
Demo
@@ -379,7 +419,8 @@ $(function() { headers: { 2: { sorter: 'ignoreArticles', - ignoreArticles : 'fr' + ignoreArticles : 'fr', + ignoreArticlesExcept : '' } } }); diff --git a/js/parsers/parser-ignore-articles.js b/js/parsers/parser-ignore-articles.js index d8429807..e84ed0d0 100644 --- a/js/parsers/parser-ignore-articles.js +++ b/js/parsers/parser-ignore-articles.js @@ -7,8 +7,10 @@ ;(function($){ "use strict"; +var ts = $.tablesorter; + // basic list from http://en.wikipedia.org/wiki/Article_%28grammar%29 - $.tablesorter.ignoreArticles = { + ts.ignoreArticles = { "en" : "the, a, an", "de" : "der, die, das, des, dem, den, ein, eine, einer, eines, einem, einen", "nl" : "de, het, de, een", @@ -24,22 +26,34 @@ // and then set the language id 'xx' in the headers option // ignoreArticles : 'xx' - $.tablesorter.addParser({ + ts.addParser({ id: 'ignoreArticles', is: function() { return false; }, format: function(s, table, cell, cellIndex) { - var c = table.config, art, lang; + var art, ignore, lang, + c = table.config, + str = s || ''; if ( !(c.headers && c.headers[cellIndex] && c.headers[cellIndex].ignoreArticlesRegex) ) { - // initialize - save regex in c.headers[cellIndex].ignoreArticles + // initialize - save regex in c.headers[cellIndex].ignoreArticlesRegex if (!c.headers) { c.headers = {}; } if (!c.headers[cellIndex]) { c.headers[cellIndex] = {}; } - lang = $.tablesorter.getData(c.$headers.eq(cellIndex), c.headers[cellIndex], 'ignoreArticles'); - art = ($.tablesorter.ignoreArticles[lang] || "the, a, an" ) + ""; + lang = ts.getData( c.$headers.eq(cellIndex), ts.getColumnData( table, c.headers, cellIndex ), 'ignoreArticles' ); + art = (ts.ignoreArticles[lang] || "the, a, an" ) + ""; c.headers[cellIndex].ignoreArticlesRegex = new RegExp('^(' + $.trim( art.split(/\s*\,\s*/).join('\\s|') + "\\s" ).replace("_\\s","") + ')', 'i'); + // exception regex stored in c.headers[cellIndex].ignoreArticlesRegex2 + ignore = ts.getData( c.$headers.eq(cellIndex), ts.getColumnData( table, c.headers, cellIndex ), 'ignoreArticlesExcept' ); + c.headers[cellIndex].ignoreArticlesRegex2 = ignore !== '' ? new RegExp('^(' + ignore.replace(/\s/g, "\\s") + ')', 'i') : ''; } - return (s || '').replace(c.headers[cellIndex].ignoreArticlesRegex, ''); + art = c.headers[cellIndex].ignoreArticlesRegex; + if (art.test(str)) { + ignore = c.headers[cellIndex].ignoreArticlesRegex2; + if ( !(ignore && ignore.test(str)) ) { + return str.replace(art, ''); + } + } + return str; }, type: 'text' }); - In v2.17.8, added a new option