duanlie2709 2012-07-25 13:49
浏览 52
已采纳

使用总计显示/隐藏动态表列

Good afternoon, in my php/mysql page I have dynamic tables with a row total for each row. I have used a show/hide script to hide certain columns which is great but I would really like the row total to update when column is hidden, iin example below if I hide say Jan it should deduct the value in that column row and update the total column?

Here is the js;

<script type="text/javascript" src="../js/prototype.js"></script>

<script language="javascript">
function toggleVis(button) { 
  // Toggle column 
  cells = $$('.t'+button.name); 
  cells.invoke(button.checked ? 'show' : 'hide'); 

  // Recaulculate total 
  $$('tr.row').each(function(row) { 
    // Initialise to zero 
    var total = 0; 
    row.down('.total').textContent = total; 

    // Sum all visible cells 
    row.select('td').each(function(cell) { 
      total += cell.visible() ? parseInt(cell.textContent, 10) : 0;
    }); 

    // Write the total in the total cell 
    row.down('.total').textContent = total; 
  }); 
}
</script>

And dynamic table + form to hide columns;

<form name="tcol" onsubmit="return false">
  Show columns
  <input type=checkbox name="col1" onclick="toggleVis(this.name)" checked> 1
  <input type=checkbox name="col2" onclick="toggleVis(this.name)" checked> 2
  <input type=checkbox name="col3" onclick="toggleVis(this.name)" checked> 3
</form>

<table border="1" cellpadding="1" cellspacing="1">
  <tr>
    <td class="tcol1">Adviser</td>
    <td class="tcol2">Jan</td>
    <td class="tcol3">Feb</td>
    <td class="total">Total</td>
  </tr>
  <?php do { ?>
    <tr class="row">
      <td class="tcol1"><?php echo $row_Recordset1['Adviser']; ?></td>
      <td class="tcol2"><?php echo $row_Recordset1['Jan'    ]; ?></td>
      <td class="tcol3"><?php echo $row_Recordset1['Feb'    ]; ?></td>
      <td class="total"><?php echo $row_Recordset1['Total'  ]; ?></td>
    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>

Many thanks for looking..

  • 写回答

2条回答 默认 最新

  • duanniubeng2265 2012-07-25 14:07
    关注

    Then you have to parse your table using Javascript, just after toggling the column.

    The easiest way would be to give a class to each rows and each cell. Take care not to use ids for you cells as they won't be unique. Don't use names either, as they are more dedicated to forms and inputs.

    <tr class="row">
      <td class="tcol1"><?php echo $row_Recordset1['Adviser']; ?></td>
      <td class="tcol2"><?php echo $row_Recordset1['Jan'    ]; ?></td>
      <td class="tcol3"><?php echo $row_Recordset1['Feb'    ]; ?></td>
      <td class="total"><?php echo $row_Recordset1['Total'  ]; ?></td>
    </tr>
    

    Then you would parse your rows and cells like the following (prototype.js style) :

    $$('tr.row').each(function(row) {
      // Initialise to zero
      var total = 0;
      row.down('.total').textContent = total;
    
      // Sum all visible cells
      row.select('td').each(function(cell) {
         total += cell.visible() ? parseInt(cell.textContent) : 0;
      });
    
      // Write the total in the total cell
      row.down('.total').textContent = total;
    });
    

    That should work.

    You could have a analog code written with other JS frameworks, like jquery, needless to say that without any framework it would be quite less trivial.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容