我有以下代码。我还没有完全学会如何修改它以满足我的项目的需要;但是,我正在慢慢地学习这段代码的不同元素。我之前从未做过这样的事情,所以这对我来说有点新鲜。
现在, 代码从此示例中获取数据数据。实际上,它来自数据库;但是,这只是让它更容易使用。它循环遍历整个数组,在收集完所有数据后,它构建表并根据特定大小的数组中的条目数自动将“size”列的行数设置为所需的行数。一旦检测到新的大小,它就会添加一个新的标题行并重新开始。
但是,如果我想要计算另一列呢?我需要修改哪些内容才能使用自动行行的唯一列?
因此,“size”的列不仅会自动计算,而且“thickness”的列也会单独计算。得到这样的东西......
----------------------------------------
name size thickness price
----------------------------------------
1 | | | $25 |
----- -------------
2 | 2x2 | | $27 |
------ ---------------
3 | | .160 | $30 |
--------------- ---------------
4 | 2x3 | | $40 |
---------------- ---------------
5 | 3x3 | | $55 |
-----------------------------------------
在上面的示例中,大小(2x2)和厚度(.160)都添加了行数,但他们自动计算了行数所需的行数。
$data = array(
array("name" => "item1", "size" => "2 x 2", "thickness" => ".020", "price" => "$25"),
array("name" => "item2", "size" => "2 x 2", "thickness" => ".025", "price" => "$28"),
array("name" => "item6", "size" => "2 x 2", "thickness" => ".080", "price" => "$50"),
array("name" => "item3", "size" => "2 x 2", "thickness" => ".030", "price" => "$30"),
array("name" => "item4", "size" => "2 x 2", "thickness" => ".040", "price" => "$40"),
array("name" => "item5", "size" => "3 x 2", "thickness" => ".050", "price" => "$43"),
array("name" => "item6", "size" => "3 x 2", "thickness" => ".050", "price" => "$43"),
array("name" => "item7", "size" => "3 x 2", "thickness" => ".050", "price" => "$43"),
array("name" => "item8", "size" => "3 x 2", "thickness" => ".050", "price" => "$43"),
array("name" => "item9", "size" => "4 x 2", "thickness" => ".050", "price" => "$43"),
array("name" => "item10", "size" => "4 x 2", "thickness" => ".050", "price" => "$43"),
array("name" => "item11", "size" => "4 x 2", "thickness" => ".050", "price" => "$43"),
);
# Create storage array
$transfer = [];
# Loop $data
foreach($data as $row) {
# Make the size the key value
$transfer[$row['size']][] = $row;
}
# Now just loop over the first array which is the grouping
?>
<table>
<?php foreach($transfer as $block => $rows): ?>
<tr>
<th>Name</th>
<th>Size</th>
<th>Thickness</th>
<th>Price</th>
</tr>
<?php
# Set the default rowspan as empty
$rowspan = false;
# This indicates an easy way to note that you need a new header row
$start = true;
# Loop each rows now
foreach($rows as $row):
if(!empty($start)) {
# Assign rowspan
$rowspan = count($rows);
# This is the first row, so make sure the start is set false
# for subsequent rows
$start = false;
}
?>
<tr>
<td><?php echo $row['name'] ?></td>
<?php if(!empty($rowspan)): ?>
<td rowspan="<?php echo $rowspan; $rowspan = false; ?>"><?php echo $row['size'] ?></td>
<?php endif ?>
<td><?php echo $row['thickness'] ?></td>
<td><?php echo $row['price'] ?></td>
</tr>
<?php endforeach ?>
<?php endforeach ?>
</table>
答案 0 :(得分:0)
您应该能够创建一些标记来指示您需要检查厚度上的行数/行数:
<table>
<tr>
<th>Name</th>
<th>Size</th>
<th>Thickness</th>
<th>Price</th>
</tr>
<tr>
<td>item1</td>
<td rowspan="5">2 x 2</td>
<td>.020</td>
<td>$25</td>
</tr>
<tr>
<td>item2</td>
<td>.025</td>
<td>$28</td>
</tr>
<tr>
<td>item6</td>
<td>.080</td>
<td>$50</td>
</tr>
<tr>
<td>item3</td>
<td>.030</td>
<td>$30</td>
</tr>
<tr>
<td>item4</td>
<td>.040</td>
<td>$40</td>
</tr>
<tr>
<th>Name</th>
<th>Size</th>
<th>Thickness</th>
<th>Price</th>
</tr>
<tr>
<td>item5</td>
<td rowspan="4">3 x 2</td>
<td rowspan="4">.050</td>
<td>$43</td>
</tr>
<tr>
<td>item6</td>
<td>$43</td>
</tr>
<tr>
<td>item7</td>
<td>$43</td>
</tr>
<tr>
<td>item8</td>
<td>$43</td>
</tr>
<tr>
<th>Name</th>
<th>Size</th>
<th>Thickness</th>
<th>Price</th>
</tr>
<tr>
<td>item9</td>
<td rowspan="3">4 x 2</td>
<td rowspan="3">.050</td>
<td>$43</td>
</tr>
<tr>
<td>item10</td>
<td>$43</td>
</tr>
<tr>
<td>item11</td>
<td>$43</td>
</tr>
</table>
给你:
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", [YOUR LOCATOR]);