gpt4 book ai didi

javascript - Tablesorter:对捷克字母表进行排序

转载 作者:行者123 更新时间:2023-11-29 15:36:02 25 4
gpt4 key购买 nike

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

我为捷克字符准备了类似的扩展,但排序对某些字符不起作用 - f。 e. \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 之前,这是错误的。

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

最佳答案

它正在工作。 “z”已在内部缓存中被替换。

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

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

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


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

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

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

然后您可以使用 textSorter option对该列使用 sugar 数组排序 - here is a demo showing an Icelandic sort

$("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”替换为“Æ”(updated demo)

$(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 函数。

您报告的第二个问题可以通过触发 "update" method 来解决。在 ajax 调用完成并呈现表格后在表格上。

$('table').trigger('update');

关于javascript - Tablesorter:对捷克字母表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com