3

我使用 tablesorter characterEquivalents 扩展,如http://mottie.github.io/tablesorter/docs/example-locale-sort.html中所述。

我为捷克字符准备了类似的扩展,但排序不适用于某些字符 - fe \u017d

$.extend( $.tablesorter.characterEquivalents, {
"a" : "\u00e1", // á
"A" : "\u00c1", // Á
"c" : "\u010d", // č
"C" : "\u010c", // Č
"d" : "\u010f", // ď
"D" : "\u010e", // Ď
"e" : "\u00e9\u011b", // éě
"E" : "\u00c9\u011a", // ÉĚ
"i" : "\u00ed", // í
"I" : "\u00cd", // Í
"n" : "\u0148", // ň
"N" : "\u0147", // Ň
"o" : "\u00f3", // ó
"O" : "\u00d3", // Ó    
"r" : "\u0159", // ř
"R" : "\u0158", // Ř
"s" : "\u0161", // š
"Š" : "\u0160", // Š
"t" : "\u0165", // ť
"T" : "\u0164", // Ť        
"u" : "\u00fa\u016f", // úů
"U" : "\u00da\u016e", // ÚŮ
"y" : "\u00fd", // ý
"Y" : "\u00dd", // Ý    
"z" : "\u017e", // ž
"Z" : "\u017d" // Ž
}); 

在此处的示例http://jsfiddle.net/Gk43v/18/中存在ŽZ之前的问题,这是错误的。

但是在我的页面上是 Ž 在表格中间,这是完全错误的。

4

1 回答 1

1

这是工作。内部缓存中的“z”已被替换。

尽管您可能需要包含一个true标志来执行深度扩展:

$.extend( true, $.tablesorter.characterEquivalents, { ... });

查看此演示(排序以查看 firebug 窗口中的列值):http: //jsfiddle.net/Gk43v/19/


更新:好的,问题似乎是实际的排序顺序。问题是默认排序是使用字符的 ASCII 值完成的,因此 Š 和 Ž 在 AZ 之后排序,没有任何字符等效替换。该函数将“Š”替换为“S”,将“Ž”替换为“Z”,使它们与未重读的字母等效且无法区分。

如果您确实希望排序保持字符顺序,则需要使用不同的文本排序器,例如Sugarjs,它允许您设置排序顺序:

Array.AlphanumericSortOrder = 'AaÁáBbCcDdÐðEeÉéĘęFfGgHhIiÍíJjKkLlMmNnOoÓóPpQqRrSsTtUuÚúVvWwXxYyÝýZzÞþÆæÖö';

然后,您可以使用该textSorter选项对该列使用糖数组排序 -这是一个显示冰岛排序的演示

$("table").tablesorter({
  theme : 'blue',
  ignoreCase : false,
  textSorter : {
    // alphanumeric sort from sugar (http://sugarjs.com/arrays#sorting)
    // for the first column (zero-based index)
    0 : Array.AlphanumericSort
  }
});

更新 #2:因为捷克字母有点复杂,所以您需要将“CH”替换为占位符,因为 Sugar 在排序顺序定义中只允许单个字符。

所以在这个例子中,我用“Æ”替换了“CH”(更新了演示

$(function () {
    Array.AlphanumericSortOrder = 'AaÁáÄäBbCcČčDdĎďEeÉéĚěFfGgHhÆæIiÍíJjKkLlMmNnŇňOoÓóÖöPpQqRrŘřSsŠšTtŤťUuÚúŮůÜüVvWwXxYyÝýZzŽž';
    Array.AlphanumericSortIgnoreCase = true;
    // see https://github.com/andrewplummer/Sugar/issues/382#issuecomment-41526957
    Array.AlphanumericSortEquivalents = {};

    // replace "Ch" and "ch" with a placeholder... it can be anything
    // in this example, I'm replacing ch with "æ" and Ch or CH with "Æ"
    // these characters have been added to the Array.AlphanumericSortOrder
    // between "h" and "I" - according to http://en.wikipedia.org/wiki/Czech_orthography
    var replaceCH = function( node ) {
        return $(node).text()
            .replace(/(Ch|CH)/g, '\u00c6')
            .replace(/ch/g, '\u00e6');
    };

    $("table").tablesorter({
        theme: 'blue',
        // table = table object; get config options from table.config
        // column is the column index (zero-based)
        ignoreCase: false,
        textExtraction : {
            1: replaceCH,
            3: replaceCH
        },
        textSorter: {
            1 : Array.AlphanumericSort, // alphanumeric sort from sugar (http://sugarjs.com/arrays#sorting)
            3 : Array.AlphanumericSort
        }
    });
});

我确实尝试使用该characterEquivalents函数来替换字符串,但它目前也只支持单个字符替换(我将在未来的版本中解决这个问题),所以现在我使用的是自定义textExtraction函数。

您报告的第二个问题可以通过在 ajax 调用完成并呈现表后触发表上的“更新”方法来解决。

$('table').trigger('update');
于 2015-03-15T21:04:00.373 回答