Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
---
2
layout: docs
3
title: "Dynamic tabs"
4
date: 2021-10-02T09:40:32+01:00
5
draft: false
6
tags:
7
- MDL-71943
8
- 4.0
9
---
10
 
11
## How it works
12
 
13
Dynamic tabs are tabs that load content in AJAX requests. Once the user clicks on the tab heading, the page does not reload but the content of the respective tab loads in AJAX request.
14
 
15
## Source files
16
 
17
* `lib/amd/src/dynamic_tabs.js`
18
* `lib/amd/src/local/repository/dynamic_tabs.js`
19
* `lib/classes/external/dynamic_tabs_get_content.php`
20
* `lib/classes/output/dynamic_tabs.php`
21
* `lib/classes/output/dynamic_tabs/base.php`
22
* `lib/db/services.php`
23
* `lib/templates/dynamic_tabs.mustache`
24
 
25
## How to use dynamic tabs
26
 
27
First of all we need to create a tab class file for each tab that we need, extending `lib/classes/output/dynamic_tabs/base.php`.
28
 
29
These tab classes need to include these 4 methods in order to work:
30
 
31
* `export_for_template` returns the data we export to the template
32
* `get_tab_label` returns the tab title
33
* `is_available` checks the tab permission and returns true/false that will enable/disable the individual tab
34
* `get_template` returns the path to the tab template file
35
 
36
{{< php >}}
37
class tab1 extends base {
38
 
39
    /**
40
     * Export this for use in a mustache template context.
41
     *
42
     * @param renderer_base $output
43
     *
44
     * @return stdClass
45
     */
46
    public function export_for_template(renderer_base $output) {
47
        $content = (object)[];
48
        $content->customtext = 'Tab 1 content example';
49
        return $content;
50
    }
51
 
52
    /**
53
     * The label to be displayed on the tab
54
     *
55
     * @return string
56
     */
57
    public function get_tab_label(): string {
58
        return 'Tab 1';
59
    }
60
 
61
    /**
62
     * Check permission of the current user to access this tab
63
     *
64
     * @return bool
65
     */
66
    public function is_available(): bool {
67
        // Define the correct permissions here.
68
        return true;
69
    }
70
 
71
    /**
72
     * Template to use to display tab contents
73
     *
74
     * @return string
75
     */
76
    public function get_template(): string {
77
        return 'tool_componentlibrary/dynamictabs_tab1';
78
    }
79
}
80
{{< / php >}}
81
 
82
Then we need to create the templates that each `get_template` method will call.
83
 
84
Finally, to add dynamic tabs to our page, we just need to call all the previously created tabs, and pass the attributes
85
needed in each tab.
86
 
87
These attributes will be stored as "data attributes" in the DOM and can be also used inside our tab classes
88
using `get_data` method (for example to check permissions in `is_available`).
89
 
90
{{< php >}}
91
    $tabs = [
92
        new tab1(['demotab' => 'Tab1', 'reportid' => $reportid]),
93
        new tab2(['demotab' => 'Tab2']),
94
    ];
95
    echo $OUTPUT->render_from_template('core/dynamic_tabs', (new dynamic_tabs($tabs))->export_for_template($OUTPUT));
96
{{< / php >}}
97
 
98
## Example
99
 
100
<iframe src="../../../../examples/dynamictabs.php" style="overflow:hidden;height:400px;width:100%;border:0" title="Moodle dynamic tabs"></iframe>