How to insert json data to owl carousel

2019-08-29 06:50发布

I have a JSON data from FB api reviews, and I want to insert the data from the JSON to an owl carousel div elements. I want each review to be created in a separate div element and that div element to be inside the owl carousel, and to work as carousel.

In the HTML I only have this:

<div id="carousel" class="owl-carousel" style="color: white;">
</div>

Previously I have set up the carousel, now my question is how to take the data from the JSON and insert in the div owl-carousel class but I want the divs that will be inserted inside to take the class owl-item, etc.

Here is the JavaScript:

$(document).ready(function () {
    var url = "https://graph.facebook.com/v3.2/...";

    $.getJSON(url, function (data) {
        var items = [];
        $.each(data.data, function (i, obj) {
            var text = '<p class="review_text">' + obj.review_text + '</p>'
            var date = '<p class="date">' + obj.created_time + '</p>'

        }); //Here I get the review text and the created date of the review
            //I want to create div element for each review and insert that div in the owl carousel

    });
});

1条回答
在下西门庆
2楼-- · 2019-08-29 07:13

This is what I have tried so far:

$(document).ready(function () {
    var url = "https://graph.facebook.com/v3.2/...";

    $.getJSON(url, function (data) {
        var items = [];
        $.each(data.data, function (i, obj) {
            //$('#target').append($('<div/>', { id: 'dummy' + i }))
            var text = '<p class="review_text">' + obj.review_text + '</p>'
            var date = '<p class="date">' + obj.created_time + '</p>'
            a = counter();
            $("#carousel").find("[data-index='" + i + "']").append(text, date)

        });
        $('#scripts').append('<input type="number" id="spam_key" value= ' + a + '>');


    });
    var wrapper = document.getElementById("carousel");
    var myHTML = '';
    for (b = 0; b <= 230; b++) { //but here instead of 230 I want to get the value of a
        myHTML += '<div id="review" data-index=' + (b) + '></div>';
    }
    wrapper.innerHTML = myHTML
});
查看更多
登录 后发表回答