显示/隐藏记录

zf9nrax1  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(403)

既然我找不到任何线索,我想我应该在这里寻求帮助。
我正在处理一个表(php/mysql),它现在看起来像这样:
https://i.gyazo.com/4115cf6fb14921ea9109580ec9c6c531.png
我想做一个按钮,我可以显示/隐藏整个列。假设我想隐藏“manager”,我可以按下一个按钮,整个列都隐藏了。
我是编程新手,我已经尝试过不同的代码,但我不能让它工作。

  1. if($result = mysqli_query($link, $sql)){
  2. if(mysqli_num_rows($result) > 0){
  3. echo "<table class='table table-bordered table-striped'>";
  4. echo "<thead>";
  5. echo "<tr>";
  6. ?>
  7. <th class="rotate2"><div><span>Naam</span></div></th>
  8. <th class="rotate2"><div><span>Functie</span></div></th>
  9. <th class="rotate2"><div><span>Afdeling</span></div></th>
  10. <th class="rotate2"><div><span>Contract</span></div></th>
  11. <th class="rotate2"><div><span>DID</span></div></th>
  12. <th class="rotate2"><div><span>DUD</span></div></th>
  13. <th class="rotate2"><div><span>Manager</span></div></th>
  14. <th class="rotate2"><div><span>Profiel</span></div></th>
  15. <?php
  16. while($row = mysqli_fetch_array($result)){
  17. echo "<tr>";
  18. echo "<td>" . $row['naam'] . "</td>";
  19. echo "<td>" . $row['functie'] . "</td>";
  20. echo "<td>" . $row['afdeling'] . "</td>";
  21. echo "<td>" . $row['contract'] . "</td>";
  22. echo "<td>" . $row['DID'] . "</td>";
  23. echo "<td>" . $row['DUD'] . "</td>";
  24. echo "<td>" . $row['manager'] . "</td>";
  25. echo "<td>" . $row['profiel'] . "</td>";

我希望有人能帮我,
我无法使图像工作,所以我做了一个gyzago。
谢谢

bvn4nwqk

bvn4nwqk1#

你需要像这样清理你的php代码below:-

  1. <table class='table table-bordered table-striped'>
  2. <thead>
  3. <tr>
  4. <th class="rotate2"><div><span>Naam</span></div></th>
  5. <th class="rotate2"><div><span>Functie</span></div></th>
  6. <th class="rotate2"><div><span>Afdeling</span></div></th>
  7. <th class="rotate2"><div><span>Contract</span></div></th>
  8. <th class="rotate2"><div><span>DID</span></div></th>
  9. <th class="rotate2"><div><span>DUD</span></div></th>
  10. <th class="rotate2"><div><span>Manager</span><br><button class="show">Show</button><button class="hide">Hide</button></div></th>
  11. <th class="rotate2"><div><span>Profiel</span></div></th>
  12. <th class="rotate2"><div><span>Action</span></div></th>
  13. </tr>
  14. </thead>
  15. <tbody>
  16. <?php
  17. if($result = mysqli_query($link, $sql)){
  18. if(mysqli_num_rows($result) > 0){
  19. while($row = mysqli_fetch_array($result)){
  20. echo "<tr>";
  21. echo "<td>" . $row['naam'] . "</td>";
  22. echo "<td>" . $row['functie'] . "</td>";
  23. echo "<td>" . $row['afdeling'] . "</td>";
  24. echo "<td>" . $row['contract'] . "</td>";
  25. echo "<td>" . $row['DID'] . "</td>";
  26. echo "<td>" . $row['DUD'] . "</td>";
  27. echo "<td>" . $row['manager'] . "</td>";
  28. echo "<td>" . $row['profiel'] . "</td>";
  29. echo "</tr>";
  30. }
  31. }
  32. }
  33. ?>
  34. </tbody>
  35. </table>

然后在下面添加jquery代码:-

  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <script>
  3. $(document).ready(function(){
  4. $('.show').hide();
  5. $('.hide').on('click',function(){
  6. $('tr').find('td:eq('+$(this).closest('th').index()+')').css('visibility','hidden');
  7. $(this).closest('th').find('.show').show();
  8. $(this).hide();
  9. });
  10. $('.show').on('click',function(){
  11. $('tr').find('td:eq('+$(this).closest('th').index()+')').css('visibility','visible');
  12. $(this).closest('th').find('.hide').show();
  13. $(this).hide();
  14. });
  15. });
  16. </script>

样品加工code:-

  1. $(document).ready(function(){
  2. $('.show').hide();
  3. $('.hide').on('click',function(){
  4. $('tr').find('td:eq('+$(this).closest('th').index()+')').css('visibility','hidden');
  5. $(this).closest('th').find('.show').show();
  6. $(this).hide();
  7. });
  8. $('.show').on('click',function(){
  9. $('tr').find('td:eq('+$(this).closest('th').index()+')').css('visibility','visible');
  10. $(this).closest('th').find('.hide').show();
  11. $(this).hide();
  12. });
  13. });
  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  2. <table class='table table-bordered table-striped'>
  3. <thead>
  4. <tr>
  5. <th class="rotate2"><div><span>Naam</span></div></th>
  6. <th class="rotate2"><div><span>Functie</span></div></th>
  7. <th class="rotate2"><div><span>Afdeling</span></div></th>
  8. <th class="rotate2"><div><span>Contract</span></div></th>
  9. <th class="rotate2"><div><span>DID</span></div></th>
  10. <th class="rotate2"><div><span>DUD</span></div></th>
  11. <th class="rotate2"><div><span>Manager</span><br><button class="show">Show</button><button class="hide">Hide</button></div></th>
  12. <th class="rotate2"><div><span>Profiel</span></div></th>
  13. </tr>
  14. </thead>
  15. <tbody>
  16. <tr>
  17. <td>A</td>
  18. <td>A1</td>
  19. <td>A2</td>
  20. <td>A3</td>
  21. <td>A4</td>
  22. <td>A5</td>
  23. <td>A6</td>
  24. <td>A7</td>
  25. </tr>
  26. <tr>
  27. <td>B</td>
  28. <td>B1</td>
  29. <td>B2</td>
  30. <td>B3</td>
  31. <td>B4</td>
  32. <td>B5</td>
  33. <td>B6</td>
  34. <td>B7</td>
  35. </tr>
  36. <tr>
  37. <td>C</td>
  38. <td>C1</td>
  39. <td>C2</td>
  40. <td>C3</td>
  41. <td>C4</td>
  42. <td>C5</td>
  43. <td>C6</td>
  44. <td>C7</td>
  45. </tr>
  46. </tbody>
  47. </table>
展开查看全部

相关问题