简体   繁体   中英

Append external json data to div category wise

i dont know how to append external json data to div. i know append with table .but, i am confused with div.please help me to solve this doubt. because,it need to append data with selected category div.

div1 category [books]

<div class="col-xs-12 col-sm-5 col-md-3 col-lg-2 card">
                <!--Card content-->
                <div class="card-body">
                    <!--Title-->
                    <h4 class="card-title">Card title</h4>
                    <!--Text-->
                    <img class="img-fluid z-depth-3 rounded-circle" src="https:/goo.gl/4cPCdn"
                        alt="Card image cap">
<h4 class="card-title">Category</h4>
                    <!--Card content-->
                    <a href="www.gogle.com" class="btn btn-primary">Button</a>
                </div>
            </div>

div 2 category [games]

<div class="col-xs-12 col-sm-5 col-md-3 col-lg-2 card">
                <!--Card content-->
                <div class="card-body">
                    <!--Title-->
                    <h4 class="card-title">Card title</h4>
                    <!--Text-->
                    <img class="img-fluid z-depth-3 rounded-circle" src="https:/goo.gl/4cPCdn"
                        alt="Card image cap">
<h4 class="card-title">Category</h4>
                    <!--Card content-->
                    <a href="www.gogle.com" class="btn btn-primary">Button</a>
                </div>
            </div>

Json

   {
    "category": {
        "books": [
            {"title":"Sentra", "url":"https:/goo.gl/4cPCdn","button":"https:google.in"},
            {"title":"Maxima", "url":"https:/goo.gl/4cPCdn"},"button":"https:google.in"}
        ],
    "games": [
            {"title":"Taurus", "url":https:/goo.gl/4cPCdn},"button":"https:/google.in"}
            {"title":"Escort", "url":https:/goo.gl/4cPCdn},"button":"https:/google.in"}
        ]
    }
}

Javascript & jquery

<script>
        $.ajax({

            url: 'json-data.json',
            type: "get",
            dataType: "json",

            success: function (data) {
                drawTable(data);
            }
        });

        function drawTable(data) {
            for (var i = 0; i < data.length; i++) {
                drawRow(data[i]);
            }
        }

        function drawRow(rowData) {
            **This part i dont know please teach me**
        }
    </script>

Fiddle

Fiddle codes click to edit

This isn't perfect but it should give you some help in understanding how to append to a div via jQuery. Your data was messed up, so I fixed it and made it a valid object. You may want to use things like JSLint to check your data before testing your pages - https://jsonlint.com/

 var data = { "category": { "books": [ {"title":"Sentra", "url":"https:/goo.gl/4cPCdn", "button":"https:google.in"}, {"title":"Maxima", "url":"https:/goo.gl/4cPCdn", "button":"https:google.in"} ], "games": [ {"title":"Taurus", "url":"https:/goo.gl/4cPCdn","button":"https:/google.in"}, {"title":"Escort", "url":"https:/goo.gl/4cPCdn","button":"https:/google.in"} ] } } /* not needed for test, we've already included our data above $.ajax({ url: 'json-data.json', type: "get", dataType: "json", success: function (data) { drawTable(data); } }); */ data = data.category; drawTable(data) function drawTable(theData) { for (category in theData) { console.log('category is '+category) var categoryEntries = theData[category] for (var i = 0; i < categoryEntries.length; i++) { var rowData = categoryEntries[i]; drawRow(category,rowData) } } } function drawRow(category,rowData) { // console.log(JSON.stringify(rowData)) var title = rowData.title; var url = rowData.url; var button = rowData.button var newDiv = '<div class="card">'+ '<div class="card-body">'+ '<h4 class="card-title">'+title+'</h4>'+ '<img class="img-fluid z-depth-3 rounded-circle" src="'+url+'" alt="Card image cap">'+ '<h4 class="card-title">'+category+'</h4>'+ '<a href="'+button+'" class="btn btn-primary">Button</a>'+ '</div>'+ '</div>' $('#'+category).append(newDiv); } 
 #books,#games { display:block; width: 100%; clear:both; } .card { float:left; width: 30vw; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <div id="books"> </div> <div id="games"> </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