CakePHP - 使用 group by 打印单个表头

发布于 2025-01-02 12:27:49 字数 2087 浏览 1 评论 0原文

我有一个模型任务列表,每个任务列表项都属于一个服务项。

在我的任务列表控制器中:

function index() {
  $this->Tasklist->recursive = 0;
  $this->set('tasklists', $this->Tasklist->find('all', array(
    'order' => array(
      'Service.name' => 'ASC',
      'Tasklist.name' => 'ASC'
    )
  )));
}

我的简单索引视图的相关部分:

<?php foreach ($tasklists as $tasklist): ?>
    <tr>
      <td><?php echo $tasklist['Tasklist']['id']; ?></td>
      <td><?php echo $tasklist['Tasklist']['name']; ?></td>
      <td>
      <?php echo $this->Html->link($tasklist['Service']['name'], array('controller' => 'services', 'action' => 'view', $tasklist['Service']['id'])); ?>
      </td>
      <td><?php echo $tasklist['Tasklist']['created']; ?></td>
      <td><?php echo $tasklist['Tasklist']['modified']; ?></td>
      <td>
      <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $tasklist['Tasklist']['id']), array('class' => 'button edit')); ?>
      </td>
    </tr>
<?php endforeach; ?>

我不想在每个表行中打印服务名称,而是将其打印为将任务列表分组在每个列表下:

<tr>
      <th colspan="5"><?php echo $this->Html->link($tasklist['Service']['name'], array('controller' => 'services', 'action' => 'view', $tasklist['Service']['id'])); ?></th>
</tr>
<tr>
      <td><?php echo $tasklist['Tasklist']['id']; ?></td>
      <td><?php echo $tasklist['Tasklist']['name']; ?></td>
      <td><?php echo $tasklist['Tasklist']['created']; ?></td>
      <td><?php echo $tasklist['Tasklist']['modified']; ?></td>
      <td>
      <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $tasklist['Tasklist']['id']), array('class' => 'button edit')); ?>
      </td>
</tr>

我已经尝试在控制器中使用组参数在我看来,还有一个嵌套的 foreach,但无法让它工作。

I have a model Tasklist, and each Tasklist item belongs to a Service item.

In my Tasklist controller:

function index() {
  $this->Tasklist->recursive = 0;
  $this->set('tasklists', $this->Tasklist->find('all', array(
    'order' => array(
      'Service.name' => 'ASC',
      'Tasklist.name' => 'ASC'
    )
  )));
}

Relevant part of my simple index View:

<?php foreach ($tasklists as $tasklist): ?>
    <tr>
      <td><?php echo $tasklist['Tasklist']['id']; ?></td>
      <td><?php echo $tasklist['Tasklist']['name']; ?></td>
      <td>
      <?php echo $this->Html->link($tasklist['Service']['name'], array('controller' => 'services', 'action' => 'view', $tasklist['Service']['id'])); ?>
      </td>
      <td><?php echo $tasklist['Tasklist']['created']; ?></td>
      <td><?php echo $tasklist['Tasklist']['modified']; ?></td>
      <td>
      <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $tasklist['Tasklist']['id']), array('class' => 'button edit')); ?>
      </td>
    </tr>
<?php endforeach; ?>

Instead of printing the Service name in every table row I would like to print it as a with the Tasklists grouped beneath each one:

<tr>
      <th colspan="5"><?php echo $this->Html->link($tasklist['Service']['name'], array('controller' => 'services', 'action' => 'view', $tasklist['Service']['id'])); ?></th>
</tr>
<tr>
      <td><?php echo $tasklist['Tasklist']['id']; ?></td>
      <td><?php echo $tasklist['Tasklist']['name']; ?></td>
      <td><?php echo $tasklist['Tasklist']['created']; ?></td>
      <td><?php echo $tasklist['Tasklist']['modified']; ?></td>
      <td>
      <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $tasklist['Tasklist']['id']), array('class' => 'button edit')); ?>
      </td>
</tr>

I have experimented with using the group parameter in my controller and a nested foreach in my view, but cannot get it to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹤舞 2025-01-09 12:27:50

我会这样做

   $current_service = '';
    <?php foreach ($tasklists as $tasklist): ?>
    <?php if($current_service != $tasklist['Service']['name']): ?>
    <tr>
          <th colspan="5"><?php echo $this->Html->link($tasklist['Service']['name'], array('controller' => 'services', 'action' => 'view', $tasklist['Service']['id'])); ?></th>
    </tr>
    <?php $current_service = $tasklist['Service']['name']; ?>
    <?php endif; ?>
    <tr>
          <td><?php echo $tasklist['Tasklist']['id']; ?></td>
          <td><?php echo $tasklist['Tasklist']['name']; ?></td>
          <td><?php echo $tasklist['Tasklist']['created']; ?></td>
          <td><?php echo $tasklist['Tasklist']['modified']; ?></td>
          <td>
          <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $tasklist['Tasklist']['id']), array('class' => 'button edit')); ?>
          </td>
    </tr>

    <?php endforeach; ?>

I'd do it like this

   $current_service = '';
    <?php foreach ($tasklists as $tasklist): ?>
    <?php if($current_service != $tasklist['Service']['name']): ?>
    <tr>
          <th colspan="5"><?php echo $this->Html->link($tasklist['Service']['name'], array('controller' => 'services', 'action' => 'view', $tasklist['Service']['id'])); ?></th>
    </tr>
    <?php $current_service = $tasklist['Service']['name']; ?>
    <?php endif; ?>
    <tr>
          <td><?php echo $tasklist['Tasklist']['id']; ?></td>
          <td><?php echo $tasklist['Tasklist']['name']; ?></td>
          <td><?php echo $tasklist['Tasklist']['created']; ?></td>
          <td><?php echo $tasklist['Tasklist']['modified']; ?></td>
          <td>
          <?php echo $this->Html->link(__('Edit', true), array('action' => 'edit', $tasklist['Tasklist']['id']), array('class' => 'button edit')); ?>
          </td>
    </tr>

    <?php endforeach; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文