如果行ID匹配,则合并HTML表格单元格?

问题描述 投票:0回答:1

我有我的模型的结果集,在我的数据库中我有患者数据,每个患者可以有1个结果或许多结果,我想要做的是如果引脚匹配我的结果表中的第一列合并,所以例如如果我有一个患有3次测试的患者,目前看起来像这样:

PIN | Name | test ... etc 
1   | Alan | HIV .... 
1   | Alan | Chlamydia ... 
1   | Alan | Vit D ...

看起来像它的id是:

PIN | Name | test ... etc 
    | Alan | HIV .... 
1   | Alan | Chlamydia ... 
    | Alan | Vit D ...

我有代码来分隔控制器中的引脚:

foreach ($results as $result){    
    if ($result['pin'] === $last_pin){
        $result['pin'] = null;
    } else {
        $last_pin = $result['pin'];
    }
}

这给了我视图中所需的输出,例如:1, 2, 3, 4而不是1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, etc

但是我如何让我的桌子“漂亮”?我使用colspanrowspan?我以前从未使用它们,也无法解决它们!

javascript html database html-table
1个回答
1
投票

rowspan属性允许您在多行上拉伸表格单元格。拉伸细胞侵入的行只是腾出空间,就像拉伸细胞在HTML中在适当的位置定义一样。对于您的使用,这意味着患者的第一行比其他细胞多一个细胞。

table { border-collapse: collapse; }
td { border: 1px solid black; padding: 5px; }
<table>
  <thead>
    <td>PIN</td>
    <td>Name</td>
    <td>Test</td>
  </thead>
  <tr>
    <td rowspan="3">2101</td>
    <td>Alice</td>
    <td>The Mondays</td>
  </tr>
  <tr>
    <td>Alice</td>
    <td>Corrugated Ankles</td>
  </tr>
  <tr>
    <td>Alice</td>
    <td>Slack Tongue</td>
  </tr>
  <tr>
    <td rowspan="2">4242</td>
    <td>Bob</td>
    <td>3rd Degree Sideburns</td>
  </tr>
  <tr>
    <td>Bob</td>
    <td>King Complex</td>
  </tr>
</table>

您需要知道单元格将延伸多少行。您可以在PIN分离代码中收集该信息:

foreach ($results as $result) {    
  if ($result['pin'] === $last_pin) {
    $result['pin'] = null;
    $top_row['rowspan'] += 1;
  } else {
    $last_pin = $result['pin'];
    $top_row = $result;
    $top_row['rowspan'] = 1;
  }
}

如果<td>$result['pin'],则跳过为PIN创建null元素,并将rowspan属性设置为您保存的值。

© www.soinside.com 2019 - 2024. All rights reserved.