简体   繁体   中英

How to Populate HTML with data from jquery (multiple nested div)

I want to Populate elements inside class info-wrapper using JQuery. Data that has to populate are inside data variable in JQuery. I can't use $('#id').append("div") like these syntax because i want to nest multiple div inside each other and popluate .card-wrapper and its child elements for every data item.

Jquery


    $.ajax({
        method: 'GET',
        url: 'getUser/admin',
        dataType: 'json',
        success: function (data) {
            console.log(typeof(data));
            data = Object.entries(data);
            for(var i = 0;i<data.length;i++)
            {

            }
        },
        error: function () {

        }
    })

Blade Template

@extends('layouts.app')

@section('admin-content')
<div class="container">
                    <div class="row" id="inject">
                        <div class="col-md-6  col-sm-12">
                            <div class="card-wrapper">
                                <div class="info-wrapper">
                                    <h2 id="name"></h2>
                                    <h6 id="email"></h6>
                                    <hr/>
                                    <h6 id="department"></h6>
                                    <h6 is="sem"></h6>
                                </div>
                                <div class="button-wrapper">
                                    <button class="btn btn-danger" type="submit">Delete Account</button>
                                    <button class="btn btn-secondary" type="submit">Block Account</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
@endsection

Because info-wrapper is just a single element, you can instead populate elements inside the div with inject id, you can make changes to your js to generate elements dynamically, I hope this can help.

    $.ajax({
    method: 'GET',
    url: 'getUser/admin',
    dataType: 'json',
    success: function (data) {
        console.log(typeof(data));
        data = Object.entries(data);

        let html = '';
        for(var i = 0;i<data.length;i++)
        {
          html += `
            <div class="card-wrapper">
              <div class="info-wrapper">
                  <h2 id="name">${data[i].name}</h2>
                  <h6 id="email">${data[i].email}</h6>
                  <hr/>
                  <h6 id="department">${data[i].department}</h6>
                  <h6 is="sem"><${data[i].sem}/h6>
              </div>
              <div class="button-wrapper">
                  <button class="btn btn-danger" type="submit">Delete Account</button>
                  <button class="btn btn-secondary" type="submit">Block Account</button>
              </div>
          </div>
          `;
        }

        html = `<div class="col-md-6  col-sm-12">${html}</div>`

        $('#inject').html(html);
    },
    error: function () {

    }
})

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