xxxxxxxxxx
1
<div id="datatable" data-mdb-loading="true"></div>
1
1
xxxxxxxxxx
1
const columns = [
2
{ label: 'Address', field: 'address' },
3
{ label: 'Company', field: 'company' },
4
//{ label: 'Email', field: 'email' },
5
{ label: 'Name', field: 'name' },
6
// { label: 'Phone', field: 'phone' },
7
{ label: 'Username', field: 'username' },
8
{ label: 'Contact', field: 'contact' },
9
];
10
11
const asyncTable = new mdb.Datatable(
12
document.getElementById('datatable'),
13
{ columns, },
14
{ loading: true }
15
);
16
17
const setActions = () => {
18
document.getElementsByClassName('call-btn').forEach(btn => {
19
btn.addEventListener('click', () => {
20
console.log(`call ${btn.attributes['data-mdb-number'].value}`)
21
})
22
})
23
24
document.getElementsByClassName('message-btn').forEach(btn => {
25
btn.addEventListener('click', () => {
26
console.log(`send a message to ${btn.attributes['data-mdb-email'].value}`)
27
})
28
})
29
}
30
31
asyncTable._element.addEventListener('render.mdb.datatable', setActions);
32
33
fetch('https://jsonplaceholder.typicode.com/users')
34
.then((response) => response.json())
35
.then((data) => {
36
asyncTable.update(
37
{
38
rows: data.map((user) => ({
39
...user,
40
address: `${user.address.city}, ${user.address.street}`,
41
company: user.company.name,
42
contact: `
43
<button class="call-btn btn btn-outline-primary btn-floating btn-sm" data-mdb-number="${user.phone}"><i class="fa fa-phone"></i></button>
44
<button class="message-btn btn ms-2 btn-primary btn-floating btn-sm" data-mdb-email="${user.email}"><i class="fa fa-envelope"></i></button>
45
`,
46
})),
47
},
48
{ loading: false }
49
);
50
});
51
52
53
Console errors: 0