簡體   English   中英

JSON和jQuery Live搜索

[英]JSON and jQuery Live Search

我為基於在線商店構建的基於jQuery / JSON的實時搜索在這個問題上困擾了幾天。 使用Tipue Drop開源,我可以在使用他們的測試JSON時獲得結果,但似乎無法讀取它。 這里有一個JSON文件,正在使用下面的javascript嘗試使用Tipue Drop讀取“名稱”和“描述”字段。 我如何解決此問題的任何建議,不勝感激!

 /* Tipue drop 5.0.2 Copyright (c) 2015 Tipue Tipue drop is released under the MIT License http://www.tipue.com/drop */ (function($) { $.fn.tipuedrop = function(options) { var set = $.extend( { 'show' : 3, 'speed' : 300, 'newWindow' : false, 'mode' : 'static', 'contentLocation' : 'tipuedrop/tipuedrop_content.json' }, options); return this.each(function() { var tipuedrop_in = { pages: [] }; $.ajaxSetup({ async: false }); if (set.mode == 'json') { $.getJSON(set.contentLocation) .done(function(json) { tipuedrop_in = $.extend({}, json); }); } if (set.mode == 'static') { tipuedrop_in = $.extend({}, tipuedrop); } $(this).keyup(function(event) { getTipuedrop($(this)); }); function getTipuedrop($obj) { if ($obj.val()) { var c = 0; for (var i = 0; i < tipuedrop_in.pages.length; i++) { var pat = new RegExp($obj.val(), 'i'); if ((tipuedrop_in.pages[i].name.search(pat) != -1 || tipuedrop_in.pages[i].description.search(pat) != -1) && c < set.show) { if (c == 0) { var out = '<div class="tipue_drop_box"><div id="tipue_drop_wrapper">'; } out += '<a href="' + tipuedrop_in.pages[i].id + '"'; if (set.newWindow) { out += ' target="_blank"'; } out += '><div class="tipue_drop_item"><div class="tipue_drop_left"><img src="' + tipuedrop_in.pages[i].id + '" class="tipue_drop_image"></div><div class="tipue_drop_right">' + tipuedrop_in.pages[i].name + '</div></div></a>'; c++; } } if (c != 0) { out += '</div></div>'; $('#tipue_drop_content').html(out); $('#tipue_drop_content').fadeIn(set.speed); } } else { $('#tipue_drop_content').fadeOut(set.speed); } } $('html').click(function() { $('#tipue_drop_content').fadeOut(set.speed); }); }); }; })(jQuery); 
 <script> $(document).ready(function() { $('#tipue_drop_input').tipuedrop({ 'mode': 'json', 'contentLocation': 'http://barbarostudios.com/clientpreviews/lungavita2/api/search.php' }); }); </script> 

由於某些項目的索引未定義,在我看來這仍然是JavaScript錯誤。 其中一個沒有名稱(dae04696-2acb-4972-a471-1b873e2a8d4f),並且一大堆都沒有描述:

159e522f-10fd-4577-ad3b-866da83d0054
19b63864-9566-4264-9510-821c9d020f74
2417fcae-7ce2-4539-a334-c88e0a81b8a9
26a014ac-b23b-4bc2-9b19-b754e875b5f1
2a9f8d42-0dbc-470a-a59b-4cf1aff0a0ed
38c389e2-6926-425d-baa0-0760d63926c1
3dc823a7-9bac-4290-9aae-757480ffe5c1
3e2fbcec-e35d-4f75-94c3-e4de71f4b428
4aba2175-e1cc-4e12-9135-9cec3e4f8914
54b8cba1-3c4b-4bf5-bf6c-b7bd1cfb25b4
5b2867c1-d077-4deb-9d8e-890d3afd5163
5b2d8d5a-1d94-499c-9205-40888472a3f3
5b7dd41a-59cd-49d1-a06f-a9b2a90e516d
5bf00622-33e1-4bed-a00b-0f14826cd2b7
6164bb51-0d0e-4eb1-81bb-cef64bca56cb
6376edc3-faba-4018-8bc2-8b3e0d9fcb48
6d618850-dca0-495f-92ed-f8b86578b063
740b265a-fd38-4bcf-8e68-ab14e8266543
79728b83-6365-4a00-8634-10656081a574
7b093823-599e-481c-8cbe-f1cfab3674e6
7ec54482-4e6e-470b-ac19-6b9cee2b7db3
9552f568-b7bd-44f2-b144-f3773ae0d097
9860c5a8-2a04-4176-95bc-762895a077e3
9a418680-cb24-4f2c-a56f-cb555edd9db4
9cad6135-3d87-4fda-988d-9ec3f7958f31
a676471c-8633-4bd4-a801-3066d59c2c00
ac20c6f3-0627-4b7b-85cb-7960c0935d3c
b09a2984-a868-47e4-84de-177572fd2b16
bc7b3be8-c388-4e1a-a884-8a899ff13889
bcff6f69-eb86-468d-be40-90e21f477f56
beddc257-4490-41e4-ba1d-088f2bd75108
c035feaa-23ae-4e60-948d-8e8fd9b6d314
c5f2083a-c5b0-4c98-934c-f7f006230285
d4ddcb79-3e0e-4958-a2de-260c05d0596c
d5447be8-8b92-473e-9df0-7d0e1a0b6ed1
d7e25d21-3e8a-4ddc-832b-e5a7939b1e75
dae04696-2acb-4972-a471-1b873e2a8d4f
e08cb73e-1060-4a28-abe0-2beba19f8407
fb464d67-7406-48d7-98d9-2c094f219079

這會導致tipuedrop在循環瀏覽頁面時失敗,並停止處理腳本的其余部分(因為您正在專門查看name和description屬性)。

如果您的PHP頁面正在從JSON文件中提取JSON,則可以使用它來靜態修改JSON文件以對其進行修復:

<?php
    $json = json_decode(file_get_contents('json.json'));
    foreach($json->pages as &$page){
        if(!isset($page->description)){
            $page->description = "";
        }
        if(!isset($page->name)){
            $page->name = "";
        }
    }
    file_put_contents('json.json', json_encode($json));
?>

另外,如果您的PHP頁面將信息拉到其他位置(txt文件或數據庫或其他內容),則可以對其進行更改,以便為每個沒有該設置的元素動態添加一個描述和名稱(這是幾乎相同的代碼,只是修改了一個小),然后回顯動態更改的代碼。

這是對我有用的實時搜索方法。

index.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Live Search</title>
<link rel="stylesheet" href="mystyle.css" />
</head>
<body>
<div id="searcharea">
<label for="search">live search</label>
<p>Enter the name or info about a speaker</p>
<input type="search" name="search" id="search" placeholder="name or info" />
</div>
<div id="update"></div>
<script src="jquery.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</body>
</html>

Script.js

$('#search').keyup(function(){
var searchField = $('#search').val();
var myExp = new RegExp(searchField, 'i');
$.getJSON('data.json', function(data){
var output = '<ul class="searchresult">';
$.each(data, function(key, val){
if((val.name.search(myExp) != -1) || (val.bio.search(myExp) != -1)) {
output +='<li>';
output +='<h2>' + val.name + '</h2>';
output +='<img src="images/' + val.shortname + '_tn.jpg" alt="'+ val.name +'" />';
output +='<p>' + val.bio + '</p>';
output +='</li>';
}
});
output += '</ul>';
$('#update').html(output);
});
});

mystyle.css

body {
background: #DAD7C7;
}

#searcharea {
margin: 0 auto;
text-align: center;
background: #BF996B;
padding: 10px;
width: 30%;
-webkit-border-radius: 10px 10px 0 0;
-moz-border-radius: 10px 10px 0 0;
border-radius: 10px 10px 0 0;
}

#searcharea label {
font: bold 1.3em Arial;
text-transform: uppercase;
padding-bottom: 5px;
color: #A61C1C;
}

#searcharea p {
margin: 0;
line-height: 1em;
padding-bottom: 5px;
}

#searcharea input {
width: 80%;
text-align: center;
}

#update {
font-family: Georgia, "Times New Roman", Times, serif;
width: 70%;
margin: 0 auto;
border-top: 1px dotted #CCC;
}

#update ul {
list-style: none;
margin: 0;
padding: 0;
}

#update ul li {
width: 100%;
padding: 0 20px;
background: #EEE;
padding-bottom: 10px;
height: 110px;
overflow: hidden;
border-bottom: 1px dotted #CCC;
-webkit-animation: myanim 1s;
-moz-appearance-animation: myanim 1s;
-o-animation: myanim 1s;
animation: myanim 1s;

-webkit-transition: height 0.3s ease-out;
-moz-appearance-transition: height 0.3s ease-out;
-o-transition: height 0.3s ease-out;
transition: height 0.3s ease-out;
}

#update li:hover {
background: #FFFCE5;
min-height: 110px;
height: 250px;
overflow: visible;
}

#update h2 {
margin: 0;
padding: 0;
font-size: 1.2em;
padding-bottom: 5px;
padding-top: 20px;
font-family: Arial, Helvetica, sans-serif;
color: #BF5841;
border-bottom: 1px dotted #CCC;
margin-bottom: 10px;
}

#update img {
float: left;
width: 80px;
margin-right: 10px;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
}

@-webkit-keyframes myanim {
0% {
opacity: 0.3;
}
100% {
opacity: 1.0;
}
}
@-o-keyframes myanim {
0% {
opacity: 0.3;
}
100% {
opacity: 1.0;
}
}

@-moz-keyframes myanim {
0% {
opacity: 0.3;
}
100% {
opacity: 1.0;
}
}

@keyframes myanim {
0% {
opacity: 0.3;
}
100% {
opacity: 1.0;
}
}

data.json

[
{
"name":"Barot Bellingham",
"shortname":"Barot_Bellingham",
"reknown":"Royal Academy of Painting and Sculpture",
"bio":"Barot has just finished his final year at The Royal Academy of Painting and Sculpture, where he excelled in glass etching paintings and portraiture. Hailed as one of the most diverse artists of his generation, Barot is equally as skilled with watercolors as he is with oils, and is just as well-balanced in different subject areas. Barot's collection entitled "The Un-Collection" will adorn the walls of Gilbert Hall, depicting his range of skills and sensibilities - all of them, uniquely Barot, yet undeniably different"
},
{
"name":"Jonathan G. Ferrar II",
"shortname":"Jonathan_Ferrar",
"reknown":"Artist to Watch in 2012",
"bio":"The Artist to Watch in 2012 by the London Review, Johnathan has already sold one of the highest priced-commissions paid to an art student, ever on record. The piece, entitled Gratitude Resort, a work in oil and mixed media, was sold for $750,000 and Jonathan donated all the proceeds to Art for Peace, an organization that provides college art scholarships for creative children in developing nations"
},
{
"name":"Hillary Hewitt Goldwynn-Post",
"shortname":"Hillary_Goldwynn",
"reknown":"New York University",
"bio":"Hillary is a sophomore art sculpture student at New York University, and has already won all the major international prizes for new sculptors, including the Divinity Circle, the International Sculptor's Medal, and the Academy of Paris Award. Hillary's CAC exhibit features 25 abstract watercolor paintings that contain only water images including waves, deep sea, and river."
},
{
"name":"Hassum Harrod",
"shortname":"Hassum_Harrod",
"reknown":"Art College in New Dehli",
"bio":"The Art College in New Dehli has sponsored Hassum on scholarship for his entire undergraduate career at the university, seeing great promise in his contemporary paintings of landscapes - that use equal parts muted and vibrant tones, and are almost a contradiction in art. Hassum will be speaking on "The use and absence of color in modern art" during Thursday's agenda."
},
{
"name":"Jennifer Jerome",
"shortname":"Jennifer_Jerome",
"reknown":"New Orleans, LA",
"bio":"A native of New Orleans, much of Jennifer's work has centered around abstract images that depict flooding and rebuilding, having grown up as a teenager in the post-flood years. Despite the sadness of devastation and lives lost, Jennifer's work also depicts the hope and togetherness of a community that has persevered. Jennifer's exhibit will be discussed during Tuesday's Water in Art theme."
},
{
"name":"LaVonne L. LaRue",
"shortname":"LaVonne_LaRue",
"reknown":"Chicago, IL",
"bio":"LaVonne's giant-sized paintings all around Chicago tell the story of love, nature, and conservation - themes that are central to her heart. LaVonne will share her love and skill of graffiti art on Monday's schedule, as she starts the painting of a 20-foot high wall in the Rousseau Room of Hotel Contempo in front of a standing-room only audience in Art in Unexpected Places."
},
{
"name":"Constance Olivia Smith",
"shortname":"Constance_Smith",
"reknown":"Fullerton-Brighton-Norwell Award",
"bio":"Constance received the Fullerton-Brighton-Norwell Award for Modern Art for her mixed-media image of a tree of life, with jewel-adorned branches depicting the arms of humanity, and precious gemstone-decorated leaves representing the spouting buds of togetherness. The daughter of a New York jeweler, Constance has been salvaging the discarded remnants of her father's jewelry-making since she was five years old, and won the New York State Fair grand prize at the age of 8 years old for a gem-adorned painting of the Manhattan Bridge."
},
{
"name":"Riley Rudolph Rewington",
"shortname":"Riley_Rewington",
"reknown":"Roux Academy of Art, Media, and Design",
"bio":"A first-year student at the Roux Academy of Art, Media, and Design, Riley is already changing the face of modern art at the university. Riley's exquisite abstract pieces have no intention of ever being understood, but instead beg the viewer to dream, create, pretend, and envision with their mind's eye. Riley will be speaking on the "Art of Abstract" during Thursday's schedule"
},
{
"name":"Xhou Ta",
"shortname":"Xhou_Ta",
"reknown":"China International Art University",
"bio":"A senior at the China International Art University, Xhou has become well-known for his miniature sculptures, often the size of a rice granule, that are displayed by rear projection of microscope images on canvas. Xhou will discuss the art and science behind his incredibly detailed works of art."
}
]

您可以在此處查看演示。


解決錯誤的一種方法是在嘗試獲取其偏移量之前檢查jQuery對象是否包含任何元素:

例:

var nav = $('.content-nav');
if (nav.length) {
  var contentNav = nav.offset().top;
  ...continue to set up the menu
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM