【问题标题】:How to represent and autocomplete JSON of suggestions based on Google Books API?如何表示和自动完成基于 Google Books API 的建议 JSON?
【发布时间】:2015-08-14 13:18:43
【问题描述】:

本题基于Results of recommendations using Google Books API are irrelevant

一般来说,我正在为用户构建将这本书添加到他的收藏中的可能性。 为此,用户使用 Google 图书中的信息搜索图书。但是如果没有基于用户在搜索字段中输入的内容的建议,那会非常不舒服。

此时我们得到了书籍建议的json文本,但我真的不明白如何表示这个?那么如何创建该 JSON 的普通列表并为用户提供选择其中一个推荐的可能性,以便在点击时在搜索字段中自动完成每个推荐?

var requestUrl = "https://suggestqueries.google.com/complete/search?client=chrome&ds=bo&q=";
var xhr;

$(document).on("input", "#query", function () {
    typewatch(function () {
      
        var queryTerm = $("#query").val();
        $("#indicator").show();
        if (xhr != null) xhr.abort();
        xhr = $.ajax({
            url: requestUrl + queryTerm,
            dataType: "jsonp",
            success: function (response) {
            
                $("#indicator").hide();
                $("#output").html(response);
               
            }
        });
    }, 500);
});




$(document).ready(function () {
    $("#indicator").hide();
});
var typewatch = (function () {
    var timer = 0;
    return function (callback, ms) {
        clearTimeout(timer);
        timer = setTimeout(callback, ms);
    };
})();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
    <input type=text id="query" placeholder="Start typing..." /><span id="indicator"></span>
    <div style="width:600px;height:700px;padding-bottom:100px;position:relative;background:#6c94b8;" id="output"></div>
    <label for="query" style="position:relative;margin-left:100px;margin-top:100px;">Tags: </label>
</div>

【问题讨论】:

    标签: javascript jquery ajax json google-api


    【解决方案1】:

    我认为这就是您所追求的: https://www.librarieshacked.org/tutorials/autocompletewithapi

    $(document).ready(function () {  // only begin once page has loaded
        $("#txtBookSearch").autocomplete({ // attach auto-complete functionality to textbox
            // define source of the data
            source: function (request, response) {
                // url link to google books, including text entered by user (request.term)
                var booksUrl = "https://www.googleapis.com/books/v1/volumes?printType=books&q=" + encodeURIComponent(request.term);
                $.ajax({
                    url: booksUrl,
                    dataType: "jsonp",
                    success: function(data) {
                        response($.map(data.items, function (item) {
                            if (item.volumeInfo.authors && item.volumeInfo.title && item.volumeInfo.industryIdentifiers && item.volumeInfo.publishedDate)
                            {
                                return {
                                    // label value will be shown in the suggestions
                                    label: item.volumeInfo.title + ', ' + item.volumeInfo.authors[0] + ', ' + item.volumeInfo.publishedDate,
                                    // value is what gets put in the textbox once an item selected
                                    value: item.volumeInfo.title,
                                    // other individual values to use later
                                    title: item.volumeInfo.title,
                                    author: item.volumeInfo.authors[0],
                                    isbn: item.volumeInfo.industryIdentifiers,
                                    publishedDate: item.volumeInfo.publishedDate,
                                    image: (item.volumeInfo.imageLinks == null ? "" : item.volumeInfo.imageLinks.thumbnail),
                                    description: item.volumeInfo.description,
                                };
                            }
                        }));
                    }
                });
            },
            select: function (event, ui) {
                // what to do when an item is selected
                // first clear anything that may already be in the description
                $('#divDescription').empty();
                // we get the currently selected item using ui.item
                // show a pic if we have one
                if (item.image != '')
                {
                    $('#divDescription').append('<img src="' + ui.item.image + '" style="float: left; padding: 10px;">');
                }
                // and title, author, and year
                $('#divDescription').append('<p><b>Title:</b> ' + ui.item.title  + '</p>');
                $('#divDescription').append('<p><b>Author:</b> ' + ui.item.author  + '</p>');
                $('#divDescription').append('<p><b>First published year:</b> ' + ui.item.publishedDate  + '</p>');          
                // and the usual description of the book
                $('#divDescription').append('<p><b>Description:</b> ' + ui.item.description  + '</p>');
                // and show the link to oclc (if we have an isbn number)
                if (ui.item.isbn && ui.item.isbn[0].identifier)
                {
                    $('#divDescription').append('<P><b>ISBN:</b> ' + ui.item.isbn[0].identifier + '</p>');
                    $('#divDescription').append('<a href="http://www.worldcat.org/isbn/' + ui.item.isbn[0].identifier + '" target="_blank">View item on worldcat</a>');
                }
            },
            minLength: 2 // set minimum length of text the user must enter
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-06
      • 2011-08-24
      • 1970-01-01
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 2014-01-14
      • 2012-01-16
      相关资源
      最近更新 更多