Skip to content

Commit

Permalink
MDL-49399 core: Add ability to specify a header col
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jan 14, 2019
1 parent 4c5b60a commit 702123a
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions lib/tablelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class flexible_table {
var $attributes = array();
var $headers = array();

/**
* @var string A column which should be considered as a header column.
*/
protected $headercolumn = null;

/**
* @var string For create header with help icon.
*/
Expand Down Expand Up @@ -429,6 +434,17 @@ function define_headers($headers) {
$this->headers = $headers;
}

/**
* Mark a specific column as being a table header using the column name defined in define_columns.
*
* Note: Only one column can be a header, and it will be rendered using a th tag.
*
* @param string $column
*/
public function define_header_column(string $column) {
$this->headercolumn = $column;
}

/**
* Defines a help icon for the header
*
Expand Down Expand Up @@ -1098,6 +1114,18 @@ public function get_row_html($row, $classname = '') {
foreach ($row as $index => $data) {
$column = $colbyindex[$index];

$attributes = [
'class' => "cell c{$index}" . $this->column_class[$column],
'id' => "{$rowid}_c{$index}",
'style' => $this->make_styles_string($this->column_style[$column]),
];

$celltype = 'td';
if ($this->headercolumn && $column == $this->headercolumn) {
$celltype = 'th';
$attributes['scope'] = 'row';
}

if (empty($this->prefs['collapse'][$column])) {
if ($this->column_suppress[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
$content = ' ';
Expand All @@ -1108,10 +1136,7 @@ public function get_row_html($row, $classname = '') {
$content = ' ';
}

$html .= html_writer::tag('td', $content, array(
'class' => 'cell c' . $index . $this->column_class[$column],
'id' => $rowid . '_c' . $index,
'style' => $this->make_styles_string($this->column_style[$column])));
$html .= html_writer::tag($celltype, $content, $attributes);
}
}

Expand Down

0 comments on commit 702123a

Please sign in to comment.