jquery interate array and match to values in html table and fill in column -
i want read through array. match employee number table, , fill in bonus value in table on match.
<script type="text/javascript" src="jquery.js"></script> <script> $( document ).ready(function() { // note employee may 0,1, or more bonuses myarray = {"employees": [ {"employee": "1231", "bonus":100}, {"employee": "1232", "bonus":200}, {"employee": "1233", "bonus":300} {"employee": "1233", "bonus":33} ] } $.each(myarray.employees, function(index) { console.log(this.employee); // having problem here... $("#mytable:has(td:contains(this.employee)").each(function () { console.log("found"); // write 2nd cell in table next employee# // assume employee number in table unique }); }); }); </script> <table id='mytable' border='1px'> <tr> <th>employee</th> <th>bonus</th> </tr> <tr> <td>1232</td> <td> </td> </tr> <tr> <td>1231</td> <td> </td> </tr> <table>
i loop through table each time
$( document ).ready(function() { // note employee may 0,1, or more bonuses myarray = {"employees": [ {"employee": "1231", "bonus":100}, {"employee": "1232", "bonus":200}, {"employee": "1233", "bonus":300}, {"employee": "1233", "bonus":33} ] }; $.each(myarray.employees, function(index) { var emp=this.employee; var bonus=this.bonus; $('#mytable td').each(function(){ if($(this).text()==emp){ console.log('found'); $(this).next().text(bonus); } }); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table id='mytable' border='1px'> <tr> <th>employee</th> <th>bonus</th> </tr> <tr> <td>1232</td> <td> </td> </tr> <tr> <td>1231</td> <td> </td> </tr> <table>
Comments
Post a Comment