如何将摘要行添加到 Drupal 视图?

发布于 2024-09-29 15:02:06 字数 235 浏览 3 评论 0原文

我已经构建了一个视图(Drupal 6.x、Views 2.x)。我希望能够在此视图的末尾添加一个汇总行 - 对几列进行总计并将总计包含在汇总行中。

我该怎么做?我可以实现一些视图数据更改钩子来更改构造的数据(在它获得主题之前)吗?

(请注意,我无法使用 views_calc,因为此视图中的部分数据来自视图关系,而 views_calc 在撰写本文时尚不支持该关系。)

I've built a view (Drupal 6.x, Views 2.x). I'd like to be able to add a summary row at the end of this view — total up several columns and include the totals in the summary row.

How can I do this? Is there some Views data-altering hook I can implement, to change the constructed data (before it gets themed)?

(Note that I can't use views_calc because some of the data in this view is coming from Views Relationships, which views_calc doesn't support at the time of writing.)

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

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

发布评论

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

评论(5

不离久伴 2024-10-06 15:02:06

几个小时后回答我自己的问题...一种方法是实现hook_views_pre_render():

/**
 * Implementation of hook_views_pre_render().
 */
function mymodule_views_pre_render(&$view) {
  if ($view->name == 'myview') {
    // perform calculations on each row
    $pointsEarned = $pointsPossible = 0;
    foreach($view->result as $submission) {
      if (is_numeric($submission->node_data_field_pointsearned_field_pointsearned_value)) {
        $pointsEarned += $submission->node_data_field_pointsearned_field_pointsearned_value;
        $pointsPossible += $submission->node_node_data_field_pointspossible_field_pointspossible_value;
      }
    }


    // insert a 'total' row
    $row = new stdClass();
    $row->node_data_field_pointsearned_field_pointsearned_value = $pointsEarned;
    $row->node_node_data_field_pointspossible_field_pointspossible_value = $pointsPossible;
    $view->result[] = $row;

    if ($pointsPossible > 0) {
      // insert an 'average' row
      $row = new stdClass();
      $row->users_name = 'Average:';
      $row->node_data_field_pointsearned_field_pointsearned_value = round($pointsEarned/$pointsPossible * 100) . "%";
      $view->result[] = $row;
    }
  }
}

To answer my own question a couple hours later... One way would be to implement hook_views_pre_render():

/**
 * Implementation of hook_views_pre_render().
 */
function mymodule_views_pre_render(&$view) {
  if ($view->name == 'myview') {
    // perform calculations on each row
    $pointsEarned = $pointsPossible = 0;
    foreach($view->result as $submission) {
      if (is_numeric($submission->node_data_field_pointsearned_field_pointsearned_value)) {
        $pointsEarned += $submission->node_data_field_pointsearned_field_pointsearned_value;
        $pointsPossible += $submission->node_node_data_field_pointspossible_field_pointspossible_value;
      }
    }


    // insert a 'total' row
    $row = new stdClass();
    $row->node_data_field_pointsearned_field_pointsearned_value = $pointsEarned;
    $row->node_node_data_field_pointspossible_field_pointspossible_value = $pointsPossible;
    $view->result[] = $row;

    if ($pointsPossible > 0) {
      // insert an 'average' row
      $row = new stdClass();
      $row->users_name = 'Average:';
      $row->node_data_field_pointsearned_field_pointsearned_value = round($pointsEarned/$pointsPossible * 100) . "%";
      $view->result[] = $row;
    }
  }
}
吃素的狼 2024-10-06 15:02:06

环顾四周,看起来 Views Calc 可以做你想做的事情。

Glancing around, it looks like Views Calc can do what you want.

清醇 2024-10-06 15:02:06

Views Calc 有很多未解决的错误。 View Summarize 似乎更稳定。

Views Calc has a lot of open bugs. View Summarize seems more stable.

临走之时 2024-10-06 15:02:06

就我个人而言,我会在视图模板中处理这个问题。为您的视图创建一个views-view.tpl.php,然后对其进行编辑、计算并打印出摘要。

另一种选择是为同一视图创建另一个显示,然后创建一个 views-view-unformatted.tpl.php 并计算并打印出摘要,而无需执行 print $row ; 以避免调用字段模板。添加使用视图显示在需要的地方。

Personally, I would handle this in the view templates. Create a views-view.tpl.php for your view, and then edit it calculate and print out the summary.

Another option is to create another display for the same view, and then create a views-view-unformatted.tpl.php and calculate and print out the summary w/o doing a print $row; to avoid calling the fields template. The add use view display where needed.

如此安好 2024-10-06 15:02:06

如果其他人遇到这个问题,您可以使用 Views Summarize 添加汇总表显示。您只需设置显示,然后选择每列的汇总方式。我还无法让它与视图数据导出一起使用,但如果您只想查看网站上的数据,它就可以工作。

If anyone else comes across this question, you can use Views Summarize that adds a summarized table display. You just set the display and then choose how you want each column summarized. I haven't been able to get this to work with Views Data Export yet, but it works if you just want to see the data on the site.

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