-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
138 lines (132 loc) · 3.43 KB
/
scripts.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// This is important! This is where the data comes from.
const BASE_URL = "https://swapi-json-server.onrender.com/planets";
// filters -- any functions you might want to apply to cell data can go here
const filters = {
intComma: (val) => {
if (Number.isNaN(parseInt(val, 10))) {
return val;
}
return parseInt(val, 10).toLocaleString();
},
};
window.dataTable = () => ({
// properties
// define your columns
columns: [
{
name: "Name",
attribute: "name",
sortable: "name",
classNames: "font-medium",
},
{
name: "Size",
attribute: "diameter",
sortable: "diameter",
format: filters.intComma,
},
{
name: "Terrain",
attribute: "terrain",
},
{
name: "Climate",
attribute: "climate",
},
{
name: "Pop.",
attribute: "population",
sortable: "population",
format: filters.intComma,
},
],
rows: [],
loading: true,
fetchUrl: "",
searchInput: "",
prevSearch: "",
totalRecords: null,
pageSize: "10",
spreadSize: 3,
currentPage: 1,
sortAttribute: "",
sortDirection: "asc",
prevSort: "",
prevPageSize: "",
// methods
initData() {
this.fetchData();
},
fetchData() {
this.loading = true;
// if search, sort, or page size changes, reset to page 1
if (
this.prevSearch !== this.searchInput ||
this.prevSort !== this.sortAttribute + this.sortDirection ||
this.prevPageSize !== this.pageSize
) {
this.currentPage = 1;
}
// construct url
const params = new URLSearchParams();
params.append("_page", this.currentPage);
params.append("_limit", this.pageSize);
if (this.searchInput) {
params.append("q", this.searchInput);
}
if (this.sortAttribute) {
params.append("_sort", this.sortAttribute);
params.append("_order", this.sortDirection);
}
this.fetchUrl = `${BASE_URL}?${params.toString()}`;
fetch(this.fetchUrl)
.then((response) => {
// json-server returns total records in a header ¯\_(ツ)_/¯
this.totalRecords = response.headers.get("x-total-count");
return response.json();
})
.then((data) => {
this.rows = data;
this.loading = false;
// store the previously used params to compare against.
// I think this is easier than setting up a watch method.
this.prevSearch = this.searchInput;
this.prevSort = this.sortAttribute + this.sortDirection;
this.prevPageSize = this.pageSize;
});
},
goToPage(n) {
this.currentPage = n;
this.fetchData();
},
sortBy(sortAttribute) {
// toggle up, toggle down, toggle off
if (this.sortAttribute !== sortAttribute) {
this.sortAttribute = sortAttribute;
this.sortDirection = "asc";
} else if (this.sortDirection === "asc") {
this.sortDirection = "desc";
} else {
this.sortAttribute = "";
this.sortDirection = "asc";
}
this.fetchData();
},
// getters
pageSpread() {
const range = [];
const start = Math.max(this.currentPage - (this.spreadSize - 1), 1);
const end = Math.min(
this.currentPage + (this.spreadSize - 1),
Math.ceil(this.totalRecords / parseInt(this.pageSize, 10))
);
for (let i = start; i <= end; i += 1) {
range.push(i);
}
return range;
},
// computed
get totalPages() {
return Math.ceil(this.totalRecords / parseInt(this.pageSize, 10));
},
});