You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.7 KiB
120 lines
3.7 KiB
document.addEventListener('DOMContentLoaded', function () {
|
|
function label(element, value) {
|
|
if (element.hasAttribute('value')) {
|
|
if (value !== undefined) {
|
|
element.setAttribute('value', value)
|
|
} else {
|
|
return element.getAttribute('value')
|
|
}
|
|
} else {
|
|
if (value !== undefined) {
|
|
element.textContent = value
|
|
} else {
|
|
return element.textContent
|
|
}
|
|
}
|
|
}
|
|
|
|
function countdownDeleteButton(event) {
|
|
const btn = event.target
|
|
const countdown = btn.getAttribute('data-countdown')
|
|
|
|
if (countdown !== '1') {
|
|
event.preventDefault()
|
|
btn.classList.add('__on-countdown')
|
|
|
|
const hasCountdown = countdown !== null
|
|
const newCountdown = hasCountdown ? countdown - 1 : 3
|
|
|
|
if (!hasCountdown) {
|
|
btn.setAttribute('data-label', label(btn))
|
|
}
|
|
|
|
label(btn, newCountdown)
|
|
btn.setAttribute('data-countdown', newCountdown)
|
|
setTimeout(countdownDeleteButton, 1000, event)
|
|
} else {
|
|
label(btn, btn.getAttribute('data-label'))
|
|
}
|
|
}
|
|
|
|
document.querySelectorAll('.delete-btn').forEach(function (btn) {
|
|
btn.addEventListener('click', countdownDeleteButton)
|
|
})
|
|
|
|
function dynamicTables() {
|
|
const pageSize = 25
|
|
|
|
const range = (x, y) =>
|
|
x > y ? [] : [x, ...range(x + 1, y)];
|
|
const visibleRows = page =>
|
|
range(page * pageSize, (page + 1) * pageSize - 1)
|
|
|
|
function toggleVisibilities(tbl, ixsToShow) {
|
|
const rows = [...tbl.tBodies[0].rows]
|
|
rows.forEach(function (row, ix) {
|
|
row.style.display = ixsToShow.includes(ix) ? 'table-row' : 'none';
|
|
})
|
|
}
|
|
|
|
function prevClick(tbl) {
|
|
const page = tbl.getAttribute('data-page') * 1
|
|
tbl.setAttribute('data-page', page - 1)
|
|
toggleVisibilities(tbl, visibleRows(page - 1))
|
|
}
|
|
|
|
function nextClick(tbl) {
|
|
const page = tbl.getAttribute('data-page') * 1
|
|
tbl.setAttribute('data-page', page + 1)
|
|
toggleVisibilities(tbl, visibleRows(page + 1))
|
|
}
|
|
|
|
function addToolbar(tbl) {
|
|
const toolbar = document.createElement('section')
|
|
toolbar.classList.add('tbl-toolbar')
|
|
|
|
const prevBtn = document.createElement('button')
|
|
prevBtn.classList.add('prev')
|
|
prevBtn.innerText = 'Prev'
|
|
prevBtn.addEventListener('click', function () {
|
|
prevClick(tbl)
|
|
})
|
|
toolbar.insertAdjacentElement('beforeend', prevBtn)
|
|
|
|
const nextBtn = document.createElement('button')
|
|
nextBtn.classList.add('next')
|
|
nextBtn.innerText = 'Next'
|
|
nextBtn.addEventListener('click', function () {
|
|
nextClick(tbl)
|
|
})
|
|
toolbar.insertAdjacentElement('beforeend', nextBtn)
|
|
|
|
tbl.insertAdjacentElement('afterend', toolbar)
|
|
}
|
|
|
|
document.querySelectorAll('table').forEach(function (tbl) {
|
|
toggleVisibilities(tbl, visibleRows(0))
|
|
tbl.setAttribute('data-page', 0)
|
|
addToolbar(tbl)
|
|
})
|
|
}
|
|
|
|
dynamicTables()
|
|
})
|
|
|
|
/*
|
|
$(document).ready(function() {
|
|
$('table').DataTable({
|
|
stateSave: true,
|
|
fixedHeader: true,
|
|
lengthMenu: [[25, 50, 100, -1], [25, 50, 100, "All"]],
|
|
stateSaveCallback: function(settings,data) {
|
|
localStorage.setItem('DataTables', JSON.stringify(data))
|
|
},
|
|
stateLoadCallback: function(settings) {
|
|
return JSON.parse(localStorage.getItem('DataTables'))
|
|
}
|
|
})
|
|
})
|
|
*/
|