Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

jquery - Create dynamic html & display json values in it

I have call ajax api call, i get response in json format. I want to display each values on screen with html elements. I've achieve this but it wont create dynamic design as i needed. Also i wan to show an message("No data available") if there is no data in response. Here is my working HTML & JSON sample. Need to display as follows

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="col-md-12">
    <div class="portlet light bg-inverse tripcontainer">
        <div id="container">
            <div class="col-md-4 driver">
                <div>
                    <label class="header">Driver Details</label>
                </div>
                <div>
                    <label>Name:</label>
                    <span class="dname"></span>
                </div>
                <div>
                    <label>Vehicle No:</label>
                    <span class="dvehicle"></span>
                </div>
                <div>
                    <label>Mobile No:</label>
                    <span class="dmob"></span>
                </div>
                <div>
                    <label>Email:</label>
                    <span class="demail"></span>
                </div>
                <div>
                    <label>Start Location:</label>
                    <span class="dstart"></span>
                </div>
                <div>
                    <label>Lat:</label>
                    <span class="dlat"></span>
                </div>
                <div>
                    <label>Long:</label>
                    <span class="dlong"></span>
                </div>
                <div>
                    <label>End Location:</label>
                    <span class="dend"></span>
                </div>
                <div>
                    <label>Lat:</label>
                    <span class="dselat"></span>
                </div>
                <div>
                    <label>Long:</label>
                    <span class="dselong"></span>
                </div>
                <div>
                    <label>Booked Seats:</label>
                    <span class="dseat"></span>
                </div>
                <div>
                    <label>Start Time:</label>
                    <span class="dstarttime"></span>
                </div>
                <div>
                    <label>Route ID:</label>
                    <span class="droute"></span>
                </div>
                <div>
                    <label>Distance:</label>
                    <span class="ddistance"></span>KM</div>
            </div>
            <div class="col-md-8 passanger">
                <div>
                    <label class="header">Passanger Details</label>
                </div>
                <div>
                    <label>Name:</label>
                    <span class="pname"></span>
                </div>
                <div>
                    <label>Mobile No:</label>
                    <span class="pmob"></span>
                </div>
                <div>
                    <label>Email:</label>
                    <span class="pemail"></span>
                </div>
                <div>
                    <label>Start Location:</label>
                    <span class="pstart"></span>
                </div>
                <div>
                    <label>Lat:</label>
                    <span class="pslat"></span>
                </div>
                <div>
                    <label>Long:</label>
                    <span class="pelong"></span>
                </div>
                <div>
                    <label>End Location:</label>
                    <span class="pend"></span>
                </div>
                <div>
                    <label>Lat:</label>
                    <span class="pelat"></span>
                </div>
                <div>
                    <label>Long:</label>
                    <span class="pelong"></span>
                </div>
                <div>
                    <label>Booked Seats:</label>
                    <span class="pseat"></span>
                </div>
                <div>
                    <label>Start Time:</label>
                    <span class="pstarttie"></span>
                </div>
                <div>
                    <label>End Time:</label>
                    <span class="pendtime"></span>
                </div>
                <div>
                    <label>Distance:</label>
                    <span class="pdistance"></span>KM</div>
            </div>
        </div>
    </div>
</div>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If I understood well you want to fill each information So to parse with javascript you can do :

var parsedResponse = JSON.parse(ajaxesponse);
if(parsedResponse.length = 0){
   console.log('No data');
}else{
   var keys = Object.keys(parsedResponse[0]);
   //parsedResponse[0] for driver data
   for(var i = 0; i < keys.length ; i++){
      //The best thing to do is to change your class name and name it the same way as your keys in the response object
      $("."+keys[i]).html(parsedResponse[0][keys[i]]);
   }
}

And your html : instead of

<div>
   <label>Email:</label>
   <span class="demail"></span>
</div>

You have something like that

<div>
   <label>Email:</label>
   <span class="email"></span>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...