简体   繁体   English

append 在多个 div 条件下明智 jQuery

[英]append in multiple div condition wise jQuery

I have an issue while appending the PHP response data in div condition-wise.我在按条件在 div 中附加 PHP 响应数据时遇到问题。 I am trying to append the data if type=='I' then it will append in other div OR if type=='A' then it will append in other div.我正在尝试 append 数据如果type=='I'然后它将 append 在其他 div 或如果type=='A'然后它将 append 在其他 div.

The code which I am made is not working, even I try multiples time not achieve the goal.我制作的代码不起作用,即使我尝试多次也无法达到目标。 I also read the other threads but it was not helpful for me in my case.我也阅读了其他线程,但对我来说这对我没有帮助。 Please help me...请帮我...

How I goal the achieve and make code correct.我如何实现目标并使代码正确。

 var mytable = $("table#table"); var mytableWithHeader = mytable.append('<thead><tr><th style="width: 20%; text-align: center; vertical-align: middle;">Name/House</th><th style="width: 10%; text-align: center; vertical-align: middle;">Age/Rent</th></tr></thead><tbody></tbody>'); $.each(response.data, function(i, item) { mytableWithHeader.find('tbody').append(` <tr> <td>${item.name}</td> <td>${item.age}</td> </tr> `); }); //I am trying to do is below - $.each(response.data, function(i, item) { if ({item.type } == 'I') { mytableWithHeader.find('tbody').append(` <tr> $("div.tab1").prepend(" <td>${item.name}</td> <td>${item.age}</td> "); </tr> `); } if ({ item.type } == 'A') { mytableWithHeader.find('tbody').append(` <tr> $("div.tab2").prepend(" <td>${item.house}</td> <td>${item.rent}</td> "); </tr> `); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="tab1"> <table id="table"></table> </div> <div class="tab2"> <table id="table"></table> </div>

Simplified version with fixes to the repeating ID in the tables and only using the conditionals to assign variables then only processing those variables once after the conditionals简化版本,修复了表中的重复 ID,并且仅使用条件来分配变量,然后仅在条件之后处理这些变量一次

 $("table.tab_table").append('<thead><tr><th style="width: 20%; text-align: center; vertical-align: middle;">Name/House</th><th style="width: 10%; text-align: center; vertical-align: middle;">Age/Rent</th></tr></thead><tbody></tbody>'); $.each(response.data, function(i, item){ const {type, house, rent, name, age} = item; let $cont, rowItems; if(type === 'A'){ $cont = $('.tab2'); rowItems = [house, rent]; }else{ $cont = $('.tab1'); rowItems = [name, age]; } const $cells = rowItems.map(txt => $('<td>').text(txt)), $row = $('<tr>').append($cells); $cont.find('table').append($row); });
 td{text-align:center}
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="tab1"> <table class="tab_table"></table> </div> <div class="tab2"> <table class="tab_table"></table> </div> <script> const response = { data:[ {type:'I', name:'Mr Foo', age:37}, {type:'I', name:'Mrs Bar', age:93}, {type:'A', house:'big', rent:200}, {type:'A', house:'small', rent:100} ] } </script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM