xxxxxxxxxx
1
<div id="datatable"></div>
1
1
xxxxxxxxxx
1
const basicData = {
2
columns: ['Name', 'Position', 'Office', 'Age', 'Start date', 'Salary'],
3
rows: [
4
["Tiger Nixon", "System Architect", "Edinburgh", "61", "2011/04/25", "$320,800"],
5
["Garrett Winters", "Accountant", "Tokyo", "63", "2011/07/25", "$170,750"],
6
["Ashton Cox", "Junior Technical Author", "San Francisco", "66", "2009/01/12", "$86,000"],
7
["Cedric Kelly", "Senior Javascript Developer", "Edinburgh", "22", "2012/03/29", "$433,060"],
8
["Airi Satou", "Accountant", "Tokyo", "33", "2008/11/28", "$162,700"],
9
["Brielle Williamson", "Integration Specialist", "New York", "61", "2012/12/02", "$372,000"],
10
["Herrod Chandler", "Sales Assistant", "San Francisco", "59", "2012/08/06", "$137,500"],
11
["Rhona Davidson", "Integration Specialist", "Tokyo", "55", "2010/10/14", "$327,900"],
12
["Colleen Hurst", "Javascript Developer", "San Francisco", "39", "2009/09/15", "$205,500"],
13
["Sonya Frost", "Software Engineer", "Edinburgh", "23", "2008/12/13", "$103,600"],
14
["Jena Gaines", "Office Manager", "London", "30", "2008/12/19", "$90,560"],
15
["Quinn Flynn", "Support Lead", "Edinburgh", "22", "2013/03/03", "$342,000"],
16
["Charde Marshall", "Regional Director", "San Francisco", "36", "2008/10/16", "$470,600"],
17
["Haley Kennedy", "Senior Marketing Designer", "London", "43", "2012/12/18", "$313,500"]
18
],
19
};
20
21
new mdb.Datatable(document.getElementById('datatable'), basicData)
22
23
24
const addClassToRow = () => {
25
// Get the table element
26
const table = document.querySelector(".datatable-body");
27
// Get all the rows in the table
28
const rows = table.querySelectorAll("tr");
29
// Loop through each row
30
for (let i = 0; i < rows.length; i++) {
31
// Get the first cell in the row
32
const firstCell = rows[i].querySelector("td");
33
// Get the value of the first word in first cell
34
const value = firstCell.innerHTML.split(' ')[0].toLowerCase()
35
36
// Add the value as a class to the row
37
rows[i].classList.add(value);
38
}
39
}
40
41
addClassToRow();
Console errors: 0