123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- (async function responseData() {
- const response = await fetch('https://jsonplaceholder.typicode.com/users');
- const users = await response.json();
- const table = document.getElementById("main");
- var tr = table.rows;
- for (var i = 1; i < tr.length; i++) {
- for (var j = 0; j < tr[i].children.length; j++) {
- switch (j) {
- case 0 : {
- tr[i].children[j].innerHTML = users[i].name;
- break;
- }
- case 1: {
- tr[i].children[j].innerHTML = users[i].username;
- break;
- }
- case 2 : {
- tr[i].children[j].innerHTML = users[i].email;
- break;
- }
- case 3 : {
- tr[i].children[j].innerHTML = users[i].website;
- break;
- }
- }
- }
- }
- for (let i = 1; i < tr.length; i++) {
- tr[i].addEventListener("click", function () {
- const info = document.querySelector(".information");
- const values = [];
- info.innerHTML = `${getProp(users[i], 3, [])}`;
- function getProp(obj, k, values) {
- let count = 1;
- for(var i in obj) {
- if(typeof(obj[i]) === 'object') {
- getProp(obj[i], k - 1, values );
- } else {
- values.push(obj[i]);
- if ((k === 1) && (count ===obj[i].length)){
- return null;
- }
- }
- count++;
- }
- return values;
- }
- info.style.display = "block";
- info.addEventListener("click", function () {
- info.style.display = "none";
- })
- })
- }
- function sortIncrease(e) {
- if (e.target.tagName != "TH") return;
- const index = e.target.cellIndex;
- const tbody = table.getElementsByTagName('tbody')[0];
- const rowsArray = [];
- for (let i = 0; i < tbody.rows.length; i++) {
- rowsArray.push(tbody.rows[i].children[index].innerHTML)
- }
- rowsArray.sort();
- for (let i = 0; i < tbody.rows.length; i++) {
- tbody.rows[i].children[index].innerHTML = rowsArray[i];
- }
- table.removeEventListener("click", sortIncrease)
- table.addEventListener("click", sortDecrease);
- }
- function sortDecrease(e) {
- if (e.target.tagName != "TH") return;
- const index = e.target.cellIndex;
- const tbody = table.getElementsByTagName('tbody')[0];
- const rowsArray = [];
- for (let i = 0; i < tbody.rows.length; i++) {
- rowsArray.push(tbody.rows[i].children[index].innerHTML)
- }
- rowsArray.reverse();
- for (let i = 0; i < tbody.rows.length; i++) {
- tbody.rows[i].children[index].innerHTML = rowsArray[i];
- }
- table.removeEventListener("click", sortDecrease)
- table.addEventListener("click", sortIncrease);
- }
- table.addEventListener("click", sortIncrease)
- })();
|