简体   繁体   中英

append dynamic data into table row jquery

 var data = [{ "mathid": "015-08-0011-000-01", "mathid1": "342-432-423-000", "englishid": "015-08-0011-000-01-1001", "englishid1": "342-432-423-000", "scienceid": "015-08-0011-000-01-2001", "scienceid1": "342-432-423-000" }, { "mathid": "015-08-0011-000-02", "mathid1": "342-432-423-001", "englishid": "015-08-0011-000-01-1002", "englishid1": "342-432-423-001", "scienceid": "015-08-0011-000-01-2002", "scienceid1": "342-432-423-001" }] var tr; var td; for (var i = 0; i < data.length; i++) { tr = $('<tr/>'); td = $('<td/>'); if (data[i].mathid) { tr.append("<td id=''>" + data[i].mathid + "</td>"); tr.append("<td id=''>" + data[i].mathid1 + "</td>"); } if (data[i].englishid) { tr.append("<td id=''>" + data[i].englishid + "</td>"); tr.append("<td id=''>" + data[i].englishid1 + "</td>"); } if (data[i].scienceid) { tr.append("<td id=''>" + data[i].scienceid + "</td>"); tr.append("<td id=''>" + data[i].scienceid1 + "</td>"); } tr.append("<td id=''>" + "<a class= id= href='#' >View Details</a>" + "</td>"); $("#searchresult").append(tr); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id="searchresult"> <thead> <tr> <td>Subject id</td> <td>Subject id1</td> </tr> </thead> </table> 

In my demo I want to add subject id and id1 to the table. I expect to add each subject to each row and each id to each td. Something is messing up the append which i cant figure out what. I don't understand what is messing my append.

Any idea is appreciated.

Expected output:

Subjectid | subjectid1
_______________________
mathid    | mathid1
_______________________
scienceid | scienceid1
_______________________
englishid | englishid1

http://jsfiddle.net/pmyfq93z/

Is this what you are after? If so the arrays were set up wrong. You had all of the information as one item in the array, here it has been split up so that it gets put in to the correct cells.

var data = [{
  "mathid": "015-08-0011-000-01",
  "mathid1": "342-432-423-000"},
            {
  "englishid": "015-08-0011-000-01-1001",
  "englishid1": "342-432-423-000"},
            {
  "scienceid": "015-08-0011-000-01-2001",
  "scienceid1": "342-432-423-000"

}]

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