Skip to content

Commit 916e7f3

Browse files
committed
Satus updated
1 parent 0820959 commit 916e7f3

2 files changed

Lines changed: 105 additions & 33 deletions

File tree

satus.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,26 @@
13381338

13391339
max-width: 100px;
13401340
}
1341+
1342+
1343+
1344+
.satus-table__paging > button
1345+
{
1346+
min-width: 32px;
1347+
height: 32px;
1348+
padding: 0 8px;
1349+
1350+
cursor: pointer;
1351+
1352+
color: var(--satus-theme-on-surface, #555);
1353+
border: none;
1354+
background: transparent;
1355+
}
1356+
1357+
.satus-table__paging > button.active
1358+
{
1359+
color: #2979ff;
1360+
}
13411361

13421362
/*--------------------------------------------------------------
13431363
>>> TEXT

satus.js

Lines changed: 85 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1889,62 +1889,60 @@ Satus.components.table = function(item) {
18891889
component_body.className = 'satus-table__body';
18901890

18911891
function update(data) {
1892+
var pages = Math.ceil(component.data.length / component.paging),
1893+
start = Math.max((component.pagingIndex - 1) * component.paging, 0),
1894+
end = component.pagingIndex * component.paging;
1895+
1896+
if (end > data.length) {
1897+
end = data.length;
1898+
} else if (end === 0) {
1899+
end = component.paging;
1900+
}
1901+
18921902
tbody.innerHTML = '';
18931903

18941904
if (data) {
1895-
for (var i = 0, l = data.length; i < l; i++) {
1896-
var tr = document.createElement('tr');
1905+
for (var i = start, l = end; i < l; i++) {
1906+
if (data[i]) {
1907+
var tr = document.createElement('tr');
18971908

1898-
for (var j = 0, k = data[i].length; j < k; j++) {
1899-
var td = document.createElement('td'),
1900-
span = document.createElement('span');
1909+
for (var j = 0, k = data[i].length; j < k; j++) {
1910+
var td = document.createElement('td'),
1911+
span = document.createElement('span');
19011912

1902-
span.innerText = data[i][j];
1913+
span.innerText = data[i][j];
19031914

1904-
td.appendChild(span);
1905-
tr.appendChild(td);
1906-
}
1915+
td.appendChild(span);
1916+
tr.appendChild(td);
1917+
}
19071918

1908-
tbody.appendChild(tr);
1919+
tbody.appendChild(tr);
1920+
}
19091921
}
19101922
}
1923+
1924+
component.pagingUpdate();
19111925
}
19121926

19131927
function sortArray(array, index, mode) {
19141928
if (mode === 'asc') {
1915-
if (typeof array[0][0] === 'number') {
1929+
if (typeof array[0][index] === 'number') {
19161930
sorted = array.sort(function(a, b) {
19171931
return a[index] - b[index];
19181932
});
19191933
} else {
19201934
sorted = array.sort(function(a, b) {
1921-
if (a[index] < b[index]) {
1922-
return -1;
1923-
}
1924-
1925-
if (a[index] > b[index]) {
1926-
return 1;
1927-
}
1928-
1929-
return 0;
1935+
return a[index].localeCompare(b[index]);
19301936
});
19311937
}
19321938
} else {
1933-
if (typeof array[0][0] === 'number') {
1939+
if (typeof array[0][index] === 'number') {
19341940
sorted = array.sort(function(a, b) {
19351941
return b[index] - a[index];
19361942
});
19371943
} else {
19381944
sorted = array.sort(function(a, b) {
1939-
if (a[index] < b[index]) {
1940-
return 1;
1941-
}
1942-
1943-
if (a[index] > b[index]) {
1944-
return -1;
1945-
}
1946-
1947-
return 0;
1945+
return b[index].localeCompare(a[index]);
19481946
});
19491947
}
19501948
}
@@ -2004,14 +2002,68 @@ Satus.components.table = function(item) {
20042002
component.appendChild(component_body);
20052003

20062004
component.data = item.data;
2005+
component.paging = item.paging;
2006+
component.pagingIndex = 0;
2007+
20072008
component.update = function(data, index, mode) {
2008-
this.querySelectorAll('.satus-table__head > div')[index].dataset.sorting = mode;
2009+
if (Satus.isset(data)) {
2010+
this.data = data;
2011+
}
2012+
2013+
if (Satus.isset(index) && Satus.isset(mode)) {
2014+
this.querySelectorAll('.satus-table__head > div')[index].dataset.sorting = mode;
20092015

2010-
this.data = data;
2016+
this.data = sortArray(this.data, index, mode);
2017+
}
20112018

2012-
update(sortArray(this.data, index, mode));
2019+
update(this.data);
20132020
};
20142021

2022+
2023+
// PAGING
2024+
2025+
function pagingUpdate() {
2026+
if (typeof this.paging === 'number') {
2027+
var pages = Math.ceil(this.data.length / this.paging);
2028+
2029+
this.querySelector('.satus-table__paging').innerHTML = '';
2030+
2031+
for (var i = 1; i <= pages; i++) {
2032+
var button = document.createElement('button');
2033+
2034+
if (i === (this.pagingIndex || 1)) {
2035+
button.className = 'active';
2036+
}
2037+
2038+
button.innerText = i;
2039+
button.parentComponent = this;
2040+
button.addEventListener('click', function() {
2041+
if (this.parentNode.querySelector('button.active')) {
2042+
this.parentNode.querySelector('button.active').classList.remove('active');
2043+
}
2044+
2045+
this.classList.add('active');
2046+
2047+
this.parentComponent.pagingIndex = Number(this.innerText);
2048+
this.parentComponent.update(this.parentComponent.data);
2049+
});
2050+
2051+
this.querySelector('.satus-table__paging').appendChild(button);
2052+
}
2053+
}
2054+
}
2055+
2056+
component.pagingUpdate = pagingUpdate;
2057+
2058+
component_paging = document.createElement('div');
2059+
2060+
component_paging.className = 'satus-table__paging';
2061+
2062+
component_scrollbar.appendChild(component_paging);
2063+
2064+
// END PAGING
2065+
2066+
20152067
var sorting;
20162068

20172069
for (var i = 0, l = item.columns.length; i < l; i++) {

0 commit comments

Comments
 (0)