1

下午好,在我的 php/mysql 页面中,我有动态表,每行都有一行。我使用了显示/隐藏脚本来隐藏某些列,这很好,但我真的希望在隐藏列时更新行总数,在下面的示例中,如果我隐藏说 Jan 它应该扣除该列行中的值并更新总列?

这是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>

以及动态表格+表格隐藏列;

<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>

非常感谢您的关注。。

4

2 回答 2

1

然后,您必须在切换列之后使用 Javascript 解析您的表。

最简单的方法是给每一行和每个单元格一个类。注意不要为您的单元格使用 id,因为它们不会是唯一的。也不要使用名称,因为它们更专注于表单和输入。

<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>

然后您将解析您的行和单元格,如下所示(样式):

$$('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;
});

那应该行得通。

您可以使用其他 JS 框架(如 )编写模拟代码,不用说,如果没有任何框架,它会变得不那么简单。

于 2012-07-25T14:07:47.443 回答
0

你试试这个..

类名用于隐藏/显示功能

$('.total').hide();
$('.total').show();

或者

$('.total').toggle();

我检查过...

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
function show()
{

$('.total').toggle();


}
</script>
<table>
<tr class="row">
  <td class="tcol1">New</td>
  <td class="tcol2">Jan</td>
  <td class="tcol3">Feb</td>
  <td class="total">Total</td>
</tr>
</table>
<input type='button' value='new' id='new' onclick='show()'>
于 2012-07-27T13:18:46.933 回答