fnFilter有多个值?

问题描述 投票:0回答:1

我有一个名为“Etiquetas”的列表,我有多个用逗号隔开的标签(例如:“Tag,Tag01,Tag02”)我有一个标签搜索,我把所有标签放在一边,当你点击它,这将被添加到实际搜索中(使用fnFilter),值传递如:'“Tag”,“Tag01”',但如果我过滤“Tag”,过滤器会显示包含该单词的所有行,而不是一个确切的词。 EX:显示带有“Tag”,“tag”和“Tag01”等的行。我需要帮助才能生成精确过滤器和多个值。我添加了一张图片来更好地解释这一点(我很难解释对不起)Image with the actual problem

javascript datatables fnfilter
1个回答
0
投票

您可以利用查找和过滤功能。虽然这不会为您提供排名列表,例如匹配大多数标签的项目,但它仅显示至少具有匹配标签的项目。

这假设标签是作为一个数组给出的,礼仪是一串逗号分隔的标签(它也可以是一个数组,但从我的理解,你提到用逗号分隔,所以我认为它是一个字符串)。

var items = [
  {title: "1", etiquettes: "tag01"},
  {title: "2", etiquettes: "tag,test"}
];

var tags = ["tag"];

var results = items.filter((item) => {
  // splits the tags in the etiquettes, and tries to find at least a matching one. if so, find will return true to the filter function for the current item
  return item.etiquettes.split(",").find((itemTag) => {
    return tags.indexOf(itemTag) >= 0;
  });
});

console.log(results);
© www.soinside.com 2019 - 2024. All rights reserved.