简体   繁体   English

AJAX请求,已过滤JSON响应html / javascript

[英]AJAX request, filtered JSON response html/javascript

when I request a .json containing person info (firstname, lastname) from my node.js server, I would like to filter that data depending on what the user inputs. 当我从node.js服务器请求一个包含人员信息(名字,姓氏)的.json时,我想根据用户输入的内容过滤该数据。

Example: I request the .json from the server, this gives me a list of people, but as I start typing in the text 'form' the list get filtered to match what I typed. 示例:我从服务器请求.json,这给了我一个人的列表,但是当我开始输入文本“ form”时,该列表将被过滤以匹配我键入的内容。

I have some problems with this, first, how can I display this .json as a list? 我对此有一些问题,首先,如何显示此.json作为列表? as of now, I display it like this: 截至目前,我将其显示为:

var parsed = JSON.parse(xmlHttp.responseText);
var html = '';
for (var i = 0; i < parsed.length; i++) {
    html += '<div>' + parsed[i] + '</div>';
}
document.getElementById("myDiv").innerHTML = html;
}

example of .json .json的示例

 "firstName": "John",
 "lastName": "Doe",

I'm sure I can manage the filtering, just need a push with the list! 我确定我可以管理过滤,只需要推送列表即可!

Shoutout to 'Zub' for helping me with the request 向“ Zub”大喊帮助您的请求

Currently you are outputting objects , you need to output the properties on these objects: 当前,您正在输出objects ,您需要输出这些对象的属性:

var parsed = JSON.parse(xmlHttp.responseText);
var html = '';
for (var i = 0; i < parsed.length; i++) {
    html += '<div>' + parsed[i].firstName + '-' + parsed[i].lastName + '</div>';
}
document.getElementById("myDiv").innerHTML = html;
}

As for the filtering, you'd need to attach to an event for the input you want to filter with (example onkeypress). 至于过滤,您需要为要过滤的输入附加一个事件(例如onkeypress)。 In the handler, you should clear out the html, run over the original list again, but put in a condition that only adds to the html when it matches your query. 在处理程序中,您应该清除html,再次在原始列表上运行,但要满足以下条件:仅在与查询匹配时才添加到html。 I'll leave that up to you 我留给你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM