简体   繁体   中英

How to populate bootstrap grids using Jquery and JSON?

I have made a NEWS website it consists of 3x4 grids. I want to populate these grids with NEWS title, description, image, etc. I am not able display anything on my website.

HTML code:

<div class="Article">
    <div class="col-md-4" title="News">
        <h3></h3>
        <p></p>
    </div>
    <div class="col-md-4" title="News">
        <h3></h3>
        <p></p>
    </div>
    <div class="col-md-4" title="News">
        <h3></h3>
        <p></p>
    </div>
</div>
<div class="Article">
    <div class="col-md-4" title="News">
        <h3></h3>
        <p></p>
    </div>
    <div class="col-md-4" title="News">
        <h3></h3>
        <p></p>
    </div>
    <div class="col-md-4" title="News">
        <h3></h3>
        <p></p>
    </div>
</div>
<div class="Article">
    <div class="col-md-4" title="News">
        <h3></h3>
        <p></p>
    </div>
    <div class="col-md-4" title="News">
        <h3></h3>
        <p></p>
    </div>
    <div class="col-md-4" title="News">
        <h3></h3>
        <p></p>
    </div>
</div>
<div class="Article">
    <div class="col-md-4" title="News">
        <h3></h3>
        <p></p>
    </div>
    <div class="col-md-4" title="News">
        <h3></h3>
        <p></p>
    </div>
    <div class="col-md-4" title="News">
        <h3></h3>
        <p></p>
    </div>
</div>
</div>

JS code:

$(document).ready(function() {
var news_source =["the-times-of-india","business-insider","techcrunch","bbc-sport"];
    for(var i=0;i<news_source.length;i++){
    $.getJSON('https://newsapi.org/v1/articles?source='+ news_source[i] +'&sortBy=top&apiKey=my-api-key',function (json) {
    console.log(json);

    for(var j=0;j<3;j++){
        $(".Article News h3").eq(j).html(json.articles[j].title);
        $('.Article News p').eq(j).html(json.articles[j].description);
    }

    });

    }
});

I am not able to display any data using JSON I have made array of website sources ie news_sources[] because each source returns 4 to 5 objects so that I can display it in grids, in total I have made 12 grids. I want to display Business NEWS in first row, sports NEWS in second row, tech NEWS in third row and so on in grid system that's why I have made array of news_sources[] to display articles from different web sources.

JSON data:

在此处输入图片说明

Website Layout:

在此处输入图片说明

Okay... Take a look at the HTML first. I have no way of testing this code but you will want something to this affect.

 var news_source = ["the-times-of-india", "business-insider", "techcrunch", "bbc-sport"]; $(function() { // get the articles var $sections = $("[data-place=article]"); // get the max-length var sec_len = $sections.length; // set for accessing the right section. var sec_count = 0; // gets all the sections, this should be a promise for (var i = 0; i < news_source.length; i++) { // i made this into a string var get_string = 'https://newsapi.org/v1/articles?source='+news_source[i]+'&sortBy=top&apiKey=my-api-key'; $.getJSON(get_string, function(json) { sec_count++; // there is no more room if (sec_count === sec_len) { return false; } else { for (var j = 0; j < json.length; j++) { var section = makeSection(); // returns div var header = makeHeader(json.articles[j].title, "3"); // returns header var paragraph = makeParagraph(json.articles[j].description); // returns paragraph section.append(header); // attach header section.append(paragraph); // attach para $sections.eq(sec_count).append(section); // append to document } } }); } }); function makeSection() { return $("<div />", { "class": "col-md-4", "title": "news" }) } function makeHeader(string, size) { return $("<h" + size + " />", { "text": string }); } function makeParagraph(string) { return $("<p />", { "text": string }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="Article" data-place="article"></div> <div class="Article" data-place="article"></div> <div class="Article" data-place="article"></div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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