Drupal Views:如何设置列和行的标题?

Posted

技术标签:

【中文标题】Drupal Views:如何设置列和行的标题?【英文标题】:Drupal Views: How to set title both column and row? 【发布时间】:2014-09-05 09:48:01 【问题描述】:

结果会是这样吗?

coltitle1 coltitle2 coltitle3

rowtitle4 结果结果结果

rowtitle3 结果结果结果

rowtitle3 结果结果结果

【问题讨论】:

向我们展示你到目前为止做了什么? 【参考方案1】:

AFAIK,这对于 core 视图是不可能的。你需要一个不同的 Views Style 插件来处理这种情况。 Views Matrix 模块提供了这样一个插件,但仍在开发中。

以下代码也实现了这种解决方案,但尚未经过广泛测试:

/**
 * Implements hook_views_plugins().
 */
function MODULE_views_plugins() 
  $plugins = array(
    'style' => array(
      'drupalcamp_sessions_schedule' => array(
        'title' => t('Schedule'),
        'help' => t('Display content as a schedule table.'),
        'handler' => 'views_plugin_style_schedule',
        'uses options' => TRUE,
        'uses row plugin' => TRUE,
        'uses fields' => TRUE,
        'type' => 'normal',
        'theme' => 'table',
        'register theme' => FALSE,
      ),
    ),
  );
  return $plugins;


class views_plugin_style_schedule extends views_plugin_style_mapping 

  /**
   * @inheritdoc
   */
  protected function define_mapping() 
    return array(
      'row' => array(
        '#title' => t('Rows headers'),
        '#required' => TRUE,
      ),
      'column' => array(
        '#title' => t('Columns header)'),
        '#required' => TRUE,
      ),
    );
  

  /**
   * @inheritdoc
   */
  public function render() 
    $column_field = isset($this->view->field[$this->options['mapping']['column']]) ? $this->options['mapping']['column'] : reset(array_keys($this->view->field));
    $row_field = isset($this->view->field[$this->options['mapping']['row']]) ? $this->options['mapping']['row'] : reset(array_keys($this->view->field));
    $variables = array(
      'view' => $this->view,
      'options' => $this->options,
      'header' => array(
       '_TIME_' => $this->view->field[$row_field]->options['label'] ? $this->view->field[$row_field]->options['label'] : t('Time')
      ),
      'rows' => array(),
      'mapping' => $this->define_mapping(),
    );
    // Collect table headers and render results into table cells.
    foreach ($this->view->result as $index => $item) 
      $column_key = $this->get_field_value($index, $column_field);
      if (!empty($column_key)) 
        if(!is_scalar($column_key)) 
          $column_key = md5(serialize($column_key));
        
        if (!isset($variables['header'][$column_key])) 
          $variables['header'][$column_key] = $this->get_field($index, $column_field);
          if ($this->view->field[$column_field]->options['label']) 
            $variables['header'][$column_key] = $this->view->field[$column_field]->options['label'] . ': ' . $variables['header'][$column_key];
          
        
      
      $row_key = $this->get_field_value($index, $row_field);
      if ($row_key != NULL && !is_scalar($row_key)) 
        $row_key = md5(serialize($row_key));
      
      if (!isset($variables['rows'][$row_key])) 
        $variables['rows'][$row_key]['_TIME_'] = array(
          'data' => $this->get_field($index, $row_field),
          'header' => TRUE,
        );
      
      $this->view->row_index = $index;
      // FIXME: Support multiple item in the same cell.
      $variables['rows'][$row_key][!empty($column_key) ? $column_key : '__DEFAULT__'] = array(
        'data' => $this->row_plugin->render($item),
        'colspan' => 1,
      );
    
    // Process table rows to...
    foreach ($variables['rows'] as $index => $row) 
      // ...order cells in each line and fill-in any empty cells.
      $variables['rows'][$index] = array();
      $default = isset($row['__DEFAULT__']) ? $row['__DEFAULT__'] : array(
        'data' => '',
        'colspan' => 1,
      );
      foreach (array_keys($variables['header']) as $column) 
        $variables['rows'][$index][] = isset($row[$column]) ? $row[$column] : $default;
      
      $last_cell_index = NULL;
      // ...merge adjacent cells containing an equal value.
      foreach ($variables['rows'][$index] as $cell_index => $cell) 
        if (!empty($last_cell_index) && ($variables['rows'][$index][$last_cell_index]['data'] == $cell['data'])) 
          $variables['rows'][$index][$last_cell_index]['colspan'] += 1;
          unset($variables['rows'][$index][$cell_index]);
        
        else 
          $last_cell_index = $cell_index;
        
      
    
    return theme($this->theme_functions(), $variables);
  

【讨论】:

以上是关于Drupal Views:如何设置列和行的标题?的主要内容,如果未能解决你的问题,请参考以下文章

在R中同时移动矩阵的特定列和行的有效方法

如何对数据框中某些列和行的数据求和?

在 SQL 中转置列和行的简单方法?

创建一个可以在 React 上拖放列和行的表

如何将数据从 python 列表中的列和行写入 csv 文件?

用 Pandas 将 DataFrame 中某些列和行的值替换为同一 DataFrame 中另一列的值