-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_1.js
48 lines (39 loc) · 1.74 KB
/
app_1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// import the data from data.js
var tableData= data;
// Reference the HTML Table. d3.select to tell java to look for <tbody> tags in HTML
var tbody= d3.select("tbody");
function buildTable(data){
// First, clear out any existing data
tbody.html("");
// Next, loop through each object in the data
// and append a row and cells for each value in the row
data.forEach((dataRow) => {
let row=tbody.append("tr");
//Object.values tells java to reference 1 object from the array of UFO sighitings
//Adding (dataRow) as the argument we are saying we want values to go into the dataRow
Object.values(dataRow).forEach((val)=> {
let cell=row.append("td");
cell.text(val);
});
})
}
// Filter out table
function handleClick() {
// #datetime is our selector string, it is what d3 is looking for. Our id will be datetime
// .property("value") is actually grabbing the values gotten by d3
// Check to see if a date was entered and filter the data using that date.
let date= d3.select("#datetime").property("value");
let filteredData= tableData;
if (date){
// Show only the rows where the date is equal to the date filter we created above, (===) means strict equality, (==) means loose eqaulity
filteredData=filteredData.filter((row) => row.datetime === date);
}
// Rebuild the table using the filtered data
// @NOTE: If no date was entered, then filteredData will
// just be the original tableData.
buildTable(filteredData);
}
// Attatch an event to listen for the form button
d3.selectAll("#filter-btn").on("click", handleClick);
//Build the table when the page loads
buildTable(tableData);