MDL-49399 core: Add ability to specify a header col

This commit is contained in:
Andrew Nicols 2018-12-03 15:04:46 +08:00
parent 4c5b60a0f9
commit 702123a1dd

View File

@ -57,6 +57,11 @@ class flexible_table {
var $attributes = array(); var $attributes = array();
var $headers = 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. * @var string For create header with help icon.
*/ */
@ -429,6 +434,17 @@ class flexible_table {
$this->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 * Defines a help icon for the header
* *
@ -1098,6 +1114,18 @@ class flexible_table {
foreach ($row as $index => $data) { foreach ($row as $index => $data) {
$column = $colbyindex[$index]; $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 (empty($this->prefs['collapse'][$column])) {
if ($this->column_suppress[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) { if ($this->column_suppress[$column] && $suppress_lastrow !== NULL && $suppress_lastrow[$index] === $data) {
$content = ' '; $content = ' ';
@ -1108,10 +1136,7 @@ class flexible_table {
$content = ' '; $content = ' ';
} }
$html .= html_writer::tag('td', $content, array( $html .= html_writer::tag($celltype, $content, $attributes);
'class' => 'cell c' . $index . $this->column_class[$column],
'id' => $rowid . '_c' . $index,
'style' => $this->make_styles_string($this->column_style[$column])));
} }
} }