responsive-table-fluxbox/js/index3.js

118 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-05-20 20:42:52 +00:00
/*
* Add heads elements into row
*/
const createHeadsIntoRow = (table, row) => {
const cells = row.querySelectorAll(".td");
cells.forEach((cell, i) => {
createHeadIntoCell(table, cell, i);
});
};
2021-05-17 07:26:09 +00:00
2021-05-20 20:10:03 +00:00
/*
2021-05-20 20:42:52 +00:00
* Add head into cell
2021-05-20 20:10:03 +00:00
*/
2021-05-20 20:42:52 +00:00
const createHeadIntoCell = (table, cell, i) => {
const elem = document.createElement("div");
elem.classList.add("head");
elem.innerHTML = theadContent(table, i);
cell.appendChild(elem);
}
2021-05-17 07:26:09 +00:00
2021-05-20 20:42:52 +00:00
/*
* Get text in columns thead
*/
const theadContent = (table, i) => {
const thead = table.querySelectorAll(".thead .td");
let content;
thead.forEach((item, j) => {
if (i === j) {
content = item.textContent;
}
}, i);
return content;
};
/*
* Remove heads from element
*/
const removeHeadsFrom = (element) => {
const heads = element.querySelectorAll(".head");
heads.forEach(head => {
head.parentNode.removeChild(head);
});
};
/*
* Insert head before cell
*/
const insertHeadBeforeCell = (row, cell) => {
let head = cell.querySelector('.head');
head.classList.add("td");
row.insertBefore(head, cell);
};
/*
* Loop on each table
*/
2021-05-20 20:10:03 +00:00
tables.forEach(t => {
2021-05-20 20:42:52 +00:00
const table = document.querySelector(`#${t.id}`);
2021-05-20 20:10:03 +00:00
/*
2021-05-20 20:42:52 +00:00
* Responsive rules limits
2021-05-20 20:10:03 +00:00
*/
2021-05-20 20:42:52 +00:00
const minLG = window.matchMedia(`(min-width: ${t.md_lg + 1}px)`);
const maxMD = window.matchMedia(`(max-width: ${t.md_lg}px)`);
const maxSM = window.matchMedia(`(max-width: ${t.sm_md}px)`);
2021-05-20 20:10:03 +00:00
/*
2021-05-20 20:42:52 +00:00
* Medium screens rule
2021-05-20 20:10:03 +00:00
*/
const mediumScreen = rule => {
if (rule.matches) {
console.log('medium');
const rows = [...table.querySelectorAll(".tr")];
rows.shift(); // vire le premier rang
2021-05-20 20:42:52 +00:00
rows.forEach(row => {
createHeadsIntoRow(table, row);
2021-05-17 07:26:09 +00:00
});
2021-05-20 20:10:03 +00:00
} else {
console.log('not medium');
removeHeadsFrom(table);
}
};
/*
2021-05-20 20:42:52 +00:00
* Small screens rule
2021-05-20 20:10:03 +00:00
*/
const smallScreen = (rule) => {
if (rule.matches) {
console.log('small');
const rows = [...table.querySelectorAll(".tr")];
rows.shift();
rows.forEach(row => {;
const cells = row.querySelectorAll(".td");
2021-05-20 20:42:52 +00:00
cells.forEach(cell => {
insertHeadBeforeCell(row, cell);
2021-05-20 20:10:03 +00:00
});
2021-05-17 07:26:09 +00:00
});
2021-05-20 20:10:03 +00:00
} else {
console.log('not small');
const rows = [...table.querySelectorAll(".tr")];
rows.shift();
rows.forEach(row => {;
removeHeadsFrom(row);
2021-05-20 20:42:52 +00:00
// Large screen exception !
2021-05-20 20:10:03 +00:00
if (!minLG.matches) {
2021-05-20 20:42:52 +00:00
createHeadsIntoRow(table, row);
2021-05-20 20:10:03 +00:00
}
2021-05-17 07:26:09 +00:00
});
2021-05-20 20:10:03 +00:00
}
};
mediumScreen(maxMD);
maxMD.addListener(mediumScreen);
smallScreen(maxSM);
maxSM.addListener(smallScreen);
});