gpt4 book ai didi

javascript - 使用 Handsontable 在同一列中对纯文本和 html 进行排序问题

转载 作者:行者123 更新时间:2023-11-29 15:35:46 24 4
gpt4 key购买 nike

我在对包含纯文本和 html(如 anchor 标记)的列进行排序时遇到问题。排序似乎发生在存储在数据源数组中的值而不是显示的值上。包含 anchor 标记的单元格始终排在纯文本单元格之上。

下面是一个带有示例的 JSFiddle。我想要的是第三列按字母顺序对名称进行排序,而不管单元格是否包含 anchor 标记或纯文本。

示例: http://jsfiddle.net/5sL2jkqt/

{ data: 2, renderer: "html" }

我尝试在第三列应用自定义单元格渲染器,但这没有帮助。非常感谢任何有关如何解决此问题的建议!

最佳答案

我为解决这个问题所做的是将以下函数添加到 handsontable.full.js

  // Strip out all tags not in the allowed parameter
function stripTags(input, allowed) {

if (!input) {
return "";
} else if (!isNaN(input)) {
return input;
}

// making sure the allowed arg is a string containing only tags in lowercase (<a><c>)
allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join("");


var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;


return input.replace(commentsAndPhpTags, "").replace(tags, function ($0, $1) {

return allowed.indexOf("<" + $1.toLowerCase() + ">") > -1 ? $0 : "";

});


};

然后我调用了原生 handsontable 函数 this.sort 中的 stripTags 函数,它将您要排序的内容添加到数组 this.sortIndex 中,如下所示:

this.sortIndex.push([i, stripTags(instance.getDataAtCell(i, this.sortColumn + colOffset), "")]);

虽然不是 100% 理想,但效果很好。它去除 HTML 并根据剩下的内容进行排序。

也可以在 GitHub 上找到

关于javascript - 使用 Handsontable 在同一列中对纯文本和 html 进行排序问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29394806/

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