所以我有一些JSON数据做了一个表...使用JQuery制作搜索过滤器?
{
"AKH":{
"name": "Amonkhet",
"code": "AKH"
"cards": [
{
"artist": "Izzy",
"cmc": 3,
"colorIdentity": [
"W"
],
"colors": [
"White"
],
"id": "df3a6e0336684c901358f3ff53ec82ff5d7cdb9d",
"imageName": "gideon of the trials",
"layout": "normal",
"loyalty": 3,
"manaCost": "{1}{W}{W}",
"multiverseid": 426716,
"name": "Gideon of the Trials",
"number": "14",
"rarity": "Mythic Rare",
"subtypes": [
"Gideon"
],
"text": "+1: Until your next turn, prevent all damage target permanent would deal.\n0: Until end of turn, Gideon of the Trials becomes a 4/4 Human Soldier creature with indestructible that's still a planeswalker. Prevent all damage that would be dealt to him this turn.\n0: You get an emblem with \"As long as you control a Gideon planeswalker, you can't lose the game and your opponents can't win the game.\"",
"type": "Planeswalker — Gideon",
"types": [
"Planeswalker"
]
},
表行最终看起来像这样为每个卡。此刻我只给每行添加ID,卡片名称和法力消耗
<td><a href="#" onclick="showInfo(this.id)"
id="df3a6e0336684c901358f3ff53ec82ff5d7cdb9d">Gideon of the Trials</a></td>
现在我想通过这些卡进行搜索。 (请记住,有超过17,000张不同的卡片会出现在这个列表中)我可以找到它。但是我遇到了几个不同的问题...要么找到它们,要么隐藏其余的东西或者它隐藏了整个列表,只显示找到的一张卡片。
所以问题A ...我错过了什么让搜索工作正常?
$(document).on('change', 'input[type=checkbox]', function() {
var lis = $('.cardsRow')
$('input[type=checkbox]').filter(':checked').each(function(){
filterKeyB = $(this).attr('id')
filterKeyA = $(this).attr('name')
$.each(json, function(setCode, setListing) {
$.each(setListing.cards,function(cardNum, cardListing){
var x = Object.keys(cardListing)
var y = Object.keys(cardListing).map(function (key){
return cardListing[key]
})
for (i = 0; (i < x.length); i++) {
if(x[i] === filterKeyA){
if (y[i] instanceof Array){
var holder = y[i]
var valueArr =[]
for(var k = 0; k < holder.length; k++){
valueArr = holder.join('|').toLowerCase().split('|')
var foundIt = valueArr.includes(filterKeyB)
}
}else{
var stringy = y[i]
var stringyA= stringy.toLowerCase().replace(/\s/g, '')
if (stringyA === filterKeyB){
var foundIt = true
}
}
if(foundIt === true){
$winner = cardListing.name
for (k = 0; (k < lis.length); k++){
if (lis[k].innerText.indexOf($winner) != -1) {
$(lis[k]).show()
}
}
}
}
}
})
问题B ...因为您已经在这里......将搜索数据附加到元素本身会是更好的做法吗?也许只是搜索次数最多的(如名称和法力),并有更先进的查询再次通过数据?
你可以做的第一件事是正确地缩进你的代码?目前它是不可读的。 – Tomalak
对于搜索过滤器,如果您有兴趣,可以使用'lodash'库轻松过滤数据。 https://lodash.com/docs/4.17.4#filter –
对于问题B,我会说是。只是好奇,你在你的应用程序中使用任何服务器端代码?在你的服务器上使用ajax进行搜索会更好也更容易 – Silencer310