MDL-78556 tablelib: flexible_table should support caption tag

This commit is contained in:
danghieu1407 2023-07-11 14:18:54 +07:00
parent f29f757f5f
commit 173da3666c
3 changed files with 104 additions and 18 deletions

View File

@ -170,6 +170,16 @@ class flexible_table {
/** @var $resetting bool Whether the table preferences is resetting. */
protected $resetting;
/**
* @var string $caption The caption of table
*/
public $caption;
/**
* @var array $captionattributes The caption attributes of table
*/
public $captionattributes;
/**
* @var filterset The currently applied filerset
* This is required for dynamic tables, but can be used by other tables too if desired.
@ -1839,8 +1849,35 @@ class flexible_table {
// Start of main data table
echo html_writer::start_tag('div', array('class' => 'no-overflow'));
echo html_writer::start_tag('table', $this->attributes);
echo html_writer::start_tag('table', $this->attributes) . $this->render_caption();
}
/**
* This function set caption for table.
*
* @param string $caption Caption of table.
* @param array|null $captionattributes Caption attributes of table.
*/
public function set_caption(string $caption, ?array $captionattributes): void {
$this->caption = $caption;
$this->captionattributes = $captionattributes;
}
/**
* This function renders a table caption.
*
* @return string $output Caption of table.
*/
public function render_caption(): string {
if ($this->caption === null) {
return '';
}
return html_writer::tag(
'caption',
$this->caption,
$this->captionattributes,
);
}
/**

View File

@ -67,17 +67,21 @@ class tablelib_test extends \advanced_testcase {
/**
* Create a table with properties as passed in params, add data and output html.
*
* @param string[] $columns
* @param string[] $headers
* @param bool $sortable
* @param bool $collapsible
* @param string[] $suppress
* @param string[] $nosorting
* @param (array|object)[] $data
* @param int $pagesize
* @param string[] $columns The columns of the table.
* @param string[] $headers The header of the table.
* @param bool $sortable Sorting of the table.
* @param bool $collapsible Is table collapsible.
* @param string[] $suppress Suppress columns.
* @param string[] $nosorting No sorting.
* @param (array|object)[] $data The data of the table.
* @param int $pagesize Page size of the table
* @param string $caption Caption of the table.
* @param array $captionattribute The attribute of the caption.
*/
protected function run_table_test($columns, $headers, $sortable, $collapsible, $suppress, $nosorting, $data, $pagesize) {
$table = $this->create_and_setup_table($columns, $headers, $sortable, $collapsible, $suppress, $nosorting);
protected function run_table_test($columns, $headers, $sortable, $collapsible, $suppress, $nosorting, $data,
$pagesize, $caption = '', $captionattribute = []) {
$table = $this->create_and_setup_table($columns, $headers, $sortable, $collapsible, $suppress, $nosorting,
$caption, $captionattribute);
$table->pagesize($pagesize, count($data));
foreach ($data as $row) {
$table->add_data_keyed($row);
@ -88,15 +92,18 @@ class tablelib_test extends \advanced_testcase {
/**
* Create a table with properties as passed in params.
*
* @param string[] $columns
* @param string[] $headers
* @param bool $sortable
* @param bool $collapsible
* @param string[] $suppress
* @param string[] $nosorting
* @param string[] $columns The columns of the table.
* @param string[] $headers The header of the table.
* @param bool $sortable Sorting of the table.
* @param bool $collapsible Is table collapsible.
* @param string[] $suppress Suppress columns.
* @param string[] $nosorting No sorting.
* @param string $caption Caption of the table.
* @param array $captionattribute The attribute of the caption.
* @return flexible_table
*/
protected function create_and_setup_table($columns, $headers, $sortable, $collapsible, $suppress, $nosorting) {
protected function create_and_setup_table($columns, $headers, $sortable, $collapsible, $suppress, $nosorting,
$caption = '', $captionattribute = '') {
$table = new flexible_table('tablelib_test');
$table->define_columns($columns);
@ -112,6 +119,9 @@ class tablelib_test extends \advanced_testcase {
foreach ($nosorting as $column) {
$table->no_sorting($column);
}
if ($caption) {
$table->set_caption($caption, $captionattribute);
}
$table->setup();
return $table;
@ -784,4 +794,39 @@ class tablelib_test extends \advanced_testcase {
];
}
/**
* Data test for set and render caption for table.
*
* @covers ::set_caption_for_table
* @covers ::render_caption_for_table
*/
public function test_set_and_render_caption_for_table(): void {
$data = $this->generate_data(10, 2);
$columns = $this->generate_columns(2);
$headers = $this->generate_headers(2);
$caption = 'Caption for table';
$captionattribute = ['class' => 'inline'];
$this->run_table_test(
$columns,
$headers,
// Sortable.
true,
// Collapsible.
false,
// Suppress columns.
[],
// No sorting.
[],
// Data.
$data,
// Page size.
10,
// Caption for table.
$caption,
// Caption attribute.
$captionattribute,
);
$this->expectOutputRegex('/' . '<caption class="inline">' . $caption . '<\/caption>' . '/');
}
}

View File

@ -2,6 +2,10 @@ This files describes API changes in core libraries and APIs,
information provided here is intended especially for developers.
=== 4.1.5 ===
* Added a new render of caption for the table in render_caption. It can be used by
set_caption($caption, $captionattributes).
e.g. $caption = 'Caption for table'
e.g. $captionattributes = ['class' => 'inline'];
* Added new \admin_setting::is_forceable() method to determine whether the setting can be overridden or not. Therefore,
whether the settings can be overriden or not will depend on the value of implemented \admin_setting::is_forceable() method,
even if we define the settings in config.php.