javascript - 如何从 json 中搜索文本并以 html 显示?
问题描述
我从 json 结果中列出项目名称并显示在侧栏中。当单击列出任何项目名称的侧栏时,它会显示它的详细信息。现在我有搜索框来搜索项目并显示项目详细信息并在侧栏中获取选定的项目名称。
这是搜索框代码:
<input class="form-control form-control-dark w-100" type="text" id="text" placeholder="Search" aria-label="Search">
这是json的结果:
"projects": [
{
"instances": null,
"name": "decodingideas",
"projectid": "decodingideas-147616",
"projectnumber": 334691107943,
"orgid": "",
"orgname": "",
"parentid": "",
"parenttype": ""
},
{
"instances": null,
"name": "pupil-workers",
"projectid": "pupil-workers",
"projectnumber": 455648594684,
"orgid": "",
"orgname": "",
"parentid": "",
"parenttype": ""
}
在这里,我将搜索 projectid、名称或实例等。
这是html中使用的代码:
<div class="container-fluid">
<div class="row">
<nav class="col-md-2 d-none d-md-block bg-light sidebar">
<div id="projectlist" class="sidebar-sticky">
<ul class="nav flex-column nav-pills">
{{range .Projects}}
<li class="nav-item" >
<a name="{{.ProjectID}}" class="nav-link" href="#">
<img class="img-fluid" style="width:8%"
src="static/image/generic_gcp.png">
{{.Name}}
<div>
<small>
ProjectId: {{.ProjectID}}
</small>
</div>
</a>
</li>
{{end}}
</ul>
<!-- Might need this seperator
<h6 class="sidebar-heading d-flex justify-content-between align-items-
center px-3 mt-4 mb-1 text-muted">
<span>Savings Reports</span>
</h6>
-->
</div>
</nav>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
{{range .Projects}}
<div id={{.ProjectID}} class="d-none justify-content-between flex-wrap
flex-md-nowrap align-items-center pb-2 mb-3 border-bottom ">
<h1 class="h2">Project:{{.Name}}</h1>
如何列出搜索和显示项目详细信息并在该项目名称的侧栏中被选中。
解决方案
这是来自 Jquery ui 的自动完成:
$( function() {
var projects= [
{
"instances": null,
"name": "decodingideas",
"projectid": "decodingideas-147616",
"projectnumber": 334691107943,
"orgid": "",
"orgname": "",
"parentid": "",
"parenttype": ""
},
{
"instances": null,
"name": "pupil-workers",
"projectid": "pupil-workers",
"projectnumber": 455648594684,
"orgid": "",
"orgname": "",
"parentid": "",
"parenttype": ""
}
];
$( "#projects" ).autocomplete({
source: function (request, response) {
response($.map(projects, function (value, key) {
return {
label: value.name+" "+ value.projectid,
value: value.projectid
}
}));
},
select: function(event, ui) {
var res= $('#projects').val(ui.item.projectid);
}
});
} );
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
SEarch: <input id="projects">
var url="";//your url to json file
source: function(request,response)
{
$.get(url, function(data)
{
obj = JSON.parse(data); //parse the data in JSON (if not already)
response($.map(obj, function(value, key)
{
return {
label: value.name+" "+ value.projectid,
value: value.projectid
}
}));
}
}
推荐阅读
- react-native - 插值不起作用 - 抽屉导航
- python-3.x - 新文档未添加到 Elastic Search 中的现有索引中
- python - 错误:使用 docker start 容器时无法 chdir 到 'app'
- javascript - `this.prop` 在对象内部给出未定义?
- node.js - Docker-compose:docker 卷中不存在 node_modules
- dolphindb - 为表中的指定列分配新值
- python - 使用 GstRtspServer 将 MJPEG 流转换为 RTSP
- asp.net-core-mvc - 已解决 - 如何在 ASP.NET Core MVC 中使用脚手架?我收到一个错误
- javascript - 从 Strophe.JS(通过 Webscokets)与 ejabberd 服务器连接说“连接...失败”
- python-3.x - 由于 Python3,pandas 中的 set_values 和 at() 没有属性“set_values”或“At_indexer”错误