javascript - How to display multiple tables dynamically using one button click using ajax -


hi want display multiple table , table data dynamically brought consuming json data on single button click using ajax & javascript,jquery . single table can generate table using below procedure.

$.ajax({     type: 'get',     url: xxx.xxx.xxx.xxx,     data: "id=" + clo + "&location=" + clop +"",      success: function (resp) {                   var location = resp;         var tr;                   (var = 0; < location.length; i++) {              tr = tr + "<td style='height:20px' align='left'>" + location[i].name + "</td>";             tr = tr + "<td style='height:20px' align='right'>" + location[i].qty + "</td>";             tr = tr + "<td style='height:20px' align='right'>" + location[i].amt + "</td>";             tr = tr + "</tr>";          };         document.getelementbyid('p_w').style.display = "block";         document.getelementbyid('wise').innerhtml = "<table id='rt1' >" + "<thead ><tr><th style='height:20px'>name</th>" + "<th style='height:20px'>qty</th>" + "<th style='height:20px'>amnt</th>"+ "</tr></thead>"             + tr +             "<tr><td style='height:20px'></td></tr>" +             "</table>";         document.getelementbyid('wise').childnodes[0].nodevalue = null;      },     error: function (e) {         spinnerplugin.activitystop();         window.plugins.toast.showlongbottom("please enable internet          spinnerplugin.activitystop();      } }); 

but generating multiple tables using on single click using ajax how can generate , want generate multiple tables in below format

table, th, td {      border: 1px solid black;  }
<body>  <p> table 1</p>  <table style="width:100%">      <tr>      <th>firstname</th>      <th>lastname</th>       <th>age</th>    </tr>    <tr>      <td>jill</td>      <td>smith</td>      <td>50</td>    </tr>    <tr>      <td>eve</td>      <td>jackson</td>      <td>94</td>    </tr>    <tr>      <td>john</td>      <td>doe</td>      <td>80</td>    </tr>  </table>  <p> table 2</p>  <table style="width:100%">      <tr>      <th>subject</th>      <th>marks</th>           </tr>    <tr>      <td>science</td>      <td>70</td>         </tr>    <tr>      <td>computers</td>      <td>80</td>          </tr>    <tr>      <td>art</td>      <td>70</td>          </tr>  </table>  <p>table 3</p>  <table style="width:100%">      <tr>      <th>laptop</th>      <th>price</th>           </tr>    <tr>      <td>dell</td>      <td>$350</td>         </tr>    <tr>      <td>lenovo</td>      <td>$450</td>          </tr>    <tr>      <td>asus</td>      <td>$200</td>          </tr>  </table>  </body>

/* sample response expected ajax  */  var data = {    student: [{      name: 'abc',      age: '20'    }, {      name: 'xyz',      age: '30'    }],    marklist: [{      subject: 'english',      mark: '50'    }, {      subject: 'arabic',      mark: '75'    }],    products: [{      company: "dell",      amount: '50'    }, {      company: "hp",      amount: '100'    }]  };    /* called on ajax success  */  fnajaxsuccess(data);    function fnajaxsuccess(data) {    document.getelementbyid('main').innerhtml = '';    fillstudentlist(data.student, 1);    fillmarklist(data.marklist, 2)    fillproductlist(data.products, 3);  }    /* creating 1st table  */  function fillstudentlist(student, tableindex) {    var html = "<p> table " + tableindex + "</p><table>";    (var = 0; < student.length; i++) {      html += "<tr><td>" + student[i].name + "</td><td>" + student[i].age + "</td></tr>"    }    document.getelementbyid('main').innerhtml += (html + "</table>");  }    /* creating 2nd table  */  function fillmarklist(marklist, tableindex) {    var html = "<p> table " + tableindex + "</p><table>";    (var = 0; < marklist.length; i++) {      html += "<tr><td>" + marklist[i].subject + "</td><td>" + marklist[i].mark + "</td></tr>"    }    document.getelementbyid('main').innerhtml += (html + "</table>");  }    /* creating 3rd table  */  function fillproductlist(products, tableindex) {    var html = "<p> table " + tableindex + "</p><table>";    (var = 0; < products.length; i++) {      html += "<tr><td>" + products[i].company + "</td><td>" + products[i].amount + "</td></tr>"    }    document.getelementbyid('main').innerhtml += (html + "</table>");  }
table, th, td {      border: 1px solid black;  }
<div id='main'></div>

here it's assumed that, ajax response have enough data filling 3 tables.

you can write

$(document).on('click', '#id-of-button', function(){     $.ajax({         type: 'get',         url: xxx.xxx.xxx.xxx,         data: "id=" + clo + "&location=" + clop +"",          success: function (resp) {             fnajaxsuccess(resp);         },         error: function (e) {             spinnerplugin.activitystop();             window.plugins.toast.showlongbottom("please enable internet              spinnerplugin.activitystop();         }     }); }); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -