xxxxxxxxxx
1
<div class="container my-5 animation fade-in">
2
3
<h1 class="mb-5">Sortable <span class="text-danger">Table</span></h1>
4
<p class="mb-5">
5
The code snippet is <strong>re-initializing popover</strong> components within a data
6
table every time the table is sorted. This is necessary because sorting
7
the table can dynamically alter or recreate elements in the DOM,
8
potentially disrupting the previously initialized popovers.
9
By re-initializing them after each sort action, we ensure that the
10
popover functionality is maintained regardless of how the table's
11
content is reordered.
12
</p>
13
14
<div class="datatable" id="my-datatable">
15
<table>
16
<thead>
17
<tr>
18
<th class="th-sm">Button 1 (Click here to sort)</th>
19
</tr>
20
</thead>
21
<tbody>
22
<tr>
23
<td><button
24
type="button"
25
data-mdb-toggle="popover"
26
class="btn btn-danger"
27
title="Popover title"
28
data-mdb-content="And here's some amazing content. It's very engaging. Right?"
29
data-mdb-trigger="focus"
30
>
31
Click to toggle popover
32
</button></td>
33
</tr>
34
<tr>
35
<td><button
36
type="button"
37
data-mdb-toggle="popover"
38
class="btn btn-dark"
39
title="Popover title"
40
data-mdb-content="And here's some amazing content. It's very engaging. Right?"
41
data-mdb-trigger="focus"
42
>
43
Click to toggle popover
44
</button></td>
45
</tr>
46
</tbody>
47
</table>
48
</div>
49
50
</div>
51
52
<footer class="text-center bg-body-tertiary">
53
<!-- Grid container -->
54
<div class="container pt-4">
55
<!-- Section: Social media -->
56
<section class="mb-4">
57
<!-- Facebook -->
58
<a
59
class="btn btn-link btn-floating btn-lg text-body m-1"
60
href="#!"
61
role="button"
62
data-mdb-ripple-color="dark"
63
><i class="fab fa-facebook-f"></i
64
></a>
65
66
<!-- Twitter -->
67
<a
68
class="btn btn-link btn-floating btn-lg text-body m-1"
69
href="#!"
70
role="button"
71
data-mdb-ripple-color="dark"
72
><i class="fab fa-twitter"></i
73
></a>
74
75
<!-- Google -->
76
<a
77
class="btn btn-link btn-floating btn-lg text-body m-1"
78
href="#!"
79
role="button"
80
data-mdb-ripple-color="dark"
81
><i class="fab fa-google"></i
82
></a>
83
84
<!-- Instagram -->
85
<a
86
class="btn btn-link btn-floating btn-lg text-body m-1"
87
href="#!"
88
role="button"
89
data-mdb-ripple-color="dark"
90
><i class="fab fa-instagram"></i
91
></a>
92
93
<!-- Linkedin -->
94
<a
95
class="btn btn-link btn-floating btn-lg text-body m-1"
96
href="#!"
97
role="button"
98
data-mdb-ripple-color="dark"
99
><i class="fab fa-linkedin"></i
100
></a>
101
<!-- Github -->
102
<a
103
class="btn btn-link btn-floating btn-lg text-body m-1"
104
href="#!"
105
role="button"
106
data-mdb-ripple-color="dark"
107
><i class="fab fa-github"></i
108
></a>
109
</section>
110
<!-- Section: Social media -->
111
</div>
112
<!-- Grid container -->
113
114
<!-- Copyright -->
115
<div class="text-center p-3" style="background-color: rgba(0, 0, 0, 0.05);">
116
© 2020 Copyright:
117
<a class="text-body" href="https://mdbootstrap.com/">MDBootstrap.com</a>
118
</div>
119
<!-- Copyright -->
120
</footer>
1
1
xxxxxxxxxx
1
const datatable = document.getElementById('my-datatable');
2
const datatableInstance = new mdb.Datatable(datatable, { selectable: true, multi: true });
3
4
initPopovers();
5
datatable.addEventListener('render.mdb.datatable', () => {
6
initPopovers();
7
});
8
9
function initPopovers(){
10
const popoversEl = datatable.querySelectorAll('[data-mdb-toggle="popover"]');
11
popoversEl.forEach((popoverEl) => {
12
new mdb.Popover(popoverEl);
13
});
14
}
15
Console errors: 0