From ad2404b3d088d7bbfca198946ca7cb2a31e115b3 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Fri, 11 Aug 2023 14:51:05 +0200 Subject: [PATCH 01/33] update --- app.ts | 257 ++++++++++++++++++++++++++++++++++++++++++++++ index.html | 28 ++++- package-lock.json | 27 +++++ package.json | 27 +++++ style.css | 92 +++++++++++++++++ 5 files changed, 430 insertions(+), 1 deletion(-) create mode 100644 app.ts create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 style.css diff --git a/app.ts b/app.ts new file mode 100644 index 00000000..e2a965f3 --- /dev/null +++ b/app.ts @@ -0,0 +1,257 @@ + +async function fetchRecordCount(): Promise { + try { + const recordCount = await fetch(`http://192.168.4.133:2050/recordCount`); + + if (!recordCount.ok) { + throw new Error('Failed to fetch record count'); + } + const data = await recordCount.json() + return data; + + } catch (error) { + console.error('Error fetching the record count:', error); + throw error; + } + + + +} + + + + +function fetchColumns(): void { + fetch("http://192.168.4.133:2050/columns") + .then((response: Response) => { + + return response.json() as Promise; + }) + .then((columns: string[]) => { + const colArray = columns; + console.log(colArray); + + for (let c = 0; c < colArray.length; c++) { + $(".head").append(`${colArray[c]}`); + } + }) + +} + +fetchColumns(); + +async function fetchRecords(): Promise { + try { + let count: number = await fetchRecordCount() - 1; + const response = await fetch(`http://192.168.4.133:2050/records?from=0&to=35`); + + if (!response.ok) { + throw new Error("Sorry, there's a problem in the network"); + } + const records = await response.json(); + return records; + + + }catch(error) { + console.error("Error fetching records:", error); + throw error; + } + } + + + + + +fetchRecords() + +async function displayRecords(): Promise { +try{ + const recArray: any[][] = await fetchRecords(); + + for (let r = 0; r <35; r++) { + $("tbody").append(``); + const lastRow = $(".row:last"); + for (let i = 0; i < recArray[r].length; i++) { + lastRow.append(`${recArray[r][i]}`); + + + } + } +}catch(error){ + console.error("Error displaying records:", error); +} +} +displayRecords() + + + +async function rightArrow(): Promise { + let count: number = await fetchRecordCount() - 1; + console.log(count); + const firstRow = document.querySelector("#recordsTable tbody"); + console.log(firstRow) + if (firstRow) { + const cells = firstRow.querySelectorAll("td"); + const firstRecord: string[] = []; + cells.forEach((cell) => { + firstRecord.push(cell.textContent || ""); + }); + console.log("First Record:", firstRecord[0]); + + + const firstID = parseFloat(firstRecord[0]); + console.log(firstID) + + if (0 <= firstID && firstID <= (count - 35)) { + let clearTable = document.querySelector('tbody') as HTMLTableSectionElement | null; + if (clearTable) { + clearTable.innerHTML = ""; + + let firstRecord = + firstID + 35 + let lastRecord = + firstRecord + 34 + + fetch(`http://192.168.4.133:2050/records?from=${firstRecord}&to=${lastRecord}`) + .then((response: Response) => { + + if (!response.ok) { + throw new Error("Sorry, there's a problem in the network"); + } + return response.json() as Promise; + }) + .then((records: any[]) => { + const recArray = records; + console.log(recArray); + + for (let r = 0; r <35; r++) { + $("tbody").append(``); + const lastRow = $(".row:last"); + for (let i = 0; i < recArray[r].length; i++) { + lastRow.append(`${recArray[r][i]}`); + + + } + } + }) + .catch((error) => { + console.error("Error fetching records:", error); + }); + + } + } + } +} + +async function leftArrow(): Promise { + let count: number = await fetchRecordCount() - 1; + console.log(count); + const firstRow = document.querySelector("#recordsTable tbody"); + console.log(firstRow) + if (firstRow) { + const cells = firstRow.querySelectorAll("td"); + const firstRecord: string[] = []; + cells.forEach((cell) => { + firstRecord.push(cell.textContent || ""); + }); + + + + const firstID = parseFloat(firstRecord[0]); + + + if (35 <= firstID && firstID <= (count)) { + let clearTable = document.querySelector('tbody') as HTMLTableSectionElement | null; + if (clearTable) { + clearTable.innerHTML = ""; + + let firstRecord = firstID - 35 + + let lastRecord =firstRecord + 34 + + + + fetch(`http://192.168.4.133:2050/records?from=${firstRecord}&to=${lastRecord}`) + .then((response: Response) => { + + if (!response.ok) { + throw new Error("Sorry, there's a problem in the network"); + } + return response.json() as Promise; + }) + .then((records: any[]) => { + const recArray = records; + console.log(recArray); + + for (let r = 0; r <35; r++) { + $("tbody").append(``); + const lastRow = $(".row:last"); + for (let i = 0; i < recArray[r].length; i++) { + lastRow.append(`${recArray[r][i]}`); + + + } + } + }) + .catch((error) => { + console.error("Error fetching records:", error); + }); + + } + } + } +} + + + + +$('#searchInput').on('keyup',async () => { + const inputValue = $('#searchInput').val() as string; + console.log(inputValue); + + + if(!inputValue.length){ + displayRecords() + } + const recArray: any[] = await fetchRecords(); + + + let filter = recArray.filter((item)=>{ + for (let i = 0; i < item.length; i++) { + if (item[i].toLowerCase().includes(inputValue.toLowerCase())) { + return true; + + } + + } + return false; + }); + const tbody = $("tbody"); + tbody.empty(); + + for (let r = 0; r < 35 && r < filter.length; r++) { + const row = $(''); + for (let i = 0; i < filter[r].length; i++) { + row.append(`${filter[r][i]}`); + } + tbody.append(row); + } + + }) + + + + + + + + + + + + + + + + + + + diff --git a/index.html b/index.html index add5e736..9fd1b027 100644 --- a/index.html +++ b/index.html @@ -3,10 +3,36 @@ JS Onboard Project + -

Hello

+ +

OnBoard-Javascript

+
+
+ +
+
+ + + + + + + + + + + + +
+ +
1 - 35 Records per Page
+ +
+ + diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..af2563bc --- /dev/null +++ b/package-lock.json @@ -0,0 +1,27 @@ +{ + "name": "onboard-javascript", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@types/jquery": { + "version": "3.5.16", + "resolved": "https://registry.npmjs.org/@types/jquery/-/jquery-3.5.16.tgz", + "integrity": "sha512-bsI7y4ZgeMkmpG9OM710RRzDFp+w4P1RGiIt30C1mSBT+ExCleeh4HObwgArnDFELmRrOpXgSYN9VF1hj+f1lw==", + "requires": { + "@types/sizzle": "*" + } + }, + "@types/sizzle": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/@types/sizzle/-/sizzle-2.3.3.tgz", + "integrity": "sha512-JYM8x9EGF163bEyhdJBpR2QX1R5naCJHC8ucJylJ3w9/CVBaskdQ8WqBf8MmQrd1kRvp/a4TS8HJ+bxzR7ZJYQ==" + }, + "typescript": { + "version": "3.8.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.3.tgz", + "integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==", + "dev": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 00000000..a1a024bf --- /dev/null +++ b/package.json @@ -0,0 +1,27 @@ +{ + "name": "onboard-javascript", + "version": "1.0.0", + "description": "This is a JavaScript project for all new developers to complete before venturing into our web frontend codebase.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "build": "tsc -p .", + "start": "npm run build -- -w" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/ThokozaniNqwili/onboard-javascript.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/ThokozaniNqwili/onboard-javascript/issues" + }, + "homepage": "https://github.com/ThokozaniNqwili/onboard-javascript#readme", + "devDependencies": { + "typescript": "^3.8.3" + }, + "dependencies": { + "@types/jquery": "^3.5.16" + } +} diff --git a/style.css b/style.css new file mode 100644 index 00000000..47760400 --- /dev/null +++ b/style.css @@ -0,0 +1,92 @@ +body{ + overflow: hidden; + background-color: #f7e7ce; +} +table{ + width: 100%; + table-layout: fixed; + +} +table, th, td { + border: 1px solid #4B3832; + border-collapse: collapse; + text-align: center; + color: black; +} + +td:hover{ + color: #97694F; + } + +.heading { + text-align: center; + text-decoration: underline; + color: #4B3832; + +} + +.showRecords{ + + transform: translate(49rem, -0.5rem); + position: absolute; + bottom: 0px; + padding: 1rem; + + +} +.showRecords #page{ + display: inline-block; + font-size: 1.2rem; + +} + + +.arrow-right, +.arrow-left{ + border: none; + background-color: transparent; + +} + +.arrow-right, +.arrow-left{ + display: inline-block; + width: 30px; + height: 30px; + border-top: 10px solid #4B3832; + border-left: 10px solid #4B3832; + + + +} + +.arrow-right, +.arrow-left:hover { + color: #97694F; +} + +.arrow-right{ + transform: rotate(135deg); + float: right; + + +} + +.arrow-left{ + transform: rotate(-45deg); + float: left; +} + +.search-container { + float: right; +} +.search-container input[type=text] { + padding: 6px; + margin-bottom: 3px; + font-size: 17px; + border: 2px solid #4B3832; + background-color: #f7e7ce; + +} + + From 575b160a26d1fa2b17cfc10785835e46ce5a1556 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Fri, 11 Aug 2023 16:03:17 +0200 Subject: [PATCH 02/33] update --- app.ts | 244 +++++++++++++++++++++++++++++++---------------------- index.html | 3 +- 2 files changed, 147 insertions(+), 100 deletions(-) diff --git a/app.ts b/app.ts index e2a965f3..0b53c873 100644 --- a/app.ts +++ b/app.ts @@ -2,7 +2,7 @@ async function fetchRecordCount(): Promise { try { const recordCount = await fetch(`http://192.168.4.133:2050/recordCount`); - + if (!recordCount.ok) { throw new Error('Failed to fetch record count'); } @@ -42,22 +42,22 @@ fetchColumns(); async function fetchRecords(): Promise { try { - let count: number = await fetchRecordCount() - 1; - const response = await fetch(`http://192.168.4.133:2050/records?from=0&to=35`); + let count: number = await fetchRecordCount() - 1; + const response = await fetch(`http://192.168.4.133:2050/records?from=0&to=35`); - if (!response.ok) { - throw new Error("Sorry, there's a problem in the network"); - } - const records = await response.json(); - return records; - - - }catch(error) { - console.error("Error fetching records:", error); - throw error; + if (!response.ok) { + throw new Error("Sorry, there's a problem in the network"); } + const records = await response.json(); + return records; + + + } catch (error) { + console.error("Error fetching records:", error); + throw error; } - +} + @@ -65,48 +65,48 @@ async function fetchRecords(): Promise { fetchRecords() async function displayRecords(): Promise { -try{ - const recArray: any[][] = await fetchRecords(); - - for (let r = 0; r <35; r++) { - $("tbody").append(``); - const lastRow = $(".row:last"); - for (let i = 0; i < recArray[r].length; i++) { - lastRow.append(``); - - - } + try { + const recArray: any[][] = await fetchRecords(); + + for (let r = 0; r < 35; r++) { + $("tbody").append(``); + const lastRow = $(".row:last"); + for (let i = 0; i < recArray[r].length; i++) { + lastRow.append(``); + + + } + } + } catch (error) { + console.error("Error displaying records:", error); } -}catch(error){ - console.error("Error displaying records:", error); -} } displayRecords() async function rightArrow(): Promise { + let searchValue = $('#searchInput').val() as string + let count: number = await fetchRecordCount() - 1; - console.log(count); + const firstRow = document.querySelector("#recordsTable tbody"); - console.log(firstRow) + if (firstRow) { const cells = firstRow.querySelectorAll("td"); const firstRecord: string[] = []; cells.forEach((cell) => { firstRecord.push(cell.textContent || ""); }); - console.log("First Record:", firstRecord[0]); - const firstID = parseFloat(firstRecord[0]); - console.log(firstID) + if (0 <= firstID && firstID <= (count - 35)) { let clearTable = document.querySelector('tbody') as HTMLTableSectionElement | null; if (clearTable) { clearTable.innerHTML = ""; - + let firstRecord = + firstID + 35 let lastRecord = + firstRecord + 34 @@ -120,17 +120,38 @@ async function rightArrow(): Promise { }) .then((records: any[]) => { const recArray = records; - console.log(recArray); - - for (let r = 0; r <35; r++) { - $("tbody").append(``); - const lastRow = $(".row:last"); - for (let i = 0; i < recArray[r].length; i++) { - lastRow.append(``); - - - } - } + if (searchValue) { + + let filter = recArray.filter((item) => { + for (let i = 0; i < item.length; i++) { + if (item[i].toLowerCase().includes(searchValue.toLowerCase())) { + return true; + + } + + } + return false; + }); + const tbody = $("tbody"); + tbody.empty(); + + for (let r = 0; r < 35 && r < filter.length; r++) { + const row = $(''); + for (let i = 0; i < filter[r].length; i++) { + row.append(``); + } + tbody.append(row); + } + + } else for (let r = 0; r < 35; r++) { + $("tbody").append(``); + const lastRow = $(".row:last"); + for (let i = 0; i < recArray[r].length; i++) { + lastRow.append(``); + + + } + } }) .catch((error) => { console.error("Error fetching records:", error); @@ -142,21 +163,22 @@ async function rightArrow(): Promise { } async function leftArrow(): Promise { + let searchValue = $('#searchInput').val() as string let count: number = await fetchRecordCount() - 1; - console.log(count); + const firstRow = document.querySelector("#recordsTable tbody"); - console.log(firstRow) + if (firstRow) { const cells = firstRow.querySelectorAll("td"); const firstRecord: string[] = []; cells.forEach((cell) => { firstRecord.push(cell.textContent || ""); }); - + const firstID = parseFloat(firstRecord[0]); - + if (35 <= firstID && firstID <= (count)) { let clearTable = document.querySelector('tbody') as HTMLTableSectionElement | null; @@ -164,10 +186,10 @@ async function leftArrow(): Promise { clearTable.innerHTML = ""; let firstRecord = firstID - 35 - - let lastRecord =firstRecord + 34 - - + + let lastRecord = firstRecord + 34 + + fetch(`http://192.168.4.133:2050/records?from=${firstRecord}&to=${lastRecord}`) .then((response: Response) => { @@ -179,17 +201,41 @@ async function leftArrow(): Promise { }) .then((records: any[]) => { const recArray = records; - console.log(recArray); - - for (let r = 0; r <35; r++) { - $("tbody").append(``); - const lastRow = $(".row:last"); - for (let i = 0; i < recArray[r].length; i++) { - lastRow.append(``); - - - } - } + + if (searchValue) { + + let filter = recArray.filter((item) => { + for (let i = 0; i < item.length; i++) { + if (item[i].toLowerCase().includes(searchValue.toLowerCase())) { + return true; + + } + + } + return false; + }); + const tbody = $("tbody"); + tbody.empty(); + + for (let r = 0; r < 35 && r < filter.length; r++) { + const row = $(''); + for (let i = 0; i < filter[r].length; i++) { + row.append(``); + } + tbody.append(row); + } + + } else for (let r = 0; r < 35; r++) { + $("tbody").append(``); + const lastRow = $(".row:last"); + for (let i = 0; i < recArray[r].length; i++) { + lastRow.append(``); + + + } + } + + }) .catch((error) => { console.error("Error fetching records:", error); @@ -203,54 +249,54 @@ async function leftArrow(): Promise { -$('#searchInput').on('keyup',async () => { +$('#searchInput').on('keyup', async () => { const inputValue = $('#searchInput').val() as string; - console.log(inputValue); - - if(!inputValue.length){ + + + if (!inputValue.length) { displayRecords() } const recArray: any[] = await fetchRecords(); - - let filter = recArray.filter((item)=>{ + + let filter = recArray.filter((item) => { for (let i = 0; i < item.length; i++) { if (item[i].toLowerCase().includes(inputValue.toLowerCase())) { - return true; - + return true; + } - + } - return false; - }); - const tbody = $("tbody"); - tbody.empty(); - - for (let r = 0; r < 35 && r < filter.length; r++) { - const row = $(''); - for (let i = 0; i < filter[r].length; i++) { - row.append(``); - } - tbody.append(row); + return false; + }); + const tbody = $("tbody"); + tbody.empty(); + + for (let r = 0; r < 35 && r < filter.length; r++) { + const row = $(''); + for (let i = 0; i < filter[r].length; i++) { + row.append(``); } - - }) + tbody.append(row); + } + +}) + + + + + + + + + + + + + - - - - - - - - - - - - - diff --git a/index.html b/index.html index 9fd1b027..a90e7522 100644 --- a/index.html +++ b/index.html @@ -11,7 +11,8 @@

OnBoard-Javascript

- + +
From 038f93e58b5c591c43dcea937653a1bf7aa6ceed Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Tue, 15 Aug 2023 15:38:23 +0200 Subject: [PATCH 03/33] update --- app.ts | 246 ++++++++++++++++++++++------------------------------- index.html | 23 +++-- style.css | 84 +++++++++++++++--- 3 files changed, 192 insertions(+), 161 deletions(-) diff --git a/app.ts b/app.ts index 0b53c873..d6d17fcb 100644 --- a/app.ts +++ b/app.ts @@ -2,20 +2,15 @@ async function fetchRecordCount(): Promise { try { const recordCount = await fetch(`http://192.168.4.133:2050/recordCount`); - if (!recordCount.ok) { throw new Error('Failed to fetch record count'); } const data = await recordCount.json() return data; - } catch (error) { console.error('Error fetching the record count:', error); throw error; } - - - } @@ -24,59 +19,59 @@ async function fetchRecordCount(): Promise { function fetchColumns(): void { fetch("http://192.168.4.133:2050/columns") .then((response: Response) => { - return response.json() as Promise; }) .then((columns: string[]) => { const colArray = columns; console.log(colArray); - for (let c = 0; c < colArray.length; c++) { $(".head").append(``); } }) - } - fetchColumns(); async function fetchRecords(): Promise { try { let count: number = await fetchRecordCount() - 1; - const response = await fetch(`http://192.168.4.133:2050/records?from=0&to=35`); - + const response = await fetch(`http://192.168.4.133:2050/records?from=0&to=${count}`); if (!response.ok) { throw new Error("Sorry, there's a problem in the network"); } const records = await response.json(); return records; - - } catch (error) { console.error("Error fetching records:", error); throw error; } } - - - - - fetchRecords() async function displayRecords(): Promise { try { - const recArray: any[][] = await fetchRecords(); - - for (let r = 0; r < 35; r++) { - $("tbody").append(``); - const lastRow = $(".row:last"); - for (let i = 0; i < recArray[r].length; i++) { - lastRow.append(``); - - - } - } + $('#loader').show() + fetch(`http://192.168.4.133:2050/records?from=0&to=19`) + .then((response: Response) => { + + if (!response.ok) { + throw new Error("Sorry, there's a problem in the network"); + } + return response.json() as Promise; + }) + .then((records: any[]) => { + const recArray = records; + $('#loader').hide() + + for (let r = 0; r < 20; r++) { + $("tbody").append(``); + const lastRow = $(".row:last"); + for (let i = 0; i < recArray[r].length; i++) { + lastRow.append(``); + } + } + $('#page').empty() + $('#page').append(`Showing record: 1 - 19`) + }) } catch (error) { console.error("Error displaying records:", error); } @@ -85,31 +80,73 @@ displayRecords() -async function rightArrow(): Promise { - let searchValue = $('#searchInput').val() as string - - let count: number = await fetchRecordCount() - 1; +$('.btnSearch').on('click', async (event: any) => { + event.preventDefault() + console.log('hello') + const inputValue = $('#searchInput').val() as number; + if (isNaN(inputValue)) { + alert(`${inputValue} is not a number`); + $('#searchInput').val('') + + } else { + + const firstNumber = Math.floor(inputValue / 10) * 10; + const lastNumber = firstNumber + 19 + const tbody = $("tbody"); + tbody.empty(); + $('#page').empty(); + try { + $('#loader').show() + fetch(`http://192.168.4.133:2050/records?from=${firstNumber}&to=${lastNumber}`) + .then((response: Response) => { + + if (!response.ok) { + throw new Error("Sorry, there's a problem in the network"); + } + return response.json() as Promise; + }) + .then((records: any[]) => { + const recArray = records; + $('#loader').hide() + + for (let r = 0; r < 20; r++) { + $("tbody").append(``); + const lastRow = $(".row:last"); + for (let i = 0; i < recArray[r].length; i++) { + lastRow.append(``); + } + } + $('#page').empty() + $('#page').append(`Showing record: ${firstNumber} - ${lastNumber}`) + }) + } catch (error) { + console.error("Error displaying records:", error); + } + } +}) + - const firstRow = document.querySelector("#recordsTable tbody"); +async function rightArrow(): Promise { + const firstRow = document.querySelector("#recordsTable tbody"); if (firstRow) { const cells = firstRow.querySelectorAll("td"); const firstRecord: string[] = []; cells.forEach((cell) => { firstRecord.push(cell.textContent || ""); }); - const firstID = parseFloat(firstRecord[0]); - - - if (0 <= firstID && firstID <= (count - 35)) { + if (0 <= firstID) { let clearTable = document.querySelector('tbody') as HTMLTableSectionElement | null; if (clearTable) { clearTable.innerHTML = ""; - - let firstRecord = + firstID + 35 - let lastRecord = + firstRecord + 34 + let firstRecord = + firstID + 20 + let lastRecord = + firstRecord + 19 + $('#page').empty() + $('#page').append(`Showing record: ${firstRecord} - ${lastRecord}`) + + $('#loader').show() fetch(`http://192.168.4.133:2050/records?from=${firstRecord}&to=${lastRecord}`) .then((response: Response) => { @@ -120,38 +157,17 @@ async function rightArrow(): Promise { }) .then((records: any[]) => { const recArray = records; - if (searchValue) { - - let filter = recArray.filter((item) => { - for (let i = 0; i < item.length; i++) { - if (item[i].toLowerCase().includes(searchValue.toLowerCase())) { - return true; - - } - - } - return false; - }); - const tbody = $("tbody"); - tbody.empty(); - - for (let r = 0; r < 35 && r < filter.length; r++) { - const row = $(''); - for (let i = 0; i < filter[r].length; i++) { - row.append(``); - } - tbody.append(row); - } + $('#loader').hide() - } else for (let r = 0; r < 35; r++) { - $("tbody").append(``); - const lastRow = $(".row:last"); - for (let i = 0; i < recArray[r].length; i++) { - lastRow.append(``); + for (let r = 0; r < 20; r++) { + $("tbody").append(``); + const lastRow = $(".row:last"); + for (let i = 0; i < recArray[r].length; i++) { + lastRow.append(``); - } } + } }) .catch((error) => { console.error("Error fetching records:", error); @@ -165,32 +181,27 @@ async function rightArrow(): Promise { async function leftArrow(): Promise { let searchValue = $('#searchInput').val() as string let count: number = await fetchRecordCount() - 1; - + const firstRow = document.querySelector("#recordsTable tbody"); - + if (firstRow) { const cells = firstRow.querySelectorAll("td"); const firstRecord: string[] = []; cells.forEach((cell) => { firstRecord.push(cell.textContent || ""); }); - - - const firstID = parseFloat(firstRecord[0]); - - - if (35 <= firstID && firstID <= (count)) { + if (20 <= firstID && firstID <= (count)) { let clearTable = document.querySelector('tbody') as HTMLTableSectionElement | null; if (clearTable) { clearTable.innerHTML = ""; - let firstRecord = firstID - 35 - - let lastRecord = firstRecord + 34 - - + let firstRecord = firstID - 20 + let lastRecord = firstRecord + 19 + $('#page').empty() + $('#page').append(`Showing record: ${firstRecord} - ${lastRecord}`) + $('#loader').show() fetch(`http://192.168.4.133:2050/records?from=${firstRecord}&to=${lastRecord}`) .then((response: Response) => { @@ -201,41 +212,20 @@ async function leftArrow(): Promise { }) .then((records: any[]) => { const recArray = records; - - if (searchValue) { - - let filter = recArray.filter((item) => { - for (let i = 0; i < item.length; i++) { - if (item[i].toLowerCase().includes(searchValue.toLowerCase())) { - return true; - - } - - } - return false; - }); - const tbody = $("tbody"); - tbody.empty(); - - for (let r = 0; r < 35 && r < filter.length; r++) { - const row = $(''); - for (let i = 0; i < filter[r].length; i++) { - row.append(``); - } - tbody.append(row); - } + $('#loader').hide() - } else for (let r = 0; r < 35; r++) { - $("tbody").append(``); - const lastRow = $(".row:last"); - for (let i = 0; i < recArray[r].length; i++) { - lastRow.append(``); + + for (let r = 0; r < 20; r++) { + $("tbody").append(``); + const lastRow = $(".row:last"); + for (let i = 0; i < recArray[r].length; i++) { + lastRow.append(``); - } } + } + - }) .catch((error) => { console.error("Error fetching records:", error); @@ -249,39 +239,7 @@ async function leftArrow(): Promise { -$('#searchInput').on('keyup', async () => { - const inputValue = $('#searchInput').val() as string; - - - - if (!inputValue.length) { - displayRecords() - } - const recArray: any[] = await fetchRecords(); - - - let filter = recArray.filter((item) => { - for (let i = 0; i < item.length; i++) { - if (item[i].toLowerCase().includes(inputValue.toLowerCase())) { - return true; - - } - - } - return false; - }); - const tbody = $("tbody"); - tbody.empty(); - - for (let r = 0; r < 35 && r < filter.length; r++) { - const row = $(''); - for (let i = 0; i < filter[r].length; i++) { - row.append(``); - } - tbody.append(row); - } -}) diff --git a/index.html b/index.html index a90e7522..676864c3 100644 --- a/index.html +++ b/index.html @@ -7,16 +7,27 @@ - +

OnBoard-Javascript

+ +
- +
+ + +
+ +
+ +
+ +
${recArray[r][i]}
${recArray[r][i]}
${recArray[r][i]}
${filter[r][i]}
${recArray[r][i]}
${recArray[r][i]}
${filter[r][i]}
${recArray[r][i]}
${filter[r][i]}
${filter[r][i]} ${colArray[c]}
${recArray[r][i]}
${recArray[r][i]}
${recArray[r][i]}
${filter[r][i]}
${recArray[r][i]}
${recArray[r][i]}
${filter[r][i]}
${recArray[r][i]}
${recArray[r][i]}
${filter[r][i]}
@@ -26,12 +37,10 @@

OnBoard-Javascript

- -
- -
1 - 35 Records per Page
- +
+ + diff --git a/style.css b/style.css index 47760400..18d75114 100644 --- a/style.css +++ b/style.css @@ -1,19 +1,47 @@ body{ overflow: hidden; background-color: #f7e7ce; + padding: 0; + margin: 0; +} +.tableRecords{ + + height: 90vh; + position: absolute; + display: flex; + + +} + + +.header{ + display: block; } table{ width: 100%; table-layout: fixed; - + flex-shrink: 0.2; + + + + + } table, th, td { border: 1px solid #4B3832; border-collapse: collapse; text-align: center; color: black; + +} +.tableRecords thead .head { + position: sticky; + background-color: #97694F; + border: 2px solid black; + top:0; } + td:hover{ color: #97694F; } @@ -23,14 +51,16 @@ td:hover{ text-decoration: underline; color: #4B3832; + } .showRecords{ - transform: translate(49rem, -0.5rem); + /* transform: translate(49rem, -0.5rem); */ position: absolute; - bottom: 0px; padding: 1rem; + right: 0; + top: 1.2%; } @@ -51,10 +81,10 @@ td:hover{ .arrow-right, .arrow-left{ display: inline-block; - width: 30px; - height: 30px; - border-top: 10px solid #4B3832; - border-left: 10px solid #4B3832; + width: 25px; + height: 25px; + border-top: 5px solid #4B3832; + border-left: 5px solid #4B3832; @@ -78,15 +108,49 @@ td:hover{ } .search-container { - float: right; + float: left; + position: absolute; + top: 2.15%; + + } .search-container input[type=text] { padding: 6px; - margin-bottom: 3px; - font-size: 17px; + font-size: 1rem; border: 2px solid #4B3832; background-color: #f7e7ce; } +.btnSearch{ + font-size: 1rem; + border: 2px solid #4B3832; + padding: 6px; + background-color:#97694F ; + color: #f7e7ce; +} +#loader{ + position: absolute; + left: 50%; + margin-left: -50px; + top: 50%; + margin-top: -50px; + border: calc(200px / 10) solid #97694F; + border-top: 20px solid #4B3832; + border-radius: 100%; + display:none; + width: 100px; + height: 100px; + animation: spin 2s linear infinite; +} + +@keyframes spin { + 0% { + transform: rotate(0deg); + } + 100% { + transform: rotate(360deg); + } +} + From 30785ac40852016eb4a138fac6829b82ed768178 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Fri, 18 Aug 2023 14:01:07 +0200 Subject: [PATCH 04/33] update --- app.ts | 327 ++++++++++++++++++++++++++++-------------------------- style.css | 17 ++- 2 files changed, 184 insertions(+), 160 deletions(-) diff --git a/app.ts b/app.ts index d6d17fcb..ce257c18 100644 --- a/app.ts +++ b/app.ts @@ -1,7 +1,13 @@ +var firstNumber: number = 0; +console.log +var lastNumber: number; +console.log + + async function fetchRecordCount(): Promise { try { - const recordCount = await fetch(`http://192.168.4.133:2050/recordCount`); + const recordCount = await fetch(`http://localhost:2050/recordCount`); if (!recordCount.ok) { throw new Error('Failed to fetch record count'); } @@ -17,13 +23,13 @@ async function fetchRecordCount(): Promise { function fetchColumns(): void { - fetch("http://192.168.4.133:2050/columns") + fetch("http://localhost:2050/columns") .then((response: Response) => { return response.json() as Promise; }) .then((columns: string[]) => { const colArray = columns; - console.log(colArray); + for (let c = 0; c < colArray.length; c++) { $(".head").append(`${colArray[c]}`); } @@ -31,47 +37,96 @@ function fetchColumns(): void { } fetchColumns(); -async function fetchRecords(): Promise { - try { - let count: number = await fetchRecordCount() - 1; - const response = await fetch(`http://192.168.4.133:2050/records?from=0&to=${count}`); - if (!response.ok) { - throw new Error("Sorry, there's a problem in the network"); - } - const records = await response.json(); - return records; - } catch (error) { - console.error("Error fetching records:", error); - throw error; + + +async function adjustRowsByScreenHeight(): Promise { + $('#loader').show() + const tableBody = document.querySelector('.tableRecords tbody') as HTMLElement; + const rowHeight = 30 + const maxRows = Math.floor(tableBody.clientHeight / rowHeight); + + return maxRows; + +} + +async function fetchRecords(from: number, to: number): Promise { + + const response = await fetch(`http://localhost:2050/records?from=${from}&to=${to}`); + if (!response.ok) { + throw new Error("Sorry, there's a problem with the network"); } + + return response.json(); + } -fetchRecords() + + +$(window).on('resize', async (event: any) => { + event.preventDefault() + $('#page').empty(); + // const count: number = await fetchRecordCount() - 1; + // const calculatedRows = await adjustRowsByScreenHeight(); + + // console.log(firstNumber) + // console.log(lastNumber) + // if (firstNumber < 0) { + // firstNumber = 0; + // } + + // if (lastNumber > count) { + // lastNumber = count; + // } else { + // lastNumber = firstNumber + (calculatedRows-1); + // } + + await displayRecords(); + +}) async function displayRecords(): Promise { try { $('#loader').show() - fetch(`http://192.168.4.133:2050/records?from=0&to=19`) - .then((response: Response) => { + const calculatedRows = await adjustRowsByScreenHeight(); + const inputValue = $('#searchInput').val() as number; + - if (!response.ok) { - throw new Error("Sorry, there's a problem in the network"); - } - return response.json() as Promise; - }) - .then((records: any[]) => { - const recArray = records; - $('#loader').hide() - - for (let r = 0; r < 20; r++) { - $("tbody").append(``); - const lastRow = $(".row:last"); - for (let i = 0; i < recArray[r].length; i++) { - lastRow.append(`${recArray[r][i]}`); - } + if (!lastNumber) { + lastNumber = firstNumber + (calculatedRows - 1); + } + if (firstNumber < 0) { + firstNumber = 0; + } + if (lastNumber >= 999999) { + lastNumber = 999999; + }else{ + lastNumber = firstNumber + (calculatedRows - 1); + } + + const records = await fetchRecords(firstNumber, lastNumber); + const tbody = $("tbody"); + tbody.empty(); + for (let r = 0; r < records.length; r++) { + $("tbody").append(``); + const lastRow = $(".row:last"); + for (let i = 0; i < records[r].length; i++) { + lastRow.append(`${records[r][i]}`); + + + + } + if(records[r].includes(inputValue)){ + console.log(records[r]); + lastRow.css('background-color', '#DDC0B4'); + } - $('#page').empty() - $('#page').append(`Showing record: 1 - 19`) - }) + tbody.append(lastRow); + + } + + $('#page').empty(); + $('#page').append(`Showing record: ${firstNumber} - ${lastNumber}`); + $('#loader').hide() + } catch (error) { console.error("Error displaying records:", error); } @@ -79,110 +134,81 @@ async function displayRecords(): Promise { displayRecords() +async function updateRecordsAndResize(inputValue: number) { + const count: number = await fetchRecordCount() - 1; + + if (isNaN(inputValue) || inputValue > count) { + alert(`${inputValue} is not a number within the range`); + $('#searchInput').val(''); + return; + } + + // Calculate a range of numbers around the inputValue + const range = await adjustRowsByScreenHeight(); // Adjust this value to control the range size + firstNumber = Math.max(0, inputValue - Math.floor(range / inputValue)); + console.log(firstNumber); + + lastNumber = Math.min(count, firstNumber + range - 1); + console.log(lastNumber); + + + + await adjustRowsByScreenHeight(); + + await displayRecords(); +} + $('.btnSearch').on('click', async (event: any) => { - event.preventDefault() - console.log('hello') + event.preventDefault(); const inputValue = $('#searchInput').val() as number; - if (isNaN(inputValue)) { - alert(`${inputValue} is not a number`); - $('#searchInput').val('') + console.log(inputValue) - } else { - - const firstNumber = Math.floor(inputValue / 10) * 10; - const lastNumber = firstNumber + 19 - const tbody = $("tbody"); - tbody.empty(); - $('#page').empty(); - try { - $('#loader').show() - fetch(`http://192.168.4.133:2050/records?from=${firstNumber}&to=${lastNumber}`) - .then((response: Response) => { - - if (!response.ok) { - throw new Error("Sorry, there's a problem in the network"); - } - return response.json() as Promise; - }) - .then((records: any[]) => { - const recArray = records; - $('#loader').hide() - - for (let r = 0; r < 20; r++) { - $("tbody").append(``); - const lastRow = $(".row:last"); - for (let i = 0; i < recArray[r].length; i++) { - lastRow.append(`${recArray[r][i]}`); - } - } - $('#page').empty() - $('#page').append(`Showing record: ${firstNumber} - ${lastNumber}`) - }) - } catch (error) { - console.error("Error displaying records:", error); - } - } -}) + await updateRecordsAndResize(inputValue); +}); async function rightArrow(): Promise { - const firstRow = document.querySelector("#recordsTable tbody"); - if (firstRow) { - const cells = firstRow.querySelectorAll("td"); - const firstRecord: string[] = []; + const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); + let count: number = await fetchRecordCount() - 1; + + + if (lastRow) { + const cells = lastRow.querySelectorAll("td"); + const lastRecord: string[] = []; cells.forEach((cell) => { - firstRecord.push(cell.textContent || ""); + lastRecord.push(cell.textContent || ""); }); - const firstID = parseFloat(firstRecord[0]); - if (0 <= firstID) { - let clearTable = document.querySelector('tbody') as HTMLTableSectionElement | null; - if (clearTable) { - clearTable.innerHTML = ""; - - let firstRecord = + firstID + 20 - let lastRecord = + firstRecord + 19 - $('#page').empty() - $('#page').append(`Showing record: ${firstRecord} - ${lastRecord}`) - - $('#loader').show() - fetch(`http://192.168.4.133:2050/records?from=${firstRecord}&to=${lastRecord}`) - .then((response: Response) => { - - if (!response.ok) { - throw new Error("Sorry, there's a problem in the network"); - } - return response.json() as Promise; - }) - .then((records: any[]) => { - const recArray = records; - $('#loader').hide() - - for (let r = 0; r < 20; r++) { - $("tbody").append(``); - const lastRow = $(".row:last"); - for (let i = 0; i < recArray[r].length; i++) { - lastRow.append(`${recArray[r][i]}`); - - - } - } - }) - .catch((error) => { - console.error("Error fetching records:", error); - }); + const lastID = parseFloat(lastRecord[0]); + console.log(lastID); + + if (0 <= lastID && lastID <= (count)) { + const tbody = $("tbody"); + tbody.empty(); + + firstNumber = lastID + 1; + + const calculatedRows = await adjustRowsByScreenHeight(); + + lastNumber = firstNumber + (calculatedRows - 1) + + await adjustRowsByScreenHeight(); + await displayRecords(); - } } + } } + + async function leftArrow(): Promise { - let searchValue = $('#searchInput').val() as string + let count: number = await fetchRecordCount() - 1; - const firstRow = document.querySelector("#recordsTable tbody"); + + const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); if (firstRow) { const cells = firstRow.querySelectorAll("td"); @@ -191,49 +217,28 @@ async function leftArrow(): Promise { firstRecord.push(cell.textContent || ""); }); const firstID = parseFloat(firstRecord[0]); - if (20 <= firstID && firstID <= (count)) { - let clearTable = document.querySelector('tbody') as HTMLTableSectionElement | null; - if (clearTable) { - clearTable.innerHTML = ""; + console.log(firstID) - let firstRecord = firstID - 20 - let lastRecord = firstRecord + 19 - $('#page').empty() - $('#page').append(`Showing record: ${firstRecord} - ${lastRecord}`) - $('#loader').show() - fetch(`http://192.168.4.133:2050/records?from=${firstRecord}&to=${lastRecord}`) - .then((response: Response) => { + const tbody = $("tbody"); + tbody.empty(); - if (!response.ok) { - throw new Error("Sorry, there's a problem in the network"); - } - return response.json() as Promise; - }) - .then((records: any[]) => { - const recArray = records; - $('#loader').hide() + const calculatedRows = await adjustRowsByScreenHeight(); + console.log(calculatedRows); + firstNumber = firstID - (calculatedRows + 1) + console.log(firstRecord); - for (let r = 0; r < 20; r++) { - $("tbody").append(``); - const lastRow = $(".row:last"); - for (let i = 0; i < recArray[r].length; i++) { - lastRow.append(`${recArray[r][i]}`); + lastNumber = firstNumber + (calculatedRows) + console.log(lastNumber); - } - } + await adjustRowsByScreenHeight(); + await displayRecords(); + } - }) - .catch((error) => { - console.error("Error fetching records:", error); - }); - } - } - } } @@ -251,6 +256,18 @@ async function leftArrow(): Promise { + + + + + + + + + + + + diff --git a/style.css b/style.css index 18d75114..45ffc769 100644 --- a/style.css +++ b/style.css @@ -5,12 +5,16 @@ body{ margin: 0; } .tableRecords{ - - height: 90vh; + position: absolute; display: flex; - - + +} +.tableRecords tbody{ + height: 85vh; +} +#recordsTable tbody .row td { + height: 1rem; } @@ -20,7 +24,8 @@ body{ table{ width: 100%; table-layout: fixed; - flex-shrink: 0.2; + + margin-bottom: 5%; @@ -44,6 +49,7 @@ table, th, td { td:hover{ color: #97694F; + background-color:#DDC0B4; } .heading { @@ -54,6 +60,7 @@ td:hover{ } + .showRecords{ /* transform: translate(49rem, -0.5rem); */ From 96682be3d35b264ff881ef059d614908d4554383 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Wed, 23 Aug 2023 11:39:47 +0200 Subject: [PATCH 05/33] update --- app.ts | 176 +++++++++++++++++++---------------------------------- index.html | 9 ++- style.css | 65 ++++++++++++-------- 3 files changed, 109 insertions(+), 141 deletions(-) diff --git a/app.ts b/app.ts index ce257c18..f3177c27 100644 --- a/app.ts +++ b/app.ts @@ -1,9 +1,8 @@ +import { ajax, css } from "jquery"; var firstNumber: number = 0; -console.log var lastNumber: number; -console.log - - +var inputValue: number | null = null; +var calculatedRows: number | null = null; async function fetchRecordCount(): Promise { try { @@ -19,9 +18,6 @@ async function fetchRecordCount(): Promise { } } - - - function fetchColumns(): void { fetch("http://localhost:2050/columns") .then((response: Response) => { @@ -35,20 +31,33 @@ function fetchColumns(): void { } }) } -fetchColumns(); - - +fetchColumns() async function adjustRowsByScreenHeight(): Promise { - $('#loader').show() - const tableBody = document.querySelector('.tableRecords tbody') as HTMLElement; - const rowHeight = 30 - const maxRows = Math.floor(tableBody.clientHeight / rowHeight); - + + let tableBody = (document.querySelector('.tableRecords tbody') as HTMLElement)?.clientHeight; + let rowHeight = 50; + let maxRows = Math.floor(tableBody / rowHeight); return maxRows; } +let resizeTimeout: number; +$(window).on('resize', async () => { + clearTimeout(resizeTimeout); + resizeTimeout = setTimeout(async () => { + $('#loader').show() + calculatedRows = await adjustRowsByScreenHeight(); + + if (inputValue !== null) { + await updateRecordsAndResize(inputValue); + } + await displayRecords() + $('#loader').hide(); + + }, 500); + }) + async function fetchRecords(from: number, to: number): Promise { const response = await fetch(`http://localhost:2050/records?from=${from}&to=${to}`); @@ -57,51 +66,30 @@ async function fetchRecords(from: number, to: number): Promise { } return response.json(); - } - -$(window).on('resize', async (event: any) => { - event.preventDefault() - $('#page').empty(); - // const count: number = await fetchRecordCount() - 1; - // const calculatedRows = await adjustRowsByScreenHeight(); - - // console.log(firstNumber) - // console.log(lastNumber) - // if (firstNumber < 0) { - // firstNumber = 0; - // } - - // if (lastNumber > count) { - // lastNumber = count; - // } else { - // lastNumber = firstNumber + (calculatedRows-1); - // } - - await displayRecords(); - -}) - async function displayRecords(): Promise { try { $('#loader').show() - const calculatedRows = await adjustRowsByScreenHeight(); - const inputValue = $('#searchInput').val() as number; + calculatedRows = await adjustRowsByScreenHeight(); - + + const inputValue = $('#searchInput').val() as number; if (!lastNumber) { lastNumber = firstNumber + (calculatedRows - 1); } - if (firstNumber < 0) { + if (firstNumber < 0 ) { firstNumber = 0; } + if (lastNumber >= 999999) { lastNumber = 999999; + firstNumber = lastNumber - (calculatedRows-1) + }else{ lastNumber = firstNumber + (calculatedRows - 1); - } - + } + console.log(firstNumber,lastNumber) const records = await fetchRecords(firstNumber, lastNumber); const tbody = $("tbody"); tbody.empty(); @@ -110,70 +98,52 @@ async function displayRecords(): Promise { const lastRow = $(".row:last"); for (let i = 0; i < records[r].length; i++) { lastRow.append(`${records[r][i]}`); - - - } if(records[r].includes(inputValue)){ - console.log(records[r]); - lastRow.css('background-color', '#DDC0B4'); - - } + + lastRow.css('background-color', '#DDC0B4'); + + } tbody.append(lastRow); - - } - $('#page').empty(); $('#page').append(`Showing record: ${firstNumber} - ${lastNumber}`); $('#loader').hide() - + } } catch (error) { console.error("Error displaying records:", error); } } displayRecords() - async function updateRecordsAndResize(inputValue: number) { const count: number = await fetchRecordCount() - 1; - - if (isNaN(inputValue) || inputValue > count) { - alert(`${inputValue} is not a number within the range`); + if (inputValue < 0 || inputValue > count) { + $('.modal').css('display','block') + $('.modal-content').append(`

${inputValue} is not a number within the range.Please try a different number

`) $('#searchInput').val(''); return; } - - // Calculate a range of numbers around the inputValue - const range = await adjustRowsByScreenHeight(); // Adjust this value to control the range size - firstNumber = Math.max(0, inputValue - Math.floor(range / inputValue)); - console.log(firstNumber); - - lastNumber = Math.min(count, firstNumber + range - 1); - console.log(lastNumber); - - - - await adjustRowsByScreenHeight(); - + calculatedRows = await adjustRowsByScreenHeight(); + const quarterRange = Math.floor(calculatedRows / 2); + firstNumber = Math.max(0, inputValue - quarterRange); + lastNumber = Math.min(count, firstNumber + (calculatedRows - 1)); await displayRecords(); } +$('#closeModalBtn').on("click", () => { + $('.modal').css('display','none') +}); $('.btnSearch').on('click', async (event: any) => { event.preventDefault(); - const inputValue = $('#searchInput').val() as number; - console.log(inputValue) - - + inputValue = $('#searchInput').val() as number; await updateRecordsAndResize(inputValue); }); - async function rightArrow(): Promise { + $('#searchInput').val(''); const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); let count: number = await fetchRecordCount() - 1; - - if (lastRow) { const cells = lastRow.querySelectorAll("td"); const lastRecord: string[] = []; @@ -181,35 +151,26 @@ async function rightArrow(): Promise { lastRecord.push(cell.textContent || ""); }); const lastID = parseFloat(lastRecord[0]); - console.log(lastID); - if (0 <= lastID && lastID <= (count)) { + const tbody = $("tbody"); tbody.empty(); - firstNumber = lastID + 1; - - const calculatedRows = await adjustRowsByScreenHeight(); - + calculatedRows = await adjustRowsByScreenHeight(); lastNumber = firstNumber + (calculatedRows - 1) - - await adjustRowsByScreenHeight(); await displayRecords(); - + }else{ + $('.arrow-right').css('display','none') } - + + } } - - async function leftArrow(): Promise { - + $('#searchInput').val(''); let count: number = await fetchRecordCount() - 1; - - const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); - if (firstRow) { const cells = firstRow.querySelectorAll("td"); const firstRecord: string[] = []; @@ -217,29 +178,18 @@ async function leftArrow(): Promise { firstRecord.push(cell.textContent || ""); }); const firstID = parseFloat(firstRecord[0]); - console.log(firstID) - - const tbody = $("tbody"); tbody.empty(); - const calculatedRows = await adjustRowsByScreenHeight(); - console.log(calculatedRows); - - firstNumber = firstID - (calculatedRows + 1) - console.log(firstRecord); - - - lastNumber = firstNumber + (calculatedRows) - console.log(lastNumber); - - await adjustRowsByScreenHeight(); - + lastNumber = firstID - 1; + firstNumber = lastNumber - (calculatedRows-1) await displayRecords(); - } + +} +} + -} diff --git a/index.html b/index.html index 676864c3..94de68b1 100644 --- a/index.html +++ b/index.html @@ -9,11 +9,16 @@

OnBoard-Javascript

- +
- +
diff --git a/style.css b/style.css index 45ffc769..a43c7451 100644 --- a/style.css +++ b/style.css @@ -8,13 +8,15 @@ body{ position: absolute; display: flex; - -} -.tableRecords tbody{ - height: 85vh; + height: 95vh; } -#recordsTable tbody .row td { - height: 1rem; +/* .tableRecords tbody{ + height: 80%; + flex-shrink: 1; + +} */ +tbody .row td{ + height: 1.5rem; } @@ -24,12 +26,7 @@ body{ table{ width: 100%; table-layout: fixed; - - margin-bottom: 5%; - - - - + margin-bottom: 5% ; } table, th, td { @@ -68,21 +65,15 @@ td:hover{ padding: 1rem; right: 0; top: 1.2%; - - } .showRecords #page{ display: inline-block; font-size: 1.2rem; - } - - .arrow-right, .arrow-left{ border: none; background-color: transparent; - } .arrow-right, @@ -92,9 +83,6 @@ td:hover{ height: 25px; border-top: 5px solid #4B3832; border-left: 5px solid #4B3832; - - - } .arrow-right, @@ -105,8 +93,6 @@ td:hover{ .arrow-right{ transform: rotate(135deg); float: right; - - } .arrow-left{ @@ -118,10 +104,8 @@ td:hover{ float: left; position: absolute; top: 2.15%; - - } -.search-container input[type=text] { +.search-container input[type=number] { padding: 6px; font-size: 1rem; border: 2px solid #4B3832; @@ -148,6 +132,8 @@ td:hover{ width: 100px; height: 100px; animation: spin 2s linear infinite; + background-color: #f7e7ce; + } @keyframes spin { @@ -158,6 +144,33 @@ td:hover{ transform: rotate(360deg); } } +.modal { + display: none; + position: fixed; + transform:translateX(50%); + z-index: 1; + top: 0; + width: 50%; + background-color: #f7e7ce; + border: 1px solid #4B3832; +} + +.modal-content { + background-color: #4B3832; + margin: 1rem; + padding: 2rem; + border: 1px solid #DDC0B4; + color: #f7e7ce; + font-size: 1.5rem; +} + +.close { + float: right; + font-size: rem; + font-weight: bold; + cursor: pointer; +} + From e28719102e87606f56b64ab4282aca8fef36c063 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Fri, 25 Aug 2023 13:20:38 +0200 Subject: [PATCH 06/33] update --- app.ts | 192 ++++++++++++++++++++++++++++------------------------- index.html | 10 +-- style.css | 39 ++++------- 3 files changed, 119 insertions(+), 122 deletions(-) diff --git a/app.ts b/app.ts index f3177c27..8a06275b 100644 --- a/app.ts +++ b/app.ts @@ -1,10 +1,10 @@ import { ajax, css } from "jquery"; var firstNumber: number = 0; var lastNumber: number; -var inputValue: number | null = null; var calculatedRows: number | null = null; +var count: number | null = null; -async function fetchRecordCount(): Promise { +async function fetchRecordCount(): Promise { // fetches the number of records from the server try { const recordCount = await fetch(`http://localhost:2050/recordCount`); if (!recordCount.ok) { @@ -18,96 +18,110 @@ async function fetchRecordCount(): Promise { } } -function fetchColumns(): void { +function fetchColumns(): void { //fetches the different columns headings from server fetch("http://localhost:2050/columns") .then((response: Response) => { return response.json() as Promise; }) .then((columns: string[]) => { - const colArray = columns; + const colArray = columns; // assigns the columns to an array for (let c = 0; c < colArray.length; c++) { - $(".head").append(`${colArray[c]}`); + $(".head").append(`${colArray[c]}`); // creates a single column for each heading } }) } fetchColumns() -async function adjustRowsByScreenHeight(): Promise { - - let tableBody = (document.querySelector('.tableRecords tbody') as HTMLElement)?.clientHeight; - let rowHeight = 50; - let maxRows = Math.floor(tableBody / rowHeight); - return maxRows; - +async function adjustRowsByScreenHeight(): Promise { // calculates the number of rows that can fit the screen + const screenHeight = window.innerHeight; // calculates screen height + const availableHeight = screenHeight - 105 // subtracts the space used from the screeen + let rowHeight = 35 + if(availableHeight <= 0){ + return 0; + }else{ + let maxRows = Math.floor(availableHeight / rowHeight) ; + return maxRows; // returns the maximum rows that can fit into screen + } } let resizeTimeout: number; $(window).on('resize', async () => { - clearTimeout(resizeTimeout); + clearTimeout(resizeTimeout); // cancel previously schedule Timeout + resizeTimeout = setTimeout(async () => { - $('#loader').show() - calculatedRows = await adjustRowsByScreenHeight(); - - if (inputValue !== null) { - await updateRecordsAndResize(inputValue); + $('#loader').show() + calculatedRows = await adjustRowsByScreenHeight(); // to recalcualte the number of max rows + await displayRecords() + + let inputValue = $('#searchInput').val() ; // priotizes the search input value if available + if (inputValue !== '') { + await updateRecordsAndResize(Number(inputValue)) } - await displayRecords() - $('#loader').hide(); - + $('#loader').hide(); }, 500); - }) - -async function fetchRecords(from: number, to: number): Promise { +}) +async function fetchRecords(from: number, to: number): Promise { // fetches records from specified number to a certain number const response = await fetch(`http://localhost:2050/records?from=${from}&to=${to}`); if (!response.ok) { throw new Error("Sorry, there's a problem with the network"); } - return response.json(); } -async function displayRecords(): Promise { +async function displayRecords(): Promise { //displays records from firstNumber to lastNumber try { $('#loader').show() + count = await fetchRecordCount() - 1; calculatedRows = await adjustRowsByScreenHeight(); - - const inputValue = $('#searchInput').val() as number; + if(calculatedRows === 0){ + lastNumber = firstNumber + + } if (!lastNumber) { lastNumber = firstNumber + (calculatedRows - 1); } - if (firstNumber < 0 ) { + if (firstNumber < 0) { firstNumber = 0; + lastNumber = firstNumber + (calculatedRows - 1); } - - if (lastNumber >= 999999) { - lastNumber = 999999; - firstNumber = lastNumber - (calculatedRows-1) - - }else{ + else { lastNumber = firstNumber + (calculatedRows - 1); - } - console.log(firstNumber,lastNumber) - const records = await fetchRecords(firstNumber, lastNumber); + } + + let records; + if(lastNumber <= count && lastNumber >= 0){ + records = await fetchRecords(firstNumber, lastNumber); + $('#page').empty(); + $('#page').append(`Showing record: ${firstNumber} - ${lastNumber}`); // changes the record range showing + $('#loader').hide() + }else{ + firstNumber = count - (calculatedRows-1) + lastNumber = count; + records = await fetchRecords(firstNumber, count); + $('#page').empty(); + $('#page').append(`Showing record: ${firstNumber} - ${count}`); + $('#loader').hide() + + } + + const tbody = $("tbody"); tbody.empty(); for (let r = 0; r < records.length; r++) { - $("tbody").append(``); + $("tbody").append(``); // creates row for each record const lastRow = $(".row:last"); for (let i = 0; i < records[r].length; i++) { - lastRow.append(`${records[r][i]}`); - } - if(records[r].includes(inputValue)){ - - lastRow.css('background-color', '#DDC0B4'); - + lastRow.append(`${records[r][i]}`); //assign each record to their column in a specified row + } + if (records[r].includes(inputValue)) { + + lastRow.css('background-color', '#DDC0B4'); // hightlights the searched row + } tbody.append(lastRow); - $('#page').empty(); - $('#page').append(`Showing record: ${firstNumber} - ${lastNumber}`); - $('#loader').hide() } } catch (error) { console.error("Error displaying records:", error); @@ -115,77 +129,73 @@ async function displayRecords(): Promise { } displayRecords() -async function updateRecordsAndResize(inputValue: number) { - const count: number = await fetchRecordCount() - 1; - if (inputValue < 0 || inputValue > count) { - $('.modal').css('display','block') - $('.modal-content').append(`

${inputValue} is not a number within the range.Please try a different number

`) - $('#searchInput').val(''); +async function updateRecordsAndResize(inputValue: number) { // calulates the range of records according to the search input + count = await fetchRecordCount() - 1; + if (inputValue < 0 || inputValue > count) { // check if the search input + $('.modal').css('display', 'block') //opens modal if search input is not within range + $('.modal-content').append(`

${inputValue} is not a number within the range.Please try a different number

`) + $('#searchInput').val(''); // empties search bar return; } - calculatedRows = await adjustRowsByScreenHeight(); - const quarterRange = Math.floor(calculatedRows / 2); + calculatedRows = await adjustRowsByScreenHeight(); + const quarterRange = Math.floor(calculatedRows / 2); // divides the calculated max rows in half + firstNumber = Math.max(0, inputValue - quarterRange); lastNumber = Math.min(count, firstNumber + (calculatedRows - 1)); await displayRecords(); } - $('#closeModalBtn').on("click", () => { - $('.modal').css('display','none') + $('.modal').css('display', 'none') // closes modal }); - $('.btnSearch').on('click', async (event: any) => { event.preventDefault(); - inputValue = $('#searchInput').val() as number; - await updateRecordsAndResize(inputValue); + let inputValue = $('#searchInput').val() as number; + await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked + }); -async function rightArrow(): Promise { - $('#searchInput').val(''); - const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); - let count: number = await fetchRecordCount() - 1; - if (lastRow) { +async function rightArrow(): Promise { // moves records to the next list + $('#searchInput').val('') + const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); // retrieves the last row + count = await fetchRecordCount() - 1; + if (lastRow) { // checks if the last row exists const cells = lastRow.querySelectorAll("td"); const lastRecord: string[] = []; cells.forEach((cell) => { lastRecord.push(cell.textContent || ""); }); - const lastID = parseFloat(lastRecord[0]); - if (0 <= lastID && lastID <= (count)) { - + const lastID = parseFloat(lastRecord[0]); // determines te value in the last row + if (0 <= lastID && lastID <= (count)) { // checks if the last value is within range const tbody = $("tbody"); - tbody.empty(); - firstNumber = lastID + 1; + tbody.empty(); // empties the table + firstNumber = lastID + 1; // calculates the first number of the page calculatedRows = await adjustRowsByScreenHeight(); - lastNumber = firstNumber + (calculatedRows - 1) - await displayRecords(); - }else{ - $('.arrow-right').css('display','none') + lastNumber = firstNumber + (calculatedRows - 1) // calculates the first number of the page + await displayRecords(); // display the new records } - - } } -async function leftArrow(): Promise { - $('#searchInput').val(''); - let count: number = await fetchRecordCount() - 1; - const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); - if (firstRow) { +async function leftArrow(): Promise { // moves records to the previous list + $('#searchInput').val('') + count = await fetchRecordCount() - 1; + const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); // retrieves the first row + if (firstRow) { // checks if the first row exists const cells = firstRow.querySelectorAll("td"); const firstRecord: string[] = []; cells.forEach((cell) => { firstRecord.push(cell.textContent || ""); }); - const firstID = parseFloat(firstRecord[0]); - const tbody = $("tbody"); - tbody.empty(); - const calculatedRows = await adjustRowsByScreenHeight(); - lastNumber = firstID - 1; - firstNumber = lastNumber - (calculatedRows-1) - await displayRecords(); - -} + const firstID = parseFloat(firstRecord[0]); // determines te value in the first row + if (0 <= firstID && firstID <= (count)) { // checks if the first value is within range + const tbody = $("tbody"); + tbody.empty(); // empties the table + const calculatedRows = await adjustRowsByScreenHeight(); + lastNumber = firstID - 1; // calculates the last number of the page + firstNumber = lastNumber - (calculatedRows - 1) // uses the last number to calculate + await displayRecords(); + } + } } diff --git a/index.html b/index.html index 94de68b1..571fc4fb 100644 --- a/index.html +++ b/index.html @@ -22,17 +22,19 @@

OnBoard-Javascript

-
+ + + -
+ @@ -43,9 +45,7 @@

OnBoard-Javascript

-
- - + diff --git a/style.css b/style.css index a43c7451..96db0c4c 100644 --- a/style.css +++ b/style.css @@ -4,39 +4,32 @@ body{ padding: 0; margin: 0; } -.tableRecords{ - position: absolute; - display: flex; - height: 95vh; -} -/* .tableRecords tbody{ - height: 80%; - flex-shrink: 1; - -} */ + + tbody .row td{ - height: 1.5rem; + padding: 0.5rem; } -.header{ - display: block; -} + table{ width: 100%; table-layout: fixed; - margin-bottom: 5% ; + } table, th, td { border: 1px solid #4B3832; border-collapse: collapse; text-align: center; + min-height: 1rem; + color: black; + } -.tableRecords thead .head { +#recordsTable thead .head { position: sticky; background-color: #97694F; border: 2px solid black; @@ -53,18 +46,12 @@ td:hover{ text-align: center; text-decoration: underline; color: #4B3832; - - } - - .showRecords{ - - /* transform: translate(49rem, -0.5rem); */ - position: absolute; - padding: 1rem; + top: 0.8%; right: 0; - top: 1.2%; + padding: 1rem; + position: absolute; } .showRecords #page{ display: inline-block; @@ -101,7 +88,7 @@ td:hover{ } .search-container { - float: left; + left:0; position: absolute; top: 2.15%; } From b4e7f57cffd9cc0361e2da6ad9755401988152d9 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Mon, 28 Aug 2023 10:09:07 +0200 Subject: [PATCH 07/33] format change --- app.ts | 359 +++++++++++++++++++++++++-------------------------------- 1 file changed, 160 insertions(+), 199 deletions(-) diff --git a/app.ts b/app.ts index 8a06275b..748b82a3 100644 --- a/app.ts +++ b/app.ts @@ -5,234 +5,195 @@ var calculatedRows: number | null = null; var count: number | null = null; async function fetchRecordCount(): Promise { // fetches the number of records from the server - try { - const recordCount = await fetch(`http://localhost:2050/recordCount`); - if (!recordCount.ok) { - throw new Error('Failed to fetch record count'); - } - const data = await recordCount.json() - return data; - } catch (error) { - console.error('Error fetching the record count:', error); - throw error; - } + try { + const recordCount = await fetch(`http://localhost:2050/recordCount`); + if (!recordCount.ok) { + throw new Error('Failed to fetch record count'); + } + const data = await recordCount.json() + return data; + } catch (error) { + console.error('Error fetching the record count:', error); + throw error; + } } function fetchColumns(): void { //fetches the different columns headings from server - fetch("http://localhost:2050/columns") - .then((response: Response) => { - return response.json() as Promise; - }) - .then((columns: string[]) => { - const colArray = columns; // assigns the columns to an array - - for (let c = 0; c < colArray.length; c++) { - $(".head").append(`${colArray[c]}`); // creates a single column for each heading - } - }) + fetch("http://localhost:2050/columns") + .then((response: Response) => { + return response.json() as Promise; + }) + .then((columns: string[]) => { + const colArray = columns; // assigns the columns to an array + + for (let c = 0; c < colArray.length; c++) { + $(".head").append(`${colArray[c]}`); // creates a single column for each heading + } + }) } fetchColumns() async function adjustRowsByScreenHeight(): Promise { // calculates the number of rows that can fit the screen - const screenHeight = window.innerHeight; // calculates screen height - const availableHeight = screenHeight - 105 // subtracts the space used from the screeen - let rowHeight = 35 - if(availableHeight <= 0){ - return 0; - }else{ - let maxRows = Math.floor(availableHeight / rowHeight) ; - return maxRows; // returns the maximum rows that can fit into screen - } + const screenHeight = window.innerHeight; // calculates screen height + const availableHeight = screenHeight - 105 // subtracts the space used from the screeen + let rowHeight = 35 + if (availableHeight <= 0) { + return 0; + } else { + let maxRows = Math.floor(availableHeight / rowHeight); + return maxRows; // returns the maximum rows that can fit into screen + } } let resizeTimeout: number; $(window).on('resize', async () => { - clearTimeout(resizeTimeout); // cancel previously schedule Timeout - - resizeTimeout = setTimeout(async () => { - $('#loader').show() - calculatedRows = await adjustRowsByScreenHeight(); // to recalcualte the number of max rows - await displayRecords() - - let inputValue = $('#searchInput').val() ; // priotizes the search input value if available - if (inputValue !== '') { - await updateRecordsAndResize(Number(inputValue)) - } - $('#loader').hide(); - }, 500); + clearTimeout(resizeTimeout); // cancel previously schedule Timeout + + resizeTimeout = setTimeout(async () => { + $('#loader').show() + calculatedRows = await adjustRowsByScreenHeight(); // to recalcualte the number of max rows + await displayRecords() + + let inputValue = $('#searchInput').val(); // priotizes the search input value if available + if (inputValue !== '') { + await updateRecordsAndResize(Number(inputValue)) + } + $('#loader').hide(); + }, 500); }) async function fetchRecords(from: number, to: number): Promise { // fetches records from specified number to a certain number - const response = await fetch(`http://localhost:2050/records?from=${from}&to=${to}`); - if (!response.ok) { - throw new Error("Sorry, there's a problem with the network"); - } - return response.json(); + const response = await fetch(`http://localhost:2050/records?from=${from}&to=${to}`); + if (!response.ok) { + throw new Error("Sorry, there's a problem with the network"); + } + return response.json(); } async function displayRecords(): Promise { //displays records from firstNumber to lastNumber - try { - $('#loader').show() - count = await fetchRecordCount() - 1; - calculatedRows = await adjustRowsByScreenHeight(); - const inputValue = $('#searchInput').val() as number; - if(calculatedRows === 0){ - lastNumber = firstNumber - - } - if (!lastNumber) { - lastNumber = firstNumber + (calculatedRows - 1); - } - if (firstNumber < 0) { - firstNumber = 0; - lastNumber = firstNumber + (calculatedRows - 1); - } - else { - lastNumber = firstNumber + (calculatedRows - 1); - } - - let records; - if(lastNumber <= count && lastNumber >= 0){ - records = await fetchRecords(firstNumber, lastNumber); - $('#page').empty(); - $('#page').append(`Showing record: ${firstNumber} - ${lastNumber}`); // changes the record range showing - $('#loader').hide() - }else{ - firstNumber = count - (calculatedRows-1) - lastNumber = count; - records = await fetchRecords(firstNumber, count); - $('#page').empty(); - $('#page').append(`Showing record: ${firstNumber} - ${count}`); - $('#loader').hide() - - } - - - const tbody = $("tbody"); - tbody.empty(); - for (let r = 0; r < records.length; r++) { - $("tbody").append(``); // creates row for each record - const lastRow = $(".row:last"); - for (let i = 0; i < records[r].length; i++) { - lastRow.append(`${records[r][i]}`); //assign each record to their column in a specified row - } - if (records[r].includes(inputValue)) { - - lastRow.css('background-color', '#DDC0B4'); // hightlights the searched row - - } - tbody.append(lastRow); - } - } catch (error) { - console.error("Error displaying records:", error); - } + try { + $('#loader').show() + count = await fetchRecordCount() - 1; + calculatedRows = await adjustRowsByScreenHeight(); + const inputValue = $('#searchInput').val() as number; + if (calculatedRows === 0) { + lastNumber = firstNumber + + } + if (!lastNumber) { + lastNumber = firstNumber + (calculatedRows - 1); + } + if (firstNumber < 0) { + firstNumber = 0; + lastNumber = firstNumber + (calculatedRows - 1); + } + else { + lastNumber = firstNumber + (calculatedRows - 1); + } + + let records; + if (lastNumber <= count && lastNumber >= 0) { + records = await fetchRecords(firstNumber, lastNumber); + $('#page').empty(); + $('#page').append(`Showing record: ${firstNumber} - ${lastNumber}`); // changes the record range showing + $('#loader').hide() + } else { + firstNumber = count - (calculatedRows - 1) + lastNumber = count; + records = await fetchRecords(firstNumber, count); + $('#page').empty(); + $('#page').append(`Showing record: ${firstNumber} - ${count}`); + $('#loader').hide() + + } + + + const tbody = $("tbody"); + tbody.empty(); + for (let r = 0; r < records.length; r++) { + $("tbody").append(``); // creates row for each record + const lastRow = $(".row:last"); + for (let i = 0; i < records[r].length; i++) { + lastRow.append(`${records[r][i]}`); //assign each record to their column in a specified row + } + if (records[r].includes(inputValue)) { + + lastRow.css('background-color', '#DDC0B4'); // hightlights the searched row + + } + tbody.append(lastRow); + } + } catch (error) { + console.error("Error displaying records:", error); + } } displayRecords() async function updateRecordsAndResize(inputValue: number) { // calulates the range of records according to the search input - count = await fetchRecordCount() - 1; - if (inputValue < 0 || inputValue > count) { // check if the search input - $('.modal').css('display', 'block') //opens modal if search input is not within range - $('.modal-content').append(`

${inputValue} is not a number within the range.Please try a different number

`) - $('#searchInput').val(''); // empties search bar - return; - } - calculatedRows = await adjustRowsByScreenHeight(); - const quarterRange = Math.floor(calculatedRows / 2); // divides the calculated max rows in half - - firstNumber = Math.max(0, inputValue - quarterRange); - lastNumber = Math.min(count, firstNumber + (calculatedRows - 1)); - await displayRecords(); + count = await fetchRecordCount() - 1; + if (inputValue < 0 || inputValue > count) { // check if the search input + $('.modal').css('display', 'block') //opens modal if search input is not within range + $('.modal-content').append(`

${inputValue} is not a number within the range.Please try a different number

`) + $('#searchInput').val(''); // empties search bar + return; + } + calculatedRows = await adjustRowsByScreenHeight(); + const quarterRange = Math.floor(calculatedRows / 2); // divides the calculated max rows in half + + firstNumber = Math.max(0, inputValue - quarterRange); + lastNumber = Math.min(count, firstNumber + (calculatedRows - 1)); + await displayRecords(); } $('#closeModalBtn').on("click", () => { - $('.modal').css('display', 'none') // closes modal + $('.modal').css('display', 'none') // closes modal }); $('.btnSearch').on('click', async (event: any) => { - event.preventDefault(); - let inputValue = $('#searchInput').val() as number; - await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked - + event.preventDefault(); + let inputValue = $('#searchInput').val() as number; + await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked + }); async function rightArrow(): Promise { // moves records to the next list - $('#searchInput').val('') - const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); // retrieves the last row - count = await fetchRecordCount() - 1; - if (lastRow) { // checks if the last row exists - const cells = lastRow.querySelectorAll("td"); - const lastRecord: string[] = []; - cells.forEach((cell) => { - lastRecord.push(cell.textContent || ""); - }); - const lastID = parseFloat(lastRecord[0]); // determines te value in the last row - if (0 <= lastID && lastID <= (count)) { // checks if the last value is within range - const tbody = $("tbody"); - tbody.empty(); // empties the table - firstNumber = lastID + 1; // calculates the first number of the page - calculatedRows = await adjustRowsByScreenHeight(); - lastNumber = firstNumber + (calculatedRows - 1) // calculates the first number of the page - await displayRecords(); // display the new records - } - } + $('#searchInput').val('') + const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); // retrieves the last row + count = await fetchRecordCount() - 1; + if (lastRow) { // checks if the last row exists + const cells = lastRow.querySelectorAll("td"); + const lastRecord: string[] = []; + cells.forEach((cell) => { + lastRecord.push(cell.textContent || ""); + }); + const lastID = parseFloat(lastRecord[0]); // determines te value in the last row + if (0 <= lastID && lastID <= (count)) { // checks if the last value is within range + const tbody = $("tbody"); + tbody.empty(); // empties the table + firstNumber = lastID + 1; // calculates the first number of the page + calculatedRows = await adjustRowsByScreenHeight(); + lastNumber = firstNumber + (calculatedRows - 1) // calculates the first number of the page + await displayRecords(); // display the new records + } + } } async function leftArrow(): Promise { // moves records to the previous list - $('#searchInput').val('') - count = await fetchRecordCount() - 1; - const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); // retrieves the first row - if (firstRow) { // checks if the first row exists - const cells = firstRow.querySelectorAll("td"); - const firstRecord: string[] = []; - cells.forEach((cell) => { - firstRecord.push(cell.textContent || ""); - }); - const firstID = parseFloat(firstRecord[0]); // determines te value in the first row - if (0 <= firstID && firstID <= (count)) { // checks if the first value is within range - const tbody = $("tbody"); - tbody.empty(); // empties the table - const calculatedRows = await adjustRowsByScreenHeight(); - lastNumber = firstID - 1; // calculates the last number of the page - firstNumber = lastNumber - (calculatedRows - 1) // uses the last number to calculate - await displayRecords(); - } - } + $('#searchInput').val('') + count = await fetchRecordCount() - 1; + const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); // retrieves the first row + if (firstRow) { // checks if the first row exists + const cells = firstRow.querySelectorAll("td"); + const firstRecord: string[] = []; + cells.forEach((cell) => { + firstRecord.push(cell.textContent || ""); + }); + const firstID = parseFloat(firstRecord[0]); // determines te value in the first row + if (0 <= firstID && firstID <= (count)) { // checks if the first value is within range + const tbody = $("tbody"); + tbody.empty(); // empties the table + const calculatedRows = await adjustRowsByScreenHeight(); + lastNumber = firstID - 1; // calculates the last number of the page + firstNumber = lastNumber - (calculatedRows - 1) // uses the last number to calculate + await displayRecords(); + } + } } - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From 1f6c3a3300db7456248bf8f2f74437ca47d97986 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Mon, 28 Aug 2023 13:10:43 +0200 Subject: [PATCH 08/33] review changes --- app.ts | 68 ++++++++++++++++++++++------------------------------------ 1 file changed, 26 insertions(+), 42 deletions(-) diff --git a/app.ts b/app.ts index 748b82a3..c40bcfc0 100644 --- a/app.ts +++ b/app.ts @@ -1,10 +1,9 @@ import { ajax, css } from "jquery"; -var firstNumber: number = 0; -var lastNumber: number; -var calculatedRows: number | null = null; -var count: number | null = null; +let firstNumber: number = 0; +let lastNumber: number = 0; -async function fetchRecordCount(): Promise { // fetches the number of records from the server + +async function fetchRecordCount(): Promise { try { const recordCount = await fetch(`http://localhost:2050/recordCount`); if (!recordCount.ok) { @@ -18,42 +17,38 @@ async function fetchRecordCount(): Promise { // fetches the number of re } } -function fetchColumns(): void { //fetches the different columns headings from server +function fetchColumns(): void { fetch("http://localhost:2050/columns") .then((response: Response) => { return response.json() as Promise; }) .then((columns: string[]) => { - const colArray = columns; // assigns the columns to an array + const colArray = columns; for (let c = 0; c < colArray.length; c++) { - $(".head").append(`${colArray[c]}`); // creates a single column for each heading + $(".head").append(`${colArray[c]}`); } }) } fetchColumns() -async function adjustRowsByScreenHeight(): Promise { // calculates the number of rows that can fit the screen - const screenHeight = window.innerHeight; // calculates screen height +function adjustRowsByScreenHeight() { + const screenHeight = window.innerHeight; const availableHeight = screenHeight - 105 // subtracts the space used from the screeen let rowHeight = 35 if (availableHeight <= 0) { return 0; } else { let maxRows = Math.floor(availableHeight / rowHeight); - return maxRows; // returns the maximum rows that can fit into screen + return maxRows; } } -let resizeTimeout: number; $(window).on('resize', async () => { - clearTimeout(resizeTimeout); // cancel previously schedule Timeout - + let resizeTimeout: number; resizeTimeout = setTimeout(async () => { $('#loader').show() - calculatedRows = await adjustRowsByScreenHeight(); // to recalcualte the number of max rows await displayRecords() - let inputValue = $('#searchInput').val(); // priotizes the search input value if available if (inputValue !== '') { await updateRecordsAndResize(Number(inputValue)) @@ -62,7 +57,7 @@ $(window).on('resize', async () => { }, 500); }) -async function fetchRecords(from: number, to: number): Promise { // fetches records from specified number to a certain number +async function fetchRecords(from: number, to: number): Promise { const response = await fetch(`http://localhost:2050/records?from=${from}&to=${to}`); if (!response.ok) { throw new Error("Sorry, there's a problem with the network"); @@ -70,27 +65,21 @@ async function fetchRecords(from: number, to: number): Promise { // fetch return response.json(); } -async function displayRecords(): Promise { //displays records from firstNumber to lastNumber +async function displayRecords(): Promise { try { $('#loader').show() - count = await fetchRecordCount() - 1; - calculatedRows = await adjustRowsByScreenHeight(); + let count = await fetchRecordCount() - 1; + let calculatedRows = adjustRowsByScreenHeight(); const inputValue = $('#searchInput').val() as number; if (calculatedRows === 0) { lastNumber = firstNumber - - } - if (!lastNumber) { - lastNumber = firstNumber + (calculatedRows - 1); } if (firstNumber < 0) { firstNumber = 0; lastNumber = firstNumber + (calculatedRows - 1); - } - else { + } else { lastNumber = firstNumber + (calculatedRows - 1); } - let records; if (lastNumber <= count && lastNumber >= 0) { records = await fetchRecords(firstNumber, lastNumber); @@ -104,10 +93,7 @@ async function displayRecords(): Promise { //displays records from firstNu $('#page').empty(); $('#page').append(`Showing record: ${firstNumber} - ${count}`); $('#loader').hide() - } - - const tbody = $("tbody"); tbody.empty(); for (let r = 0; r < records.length; r++) { @@ -117,9 +103,7 @@ async function displayRecords(): Promise { //displays records from firstNu lastRow.append(`${records[r][i]}`); //assign each record to their column in a specified row } if (records[r].includes(inputValue)) { - lastRow.css('background-color', '#DDC0B4'); // hightlights the searched row - } tbody.append(lastRow); } @@ -129,35 +113,35 @@ async function displayRecords(): Promise { //displays records from firstNu } displayRecords() -async function updateRecordsAndResize(inputValue: number) { // calulates the range of records according to the search input - count = await fetchRecordCount() - 1; +async function updateRecordsAndResize(inputValue: number) { + let count = await fetchRecordCount() - 1; if (inputValue < 0 || inputValue > count) { // check if the search input $('.modal').css('display', 'block') //opens modal if search input is not within range $('.modal-content').append(`

${inputValue} is not a number within the range.Please try a different number

`) $('#searchInput').val(''); // empties search bar return; } - calculatedRows = await adjustRowsByScreenHeight(); + let calculatedRows = adjustRowsByScreenHeight(); const quarterRange = Math.floor(calculatedRows / 2); // divides the calculated max rows in half - firstNumber = Math.max(0, inputValue - quarterRange); lastNumber = Math.min(count, firstNumber + (calculatedRows - 1)); await displayRecords(); } + $('#closeModalBtn').on("click", () => { $('.modal').css('display', 'none') // closes modal }); + $('.btnSearch').on('click', async (event: any) => { event.preventDefault(); let inputValue = $('#searchInput').val() as number; await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked - }); -async function rightArrow(): Promise { // moves records to the next list +async function rightArrow(): Promise { $('#searchInput').val('') const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); // retrieves the last row - count = await fetchRecordCount() - 1; + let count = await fetchRecordCount() - 1; if (lastRow) { // checks if the last row exists const cells = lastRow.querySelectorAll("td"); const lastRecord: string[] = []; @@ -169,16 +153,16 @@ async function rightArrow(): Promise { // moves records to the next list const tbody = $("tbody"); tbody.empty(); // empties the table firstNumber = lastID + 1; // calculates the first number of the page - calculatedRows = await adjustRowsByScreenHeight(); + let calculatedRows = await adjustRowsByScreenHeight(); lastNumber = firstNumber + (calculatedRows - 1) // calculates the first number of the page await displayRecords(); // display the new records } } } -async function leftArrow(): Promise { // moves records to the previous list +async function leftArrow(): Promise { $('#searchInput').val('') - count = await fetchRecordCount() - 1; + let count = await fetchRecordCount() - 1; const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); // retrieves the first row if (firstRow) { // checks if the first row exists const cells = firstRow.querySelectorAll("td"); From eef44e5cd8096dc375f4edb2ef10bd61ad5b8f98 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Tue, 29 Aug 2023 08:17:00 +0200 Subject: [PATCH 09/33] update requested changes --- app.ts | 131 ++++++++++++++++++++++++++++++++------------------------- 1 file changed, 73 insertions(+), 58 deletions(-) diff --git a/app.ts b/app.ts index c40bcfc0..72d1accb 100644 --- a/app.ts +++ b/app.ts @@ -1,48 +1,59 @@ import { ajax, css } from "jquery"; -let firstNumber: number = 0; -let lastNumber: number = 0; - +let firstNumber = 0; +let lastNumber = 0; +let backend: string = "http://localhost:2050"; async function fetchRecordCount(): Promise { try { - const recordCount = await fetch(`http://localhost:2050/recordCount`); - if (!recordCount.ok) { + const response = await fetch(`${backend}/recordCount`); + if (!response.ok) { throw new Error('Failed to fetch record count'); } - const data = await recordCount.json() - return data; + return response.json() } catch (error) { console.error('Error fetching the record count:', error); throw error; - } -} + }; +}; -function fetchColumns(): void { - fetch("http://localhost:2050/columns") - .then((response: Response) => { - return response.json() as Promise; - }) - .then((columns: string[]) => { - const colArray = columns; +async function fetchColumns(): Promise { + try { + const response = await fetch(`${backend}/columns`); + if (!response.ok) { + throw new Error('Failed to fetch columns'); + } + const jsonText = await response.text(); + const columns: string[] = await JSON.parse(jsonText); + return columns + } catch (error) { + console.error('Error fetching columns', error) + throw error + }; +}; - for (let c = 0; c < colArray.length; c++) { - $(".head").append(`${colArray[c]}`); - } - }) -} -fetchColumns() +async function createTable() { + try { + const columns = await fetchColumns() + for (const col of columns) { + $(".head").append(`${col}`); + } + } catch (error) { + console.error('Error creating table', error) + }; +}; +createTable() function adjustRowsByScreenHeight() { const screenHeight = window.innerHeight; - const availableHeight = screenHeight - 105 // subtracts the space used from the screeen - let rowHeight = 35 + const availableHeight = screenHeight - 105; // subtracts the space used from the screeen + let rowHeight = 35; if (availableHeight <= 0) { return 0; } else { let maxRows = Math.floor(availableHeight / rowHeight); return maxRows; - } -} + }; +}; $(window).on('resize', async () => { let resizeTimeout: number; @@ -55,15 +66,20 @@ $(window).on('resize', async () => { } $('#loader').hide(); }, 500); -}) +}); async function fetchRecords(from: number, to: number): Promise { - const response = await fetch(`http://localhost:2050/records?from=${from}&to=${to}`); - if (!response.ok) { - throw new Error("Sorry, there's a problem with the network"); + try { + const response = await fetch(`${backend}/records?from=${from}&to=${to}`); + if (!response.ok) { + throw new Error("Sorry, there's a problem with the network"); + } + return response.json(); + } catch (error) { + console.error('Error fetching records from server', error); + throw error } - return response.json(); -} +}; async function displayRecords(): Promise { try { @@ -73,8 +89,7 @@ async function displayRecords(): Promise { const inputValue = $('#searchInput').val() as number; if (calculatedRows === 0) { lastNumber = firstNumber - } - if (firstNumber < 0) { + } else if (firstNumber < 0) { firstNumber = 0; lastNumber = firstNumber + (calculatedRows - 1); } else { @@ -93,24 +108,24 @@ async function displayRecords(): Promise { $('#page').empty(); $('#page').append(`Showing record: ${firstNumber} - ${count}`); $('#loader').hide() - } + }; const tbody = $("tbody"); tbody.empty(); - for (let r = 0; r < records.length; r++) { + for (const record of records) { $("tbody").append(``); // creates row for each record const lastRow = $(".row:last"); - for (let i = 0; i < records[r].length; i++) { - lastRow.append(`${records[r][i]}`); //assign each record to their column in a specified row + for (const value of record) { + lastRow.append(`${value}`); // assign each record to their column in a specified row } - if (records[r].includes(inputValue)) { - lastRow.css('background-color', '#DDC0B4'); // hightlights the searched row + if (record.includes(inputValue)) { + lastRow.css('background-color', '#DDC0B4'); // highlights the searched row } - tbody.append(lastRow); + $("tbody").append(lastRow); } } catch (error) { console.error("Error displaying records:", error); - } -} + }; +}; displayRecords() async function updateRecordsAndResize(inputValue: number) { @@ -126,7 +141,7 @@ async function updateRecordsAndResize(inputValue: number) { firstNumber = Math.max(0, inputValue - quarterRange); lastNumber = Math.min(count, firstNumber + (calculatedRows - 1)); await displayRecords(); -} +}; $('#closeModalBtn').on("click", () => { $('.modal').css('display', 'none') // closes modal @@ -144,21 +159,21 @@ async function rightArrow(): Promise { let count = await fetchRecordCount() - 1; if (lastRow) { // checks if the last row exists const cells = lastRow.querySelectorAll("td"); - const lastRecord: string[] = []; - cells.forEach((cell) => { + const lastRecord = []; + for (const cell of Array.from(cells)) { lastRecord.push(cell.textContent || ""); - }); + } const lastID = parseFloat(lastRecord[0]); // determines te value in the last row if (0 <= lastID && lastID <= (count)) { // checks if the last value is within range const tbody = $("tbody"); tbody.empty(); // empties the table firstNumber = lastID + 1; // calculates the first number of the page - let calculatedRows = await adjustRowsByScreenHeight(); + let calculatedRows = adjustRowsByScreenHeight(); lastNumber = firstNumber + (calculatedRows - 1) // calculates the first number of the page await displayRecords(); // display the new records - } - } -} + }; + }; +}; async function leftArrow(): Promise { $('#searchInput').val('') @@ -166,18 +181,18 @@ async function leftArrow(): Promise { const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); // retrieves the first row if (firstRow) { // checks if the first row exists const cells = firstRow.querySelectorAll("td"); - const firstRecord: string[] = []; - cells.forEach((cell) => { + const firstRecord = []; + for (const cell of Array.from(cells)) { firstRecord.push(cell.textContent || ""); - }); + } const firstID = parseFloat(firstRecord[0]); // determines te value in the first row if (0 <= firstID && firstID <= (count)) { // checks if the first value is within range const tbody = $("tbody"); tbody.empty(); // empties the table - const calculatedRows = await adjustRowsByScreenHeight(); + const calculatedRows = adjustRowsByScreenHeight(); lastNumber = firstID - 1; // calculates the last number of the page firstNumber = lastNumber - (calculatedRows - 1) // uses the last number to calculate await displayRecords(); - } - } -} + }; + }; +}; From 097c7c1b0ccb6cf214b6993987205863585ba8c6 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Tue, 29 Aug 2023 08:37:53 +0200 Subject: [PATCH 10/33] update requested changes, index and style --- index.html | 18 +----------------- style.css | 31 ++----------------------------- 2 files changed, 3 insertions(+), 46 deletions(-) diff --git a/index.html b/index.html index 571fc4fb..73656c61 100644 --- a/index.html +++ b/index.html @@ -4,8 +4,8 @@ JS Onboard Project + -

OnBoard-Javascript

@@ -15,40 +15,24 @@

OnBoard-Javascript

-
-
- - - - - - - - -
- - - - - diff --git a/style.css b/style.css index 96db0c4c..d41f2d10 100644 --- a/style.css +++ b/style.css @@ -4,30 +4,19 @@ body{ padding: 0; margin: 0; } - - - tbody .row td{ padding: 0.5rem; } - - - table{ width: 100%; table-layout: fixed; - - } table, th, td { border: 1px solid #4B3832; border-collapse: collapse; text-align: center; min-height: 1rem; - - color: black; - - + color: black; } #recordsTable thead .head { position: sticky; @@ -35,13 +24,10 @@ table, th, td { border: 2px solid black; top:0; } - - td:hover{ color: #97694F; background-color:#DDC0B4; - } - +} .heading { text-align: center; text-decoration: underline; @@ -62,7 +48,6 @@ td:hover{ border: none; background-color: transparent; } - .arrow-right, .arrow-left{ display: inline-block; @@ -71,22 +56,18 @@ td:hover{ border-top: 5px solid #4B3832; border-left: 5px solid #4B3832; } - .arrow-right, .arrow-left:hover { color: #97694F; } - .arrow-right{ transform: rotate(135deg); float: right; } - .arrow-left{ transform: rotate(-45deg); float: left; } - .search-container { left:0; position: absolute; @@ -120,9 +101,7 @@ td:hover{ height: 100px; animation: spin 2s linear infinite; background-color: #f7e7ce; - } - @keyframes spin { 0% { transform: rotate(0deg); @@ -141,7 +120,6 @@ td:hover{ background-color: #f7e7ce; border: 1px solid #4B3832; } - .modal-content { background-color: #4B3832; margin: 1rem; @@ -150,14 +128,9 @@ td:hover{ color: #f7e7ce; font-size: 1.5rem; } - .close { float: right; font-size: rem; font-weight: bold; cursor: pointer; } - - - - From 3e38dff34e11c4c8826ce617f4c5f809efb50168 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Tue, 29 Aug 2023 11:44:50 +0200 Subject: [PATCH 11/33] update search --- app.ts | 17 +++++++++++------ index.html | 6 +++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app.ts b/app.ts index 72d1accb..56e1a90d 100644 --- a/app.ts +++ b/app.ts @@ -132,25 +132,30 @@ async function updateRecordsAndResize(inputValue: number) { let count = await fetchRecordCount() - 1; if (inputValue < 0 || inputValue > count) { // check if the search input $('.modal').css('display', 'block') //opens modal if search input is not within range - $('.modal-content').append(`

${inputValue} is not a number within the range.Please try a different number

`) + $('.content').append(`

${inputValue} is not a number within the range.Please try a different number

`) $('#searchInput').val(''); // empties search bar return; } let calculatedRows = adjustRowsByScreenHeight(); - const quarterRange = Math.floor(calculatedRows / 2); // divides the calculated max rows in half - firstNumber = Math.max(0, inputValue - quarterRange); + const halfRange = Math.floor(calculatedRows / 2); // divides the calculated max rows in half + firstNumber = Math.max(0, inputValue - halfRange); lastNumber = Math.min(count, firstNumber + (calculatedRows - 1)); await displayRecords(); }; $('#closeModalBtn').on("click", () => { + $('.content').empty() $('.modal').css('display', 'none') // closes modal }); -$('.btnSearch').on('click', async (event: any) => { +$('#btnSearch').on("click", async (event) => { event.preventDefault(); - let inputValue = $('#searchInput').val() as number; - await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked + try { + let inputValue = $('#searchInput').val() as number; + await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked + } catch (error) { + console.error('An error occurred:', error); + } }); async function rightArrow(): Promise { diff --git a/index.html b/index.html index 73656c61..acc00c9c 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,7 @@ JS Onboard Project - +
@@ -12,13 +12,13 @@

OnBoard-Javascript

- +
From 3c23cb2af3921a5e6d8a5001b87dbcb4ab0a18f5 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Wed, 30 Aug 2023 08:19:11 +0200 Subject: [PATCH 12/33] first push 30/08 --- app.ts | 70 +++++++------- index.html | 27 +++--- style.css | 265 +++++++++++++++++++++++++++++------------------------ 3 files changed, 194 insertions(+), 168 deletions(-) diff --git a/app.ts b/app.ts index 56e1a90d..bb34b137 100644 --- a/app.ts +++ b/app.ts @@ -3,6 +3,24 @@ let firstNumber = 0; let lastNumber = 0; let backend: string = "http://localhost:2050"; +window.onload = () => { + createTable(); + displayRecords(); + $('#btnSearch').on("click", async (event) => { + event.preventDefault(); + try { + let inputValue = $('#searchInput').val() as number; + await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked + } catch (error) { + throw new Error('An error occurred') + } + }); + $('#closeModalBtn').on("click", () => { + $('.content').empty() + $('.modal').css('display', 'none') // closes modal + }); +} + async function fetchRecordCount(): Promise { try { const response = await fetch(`${backend}/recordCount`); @@ -11,8 +29,7 @@ async function fetchRecordCount(): Promise { } return response.json() } catch (error) { - console.error('Error fetching the record count:', error); - throw error; + throw new Error('Error fetching the record count') }; }; @@ -26,22 +43,20 @@ async function fetchColumns(): Promise { const columns: string[] = await JSON.parse(jsonText); return columns } catch (error) { - console.error('Error fetching columns', error) - throw error + throw new Error('Error fetching columns') }; }; async function createTable() { try { - const columns = await fetchColumns() + const columns = await fetchColumns(); for (const col of columns) { $(".head").append(`${col}`); } } catch (error) { - console.error('Error creating table', error) + throw new Error('Error creating table'); }; }; -createTable() function adjustRowsByScreenHeight() { const screenHeight = window.innerHeight; @@ -76,14 +91,13 @@ async function fetchRecords(from: number, to: number): Promise { } return response.json(); } catch (error) { - console.error('Error fetching records from server', error); - throw error + throw new Error('Error fetching records from server'); } }; async function displayRecords(): Promise { try { - $('#loader').show() + $('#loader').show(); let count = await fetchRecordCount() - 1; let calculatedRows = adjustRowsByScreenHeight(); const inputValue = $('#searchInput').val() as number; @@ -100,14 +114,14 @@ async function displayRecords(): Promise { records = await fetchRecords(firstNumber, lastNumber); $('#page').empty(); $('#page').append(`Showing record: ${firstNumber} - ${lastNumber}`); // changes the record range showing - $('#loader').hide() + $('#loader').hide(); } else { - firstNumber = count - (calculatedRows - 1) + firstNumber = count - (calculatedRows - 1); lastNumber = count; records = await fetchRecords(firstNumber, count); $('#page').empty(); $('#page').append(`Showing record: ${firstNumber} - ${count}`); - $('#loader').hide() + $('#loader').hide(); }; const tbody = $("tbody"); tbody.empty(); @@ -123,16 +137,15 @@ async function displayRecords(): Promise { $("tbody").append(lastRow); } } catch (error) { - console.error("Error displaying records:", error); + throw new Error('Error displaying records') }; }; -displayRecords() async function updateRecordsAndResize(inputValue: number) { let count = await fetchRecordCount() - 1; if (inputValue < 0 || inputValue > count) { // check if the search input - $('.modal').css('display', 'block') //opens modal if search input is not within range - $('.content').append(`

${inputValue} is not a number within the range.Please try a different number

`) + $('.modal').css('display', 'block');//opens modal if search input is not within range + $('.content').append(`

${inputValue} is not a number within the range.Please try a different number

`); $('#searchInput').val(''); // empties search bar return; } @@ -143,23 +156,8 @@ async function updateRecordsAndResize(inputValue: number) { await displayRecords(); }; -$('#closeModalBtn').on("click", () => { - $('.content').empty() - $('.modal').css('display', 'none') // closes modal -}); - -$('#btnSearch').on("click", async (event) => { - event.preventDefault(); - try { - let inputValue = $('#searchInput').val() as number; - await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked - } catch (error) { - console.error('An error occurred:', error); - } -}); - async function rightArrow(): Promise { - $('#searchInput').val('') + $('#searchInput').val(''); const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); // retrieves the last row let count = await fetchRecordCount() - 1; if (lastRow) { // checks if the last row exists @@ -174,14 +172,14 @@ async function rightArrow(): Promise { tbody.empty(); // empties the table firstNumber = lastID + 1; // calculates the first number of the page let calculatedRows = adjustRowsByScreenHeight(); - lastNumber = firstNumber + (calculatedRows - 1) // calculates the first number of the page + lastNumber = firstNumber + (calculatedRows - 1);// calculates the first number of the page await displayRecords(); // display the new records }; }; }; async function leftArrow(): Promise { - $('#searchInput').val('') + $('#searchInput').val(''); let count = await fetchRecordCount() - 1; const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); // retrieves the first row if (firstRow) { // checks if the first row exists @@ -196,7 +194,7 @@ async function leftArrow(): Promise { tbody.empty(); // empties the table const calculatedRows = adjustRowsByScreenHeight(); lastNumber = firstID - 1; // calculates the last number of the page - firstNumber = lastNumber - (calculatedRows - 1) // uses the last number to calculate + firstNumber = lastNumber - (calculatedRows - 1); // uses the last number to calculate await displayRecords(); }; }; diff --git a/index.html b/index.html index acc00c9c..c7e9ef00 100644 --- a/index.html +++ b/index.html @@ -1,38 +1,41 @@ + JS Onboard Project - + + -
+

OnBoard-Javascript

+ +
- -
+ +
-
+ - - + +
+ diff --git a/style.css b/style.css index d41f2d10..590d170b 100644 --- a/style.css +++ b/style.css @@ -1,136 +1,161 @@ -body{ - overflow: hidden; - background-color: #f7e7ce; - padding: 0; - margin: 0; -} -tbody .row td{ - padding: 0.5rem; -} -table{ - width: 100%; - table-layout: fixed; -} -table, th, td { - border: 1px solid #4B3832; - border-collapse: collapse; - text-align: center; - min-height: 1rem; - color: black; -} +body { + overflow: hidden; + background-color: #f7e7ce; + padding: 0; + margin: 0; +} + +tbody .row td { + padding: 0.5rem; +} + +table { + width: 100%; + table-layout: fixed; +} + +table, +th, +td { + border: 1px solid #4B3832; + border-collapse: collapse; + text-align: center; + min-height: 1rem; + color: black; +} + #recordsTable thead .head { - position: sticky; - background-color: #97694F; - border: 2px solid black; - top:0; -} -td:hover{ - color: #97694F; - background-color:#DDC0B4; -} -.heading { - text-align: center; - text-decoration: underline; - color: #4B3832; -} -.showRecords{ - top: 0.8%; - right: 0; - padding: 1rem; - position: absolute; -} -.showRecords #page{ - display: inline-block; - font-size: 1.2rem; -} -.arrow-right, -.arrow-left{ - border: none; - background-color: transparent; -} + position: sticky; + background-color: #97694F; + border: 2px solid black; + top: 0; +} + +td:hover { + color: #97694F; + background-color: #DDC0B4; +} + +.heading { + text-align: center; + text-decoration: underline; + color: #4B3832; +} + +.showRecords { + top: 0.8%; + right: 0; + padding: 1rem; + position: absolute; +} + +.showRecords #page { + display: inline-block; + font-size: 1.2rem; +} + .arrow-right, -.arrow-left{ - display: inline-block; - width: 25px; - height: 25px; - border-top: 5px solid #4B3832; - border-left: 5px solid #4B3832; +.arrow-left { + border: none; + background-color: transparent; } + .arrow-right, +.arrow-left { + display: inline-block; + width: 25px; + height: 25px; + border-top: 5px solid #4B3832; + border-left: 5px solid #4B3832; +} + +.arrow-right:hover, .arrow-left:hover { - color: #97694F; + color: black; + } -.arrow-right{ - transform: rotate(135deg); - float: right; + +.arrow-right { + transform: rotate(135deg); + float: right; } -.arrow-left{ - transform: rotate(-45deg); - float: left; + +.arrow-left { + transform: rotate(-45deg); + float: left; } + .search-container { - left:0; - position: absolute; - top: 2.15%; + left: 0; + position: absolute; + top: 2.15%; } + .search-container input[type=number] { - padding: 6px; - font-size: 1rem; - border: 2px solid #4B3832; - background-color: #f7e7ce; - -} -.btnSearch{ - font-size: 1rem; - border: 2px solid #4B3832; - padding: 6px; - background-color:#97694F ; - color: #f7e7ce; -} -#loader{ - position: absolute; - left: 50%; - margin-left: -50px; - top: 50%; - margin-top: -50px; - border: calc(200px / 10) solid #97694F; - border-top: 20px solid #4B3832; - border-radius: 100%; - display:none; - width: 100px; - height: 100px; - animation: spin 2s linear infinite; - background-color: #f7e7ce; -} + padding: 6px; + font-size: 1rem; + border: 2px solid #4B3832; + background-color: #f7e7ce; + +} + +.btnSearch { + font-size: 1rem; + border: 2px solid #4B3832; + padding: 6px; + background-color: #97694F; + color: #f7e7ce; +} + +#loader { + position: absolute; + left: 50%; + margin-left: -50px; + top: 50%; + margin-top: -50px; + border: calc(200px / 10) solid #97694F; + border-top: 20px solid #4B3832; + border-radius: 100%; + display: none; + width: 100px; + height: 100px; + animation: spin 2s linear infinite; + background-color: #f7e7ce; +} + @keyframes spin { - 0% { - transform: rotate(0deg); - } - 100% { - transform: rotate(360deg); - } -} + 0% { + transform: rotate(0deg); + } + + 100% { + transform: rotate(360deg); + } +} + .modal { - display: none; - position: fixed; - transform:translateX(50%); - z-index: 1; - top: 0; - width: 50%; - background-color: #f7e7ce; - border: 1px solid #4B3832; -} + display: none; + position: fixed; + transform: translateX(50%); + z-index: 1; + top: 0; + width: 50%; + background-color: #f7e7ce; + border: 1px solid #4B3832; +} + .modal-content { - background-color: #4B3832; - margin: 1rem; - padding: 2rem; - border: 1px solid #DDC0B4; - color: #f7e7ce; - font-size: 1.5rem; -} + background-color: #4B3832; + margin: 1rem; + padding: 2rem; + border: 1px solid #DDC0B4; + color: #f7e7ce; + font-size: 1.5rem; +} + .close { - float: right; - font-size: rem; - font-weight: bold; - cursor: pointer; + float: right; + font-size: rem; + font-weight: bold; + cursor: pointer; } From d8792ca2c41f19f57c069bec0eef0a7ddaa4664f Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Wed, 30 Aug 2023 12:17:43 +0200 Subject: [PATCH 13/33] onclick --- app.ts | 8 +++++++- index.html | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app.ts b/app.ts index bb34b137..d4daa28d 100644 --- a/app.ts +++ b/app.ts @@ -19,7 +19,13 @@ window.onload = () => { $('.content').empty() $('.modal').css('display', 'none') // closes modal }); -} + $('.arrow-right').on('click', () => { + rightArrow(); + }); + $('.arrow-left').on('click', () => { + leftArrow(); + }); +}; async function fetchRecordCount(): Promise { try { diff --git a/index.html b/index.html index c7e9ef00..21b3407b 100644 --- a/index.html +++ b/index.html @@ -24,9 +24,9 @@

OnBoard-Javascript

- +
- +
From 55837eb76737e82fe37850762026f73d5240b6e4 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Wed, 30 Aug 2023 15:35:08 +0200 Subject: [PATCH 14/33] last push 30/08 --- app.ts | 39 +++++++++++++++++++++++++-------------- index.html | 2 +- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/app.ts b/app.ts index d4daa28d..3b563acd 100644 --- a/app.ts +++ b/app.ts @@ -8,12 +8,8 @@ window.onload = () => { displayRecords(); $('#btnSearch').on("click", async (event) => { event.preventDefault(); - try { - let inputValue = $('#searchInput').val() as number; - await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked - } catch (error) { - throw new Error('An error occurred') - } + let inputValue = $('#searchInput').val() as number; + await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked }); $('#closeModalBtn').on("click", () => { $('.content').empty() @@ -21,10 +17,21 @@ window.onload = () => { }); $('.arrow-right').on('click', () => { rightArrow(); - }); + }); $('.arrow-left').on('click', () => { leftArrow(); }); + $('#searchInput').on('keydown', (event) => { + if (event.key === 'e' || event.key === 'E') { + event.preventDefault(); + }; + }); + $('#searchInput').on('input', (event) => { + const inputValue = $('#searchInput').val() as string; + if (inputValue.includes('.')) { + $('#searchInput').val(inputValue.replace('.', ' ')); + }; + }); }; async function fetchRecordCount(): Promise { @@ -58,7 +65,7 @@ async function createTable() { const columns = await fetchColumns(); for (const col of columns) { $(".head").append(``); - } + }; } catch (error) { throw new Error('Error creating table'); }; @@ -98,7 +105,7 @@ async function fetchRecords(from: number, to: number): Promise { return response.json(); } catch (error) { throw new Error('Error fetching records from server'); - } + }; }; async function displayRecords(): Promise { @@ -114,7 +121,7 @@ async function displayRecords(): Promise { lastNumber = firstNumber + (calculatedRows - 1); } else { lastNumber = firstNumber + (calculatedRows - 1); - } + }; let records; if (lastNumber <= count && lastNumber >= 0) { records = await fetchRecords(firstNumber, lastNumber); @@ -143,7 +150,7 @@ async function displayRecords(): Promise { $("tbody").append(lastRow); } } catch (error) { - throw new Error('Error displaying records') + throw new Error('Error displaying records'); }; }; @@ -154,7 +161,7 @@ async function updateRecordsAndResize(inputValue: number) { $('.content').append(`

${inputValue} is not a number within the range.Please try a different number

`); $('#searchInput').val(''); // empties search bar return; - } + }; let calculatedRows = adjustRowsByScreenHeight(); const halfRange = Math.floor(calculatedRows / 2); // divides the calculated max rows in half firstNumber = Math.max(0, inputValue - halfRange); @@ -163,6 +170,7 @@ async function updateRecordsAndResize(inputValue: number) { }; async function rightArrow(): Promise { + $('.arrow-right').css('display', 'none') $('#searchInput').val(''); const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); // retrieves the last row let count = await fetchRecordCount() - 1; @@ -171,7 +179,7 @@ async function rightArrow(): Promise { const lastRecord = []; for (const cell of Array.from(cells)) { lastRecord.push(cell.textContent || ""); - } + }; const lastID = parseFloat(lastRecord[0]); // determines te value in the last row if (0 <= lastID && lastID <= (count)) { // checks if the last value is within range const tbody = $("tbody"); @@ -180,11 +188,13 @@ async function rightArrow(): Promise { let calculatedRows = adjustRowsByScreenHeight(); lastNumber = firstNumber + (calculatedRows - 1);// calculates the first number of the page await displayRecords(); // display the new records + $('.arrow-right').css('display', 'inline-block') }; }; }; async function leftArrow(): Promise { + $('.arrow-left').css('display', 'none') $('#searchInput').val(''); let count = await fetchRecordCount() - 1; const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); // retrieves the first row @@ -193,7 +203,7 @@ async function leftArrow(): Promise { const firstRecord = []; for (const cell of Array.from(cells)) { firstRecord.push(cell.textContent || ""); - } + }; const firstID = parseFloat(firstRecord[0]); // determines te value in the first row if (0 <= firstID && firstID <= (count)) { // checks if the first value is within range const tbody = $("tbody"); @@ -202,6 +212,7 @@ async function leftArrow(): Promise { lastNumber = firstID - 1; // calculates the last number of the page firstNumber = lastNumber - (calculatedRows - 1); // uses the last number to calculate await displayRecords(); + $('.arrow-left').css('display', 'inline-block') }; }; }; diff --git a/index.html b/index.html index 21b3407b..b7cd038b 100644 --- a/index.html +++ b/index.html @@ -20,7 +20,7 @@

OnBoard-Javascript

- +
From 10b0d4086e03e7fcc17f9c50745a995b671dbe51 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Tue, 5 Sep 2023 08:12:37 +0200 Subject: [PATCH 15/33] 09/05 --- app.ts | 360 ++++++++++++++++++++++++++++++----------------------- index.html | 5 +- style.css | 34 +++-- 3 files changed, 227 insertions(+), 172 deletions(-) diff --git a/app.ts b/app.ts index 3b563acd..e3dcad8d 100644 --- a/app.ts +++ b/app.ts @@ -1,75 +1,70 @@ import { ajax, css } from "jquery"; -let firstNumber = 0; -let lastNumber = 0; -let backend: string = "http://localhost:2050"; +class GlobalVariables { + firstNumber = 0; + lastNumber = 0; + backend = "http://localhost:2050"; -window.onload = () => { - createTable(); - displayRecords(); - $('#btnSearch').on("click", async (event) => { - event.preventDefault(); - let inputValue = $('#searchInput').val() as number; - await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked - }); - $('#closeModalBtn').on("click", () => { - $('.content').empty() - $('.modal').css('display', 'none') // closes modal - }); - $('.arrow-right').on('click', () => { - rightArrow(); - }); - $('.arrow-left').on('click', () => { - leftArrow(); - }); - $('#searchInput').on('keydown', (event) => { - if (event.key === 'e' || event.key === 'E') { - event.preventDefault(); - }; - }); - $('#searchInput').on('input', (event) => { - const inputValue = $('#searchInput').val() as string; - if (inputValue.includes('.')) { - $('#searchInput').val(inputValue.replace('.', ' ')); - }; - }); -}; + async fetchRecordCount(): Promise { + return fetch(`${this.backend}/recordCount`) + .then((res) => { + if (!res.ok) { + throw 'Failed to fetch record count'; + } + return res.json(); + }) + .catch((err) => { + throw 'Error fetching the record count: ' + err; + }); + } -async function fetchRecordCount(): Promise { - try { - const response = await fetch(`${backend}/recordCount`); - if (!response.ok) { - throw new Error('Failed to fetch record count'); - } - return response.json() - } catch (error) { - throw new Error('Error fetching the record count') - }; -}; + async fetchColumns(): Promise { + return fetch(`${this.backend}/columns`) + .then(res => { + if (!res.ok) { + throw 'Failed to fetch columns'; + } + return res.json(); + }).catch(err => { + throw 'Error fetching columns' + err; + }) + } -async function fetchColumns(): Promise { - try { - const response = await fetch(`${backend}/columns`); - if (!response.ok) { - throw new Error('Failed to fetch columns'); - } - const jsonText = await response.text(); - const columns: string[] = await JSON.parse(jsonText); - return columns - } catch (error) { - throw new Error('Error fetching columns') - }; -}; + async fetchRecords(from: number, to: number): Promise { + return fetch(`${this.backend}/records?from=${from}&to=${to}`) + .then(res => { + if (!res.ok) { + throw "Sorry, there's a problem with the network"; + } + return res.json(); + }).catch(err => { + throw 'Error fetching records from server ' + err; + }); + } +} + +const globalVars = new GlobalVariables(); +let recordCount: number; + +globalVars.fetchRecordCount() + .then((count) => { + recordCount = count - 1; // Assign the fetched count to the global variable + displayRecords(recordCount); + }) + .catch(err => { + throw 'Error fetching record count' + err; + }) async function createTable() { - try { - const columns = await fetchColumns(); - for (const col of columns) { - $(".head").append(`
`); - }; - } catch (error) { - throw new Error('Error creating table'); - }; -}; + return globalVars.fetchColumns() + .then(columns => { + for (const col of columns) { + $(".head").append(``); + } + }) + .catch(err => { + throw 'Error creating table' + err; + }) +} function adjustRowsByScreenHeight() { const screenHeight = window.innerHeight; @@ -80,123 +75,141 @@ function adjustRowsByScreenHeight() { } else { let maxRows = Math.floor(availableHeight / rowHeight); return maxRows; - }; -}; + } +} $(window).on('resize', async () => { let resizeTimeout: number; resizeTimeout = setTimeout(async () => { - $('#loader').show() - await displayRecords() + $('#loader').show(); + await displayRecords(recordCount); let inputValue = $('#searchInput').val(); // priotizes the search input value if available if (inputValue !== '') { - await updateRecordsAndResize(Number(inputValue)) + await updateRecordsAndResize(Number(inputValue)); } $('#loader').hide(); - }, 500); -}); - -async function fetchRecords(from: number, to: number): Promise { - try { - const response = await fetch(`${backend}/records?from=${from}&to=${to}`); - if (!response.ok) { - throw new Error("Sorry, there's a problem with the network"); - } - return response.json(); - } catch (error) { - throw new Error('Error fetching records from server'); - }; -}; + }, 500) +}) -async function displayRecords(): Promise { - try { - $('#loader').show(); - let count = await fetchRecordCount() - 1; - let calculatedRows = adjustRowsByScreenHeight(); - const inputValue = $('#searchInput').val() as number; - if (calculatedRows === 0) { - lastNumber = firstNumber - } else if (firstNumber < 0) { - firstNumber = 0; - lastNumber = firstNumber + (calculatedRows - 1); - } else { - lastNumber = firstNumber + (calculatedRows - 1); - }; - let records; - if (lastNumber <= count && lastNumber >= 0) { - records = await fetchRecords(firstNumber, lastNumber); - $('#page').empty(); - $('#page').append(`Showing record: ${firstNumber} - ${lastNumber}`); // changes the record range showing - $('#loader').hide(); +async function displayRecords(recordCount: number): Promise { + $('#loader').show(); + let calculatedRows = adjustRowsByScreenHeight(); + const inputValue = $('#searchInput').val() as number; + if (calculatedRows === 0) { + globalVars.lastNumber = globalVars.firstNumber; + } else if (globalVars.firstNumber < 0) { + globalVars.firstNumber = 0; + globalVars.lastNumber = globalVars.firstNumber + (calculatedRows - 1); + } else { + globalVars.lastNumber = globalVars.firstNumber + (calculatedRows - 1); + } + if (globalVars.firstNumber === 0) { + $('.arrow-left').hide(); + } else { + $('.arrow-left').show(); + } + if (globalVars.lastNumber >= recordCount) { + $('.arrow-right').hide(); + } else { + $('.arrow-right').show(); + } + if (!isNaN(globalVars.firstNumber) && !isNaN(globalVars.lastNumber) && !isNaN(recordCount) && !isNaN(calculatedRows)) { + if (globalVars.lastNumber <= recordCount && globalVars.lastNumber >= 0) { + return globalVars + .fetchRecords(globalVars.firstNumber, globalVars.lastNumber) + .then((records) => { + $('#page').empty(); + $('#page').append(`Showing record: ${globalVars.firstNumber} - ${globalVars.lastNumber}`); + $('#loader').hide(); + const tbody = $("tbody"); + tbody.empty(); + for (const record of records) { + $("tbody").append(``); // creates row for each record + const lastRow = $(".row:last"); + for (const value of record) { + lastRow.append(``); // assign each record to their column in a specified row + } + if (record.includes(inputValue)) { + lastRow.css('background-color', '#DDC0B4'); // highlights the searched row + } + $("tbody").append(lastRow); + } + }) + .catch((err) => { + throw 'Error fetching records' + err; + }); } else { - firstNumber = count - (calculatedRows - 1); - lastNumber = count; - records = await fetchRecords(firstNumber, count); - $('#page').empty(); - $('#page').append(`Showing record: ${firstNumber} - ${count}`); - $('#loader').hide(); - }; - const tbody = $("tbody"); - tbody.empty(); - for (const record of records) { - $("tbody").append(``); // creates row for each record - const lastRow = $(".row:last"); - for (const value of record) { - lastRow.append(``); // assign each record to their column in a specified row - } - if (record.includes(inputValue)) { - lastRow.css('background-color', '#DDC0B4'); // highlights the searched row - } - $("tbody").append(lastRow); + globalVars.firstNumber = recordCount - (calculatedRows - 1); + globalVars.lastNumber = recordCount; + return globalVars + .fetchRecords(globalVars.firstNumber, recordCount) + .then((records) => { + $('#page').empty(); + $('#page').append(`Showing record: ${globalVars.firstNumber} - ${globalVars.lastNumber}`); + $('#loader').hide(); + const tbody = $("tbody"); + tbody.empty(); + for (const record of records) { + $("tbody").append(``); // creates row for each record + const lastRow = $(".row:last"); + for (const value of record) { + lastRow.append(``); // assign each record to their column in a specified row + } + if (record.includes(inputValue)) { + lastRow.css('background-color', '#DDC0B4'); // highlights the searched row + } + $("tbody").append(lastRow); + $('#loader').hide(); + } + }) + .catch((err) => { + throw 'Error fetching records' + err; + }) } - } catch (error) { - throw new Error('Error displaying records'); - }; -}; + } else { + throw new Error("Invalid numeric values detected."); + } +} async function updateRecordsAndResize(inputValue: number) { - let count = await fetchRecordCount() - 1; - if (inputValue < 0 || inputValue > count) { // check if the search input + if (inputValue < 0 || inputValue > recordCount) { // check if the search input $('.modal').css('display', 'block');//opens modal if search input is not within range - $('.content').append(`

${inputValue} is not a number within the range.Please try a different number

`); + $('.content').append(`

${inputValue} is not a number within the range. Please try a different number

`); $('#searchInput').val(''); // empties search bar - return; - }; + return + } let calculatedRows = adjustRowsByScreenHeight(); const halfRange = Math.floor(calculatedRows / 2); // divides the calculated max rows in half - firstNumber = Math.max(0, inputValue - halfRange); - lastNumber = Math.min(count, firstNumber + (calculatedRows - 1)); - await displayRecords(); -}; + globalVars.firstNumber = Math.max(0, inputValue - halfRange); + globalVars.lastNumber = Math.min(recordCount, globalVars.firstNumber + (calculatedRows - 1)); + await displayRecords(recordCount); +} async function rightArrow(): Promise { - $('.arrow-right').css('display', 'none') + $('#page').empty(); $('#searchInput').val(''); const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); // retrieves the last row - let count = await fetchRecordCount() - 1; if (lastRow) { // checks if the last row exists const cells = lastRow.querySelectorAll("td"); const lastRecord = []; for (const cell of Array.from(cells)) { lastRecord.push(cell.textContent || ""); - }; + } const lastID = parseFloat(lastRecord[0]); // determines te value in the last row - if (0 <= lastID && lastID <= (count)) { // checks if the last value is within range + if (0 <= lastID && lastID <= (recordCount)) { // checks if the last value is within range const tbody = $("tbody"); tbody.empty(); // empties the table - firstNumber = lastID + 1; // calculates the first number of the page + globalVars.firstNumber = lastID + 1; // calculates the first number of the page let calculatedRows = adjustRowsByScreenHeight(); - lastNumber = firstNumber + (calculatedRows - 1);// calculates the first number of the page - await displayRecords(); // display the new records - $('.arrow-right').css('display', 'inline-block') - }; - }; -}; + globalVars.lastNumber = globalVars.firstNumber + (calculatedRows - 1);// calculates the first number of the page + await displayRecords(recordCount); // display the new records + } + } +} async function leftArrow(): Promise { - $('.arrow-left').css('display', 'none') + $('#page').empty(); $('#searchInput').val(''); - let count = await fetchRecordCount() - 1; const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); // retrieves the first row if (firstRow) { // checks if the first row exists const cells = firstRow.querySelectorAll("td"); @@ -205,14 +218,45 @@ async function leftArrow(): Promise { firstRecord.push(cell.textContent || ""); }; const firstID = parseFloat(firstRecord[0]); // determines te value in the first row - if (0 <= firstID && firstID <= (count)) { // checks if the first value is within range + if (0 <= firstID && firstID <= (recordCount)) { // checks if the first value is within range const tbody = $("tbody"); tbody.empty(); // empties the table const calculatedRows = adjustRowsByScreenHeight(); - lastNumber = firstID - 1; // calculates the last number of the page - firstNumber = lastNumber - (calculatedRows - 1); // uses the last number to calculate - await displayRecords(); - $('.arrow-left').css('display', 'inline-block') - }; - }; -}; + globalVars.lastNumber = firstID - 1; // calculates the last number of the page + globalVars.firstNumber = globalVars.lastNumber - (calculatedRows - 1); // uses the last number to calculate + await displayRecords(recordCount); + } + } +} + +window.onload = () => { + createTable(); + displayRecords(recordCount); + $('#btnSearch').on("click", async (event) => { + event.preventDefault(); + let inputValue = $('#searchInput').val() as number; + $('#page').empty(); + await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked + }) + $('#closeModalBtn').on("click", () => { + $('.content').empty(); + $('.modal').css('display', 'none'); // closes modal + }) + $('.arrow-right').on('click', () => { + rightArrow(); + }) + $('.arrow-left').on('click', () => { + leftArrow(); + }) + $('#searchInput').on('keydown', (event) => { + if (event.key === 'e' || event.key === 'E') { + event.preventDefault(); + } + }) + $('#searchInput').on('input', (event) => { + const inputValue = $('#searchInput').val() as string; + if (inputValue.includes('.')) { + $('#searchInput').val(inputValue.replace('.', '')); + } + }) +} diff --git a/index.html b/index.html index b7cd038b..c2879fd4 100644 --- a/index.html +++ b/index.html @@ -9,7 +9,9 @@ -
+
+
+

OnBoard-Javascript

${col}${col}${col}
${value}
${value}
${value}
+ diff --git a/style.css b/style.css index 590d170b..5ba89838 100644 --- a/style.css +++ b/style.css @@ -109,19 +109,27 @@ td:hover { #loader { position: absolute; - left: 50%; - margin-left: -50px; - top: 50%; - margin-top: -50px; - border: calc(200px / 10) solid #97694F; - border-top: 20px solid #4B3832; - border-radius: 100%; - display: none; - width: 100px; - height: 100px; - animation: spin 2s linear infinite; - background-color: #f7e7ce; -} + top: 0; + left: 0; + width: 100%; + height: 100%; + background-color:#f7e7ce; + display: flex; + justify-content: center; + align-items: center; + z-index: 9999; + } + +.loader-spinner { + border: calc(200px / 10) solid #97694F; + border-top: 20px solid #4B3832; + border-radius: 100%; + width: 100px; + height: 100px; + animation: spin 2s linear infinite; + background-color: #f7e7ce; + } + @keyframes spin { 0% { From 11ea6c463c29279b0ad91b4da2da0e2a4beb20ee Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Tue, 5 Sep 2023 09:01:31 +0200 Subject: [PATCH 16/33] update --- app.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app.ts b/app.ts index e3dcad8d..caa86950 100644 --- a/app.ts +++ b/app.ts @@ -14,7 +14,7 @@ class GlobalVariables { }) .catch((err) => { throw 'Error fetching the record count: ' + err; - }); + }) } async fetchColumns(): Promise { @@ -38,12 +38,12 @@ class GlobalVariables { return res.json(); }).catch(err => { throw 'Error fetching records from server ' + err; - }); + }) } } const globalVars = new GlobalVariables(); -let recordCount: number; +let recordCount:number; globalVars.fetchRecordCount() .then((count) => { @@ -66,7 +66,7 @@ async function createTable() { }) } -function adjustRowsByScreenHeight() { +const adjustRowsByScreenHeight = ():number => { const screenHeight = window.innerHeight; const availableHeight = screenHeight - 105; // subtracts the space used from the screeen let rowHeight = 35; From 2b53415224ee79b5299e89e7fa40e2dc99bc4179 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Tue, 5 Sep 2023 11:58:14 +0200 Subject: [PATCH 17/33] update --- app.ts | 136 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 83 insertions(+), 53 deletions(-) diff --git a/app.ts b/app.ts index caa86950..280795b8 100644 --- a/app.ts +++ b/app.ts @@ -3,8 +3,9 @@ class GlobalVariables { firstNumber = 0; lastNumber = 0; backend = "http://localhost:2050"; - - async fetchRecordCount(): Promise { + resizeTimeout = 0; + //fetches the number of records from localhost + fetchRecordCount(): Promise { return fetch(`${this.backend}/recordCount`) .then((res) => { if (!res.ok) { @@ -16,8 +17,8 @@ class GlobalVariables { throw 'Error fetching the record count: ' + err; }) } - - async fetchColumns(): Promise { + //fetches columns from localhost + fetchColumns(): Promise { return fetch(`${this.backend}/columns`) .then(res => { if (!res.ok) { @@ -28,8 +29,8 @@ class GlobalVariables { throw 'Error fetching columns' + err; }) } - - async fetchRecords(from: number, to: number): Promise { + //fetches records from localhost + fetchRecords(from: number, to: number): Promise { return fetch(`${this.backend}/records?from=${from}&to=${to}`) .then(res => { if (!res.ok) { @@ -43,18 +44,19 @@ class GlobalVariables { } const globalVars = new GlobalVariables(); -let recordCount:number; +let recordCount: number; globalVars.fetchRecordCount() .then((count) => { - recordCount = count - 1; // Assign the fetched count to the global variable + // Assign the fetched count to the variable + recordCount = count - 1; displayRecords(recordCount); }) .catch(err => { throw 'Error fetching record count' + err; }) - -async function createTable() { +//Initializes the table head +function createTable() { return globalVars.fetchColumns() .then(columns => { for (const col of columns) { @@ -65,10 +67,12 @@ async function createTable() { throw 'Error creating table' + err; }) } - -const adjustRowsByScreenHeight = ():number => { +//calculates the number of rows that can fit the screen +const adjustRowsByScreenHeight = (): number => { + //retrieves screen height const screenHeight = window.innerHeight; - const availableHeight = screenHeight - 105; // subtracts the space used from the screeen + //subtracts the space used from the screeen + const availableHeight = screenHeight - 105; let rowHeight = 35; if (availableHeight <= 0) { return 0; @@ -77,24 +81,25 @@ const adjustRowsByScreenHeight = ():number => { return maxRows; } } - -$(window).on('resize', async () => { - let resizeTimeout: number; - resizeTimeout = setTimeout(async () => { - $('#loader').show(); +//calls to re-display records when screen is adjusted +$(window).on('resize', () => { + clearTimeout(globalVars.resizeTimeout); + globalVars.resizeTimeout = setTimeout(async () => { await displayRecords(recordCount); - let inputValue = $('#searchInput').val(); // priotizes the search input value if available + // priotizes the search input value if available + let inputValue = $('#searchInput').val(); if (inputValue !== '') { await updateRecordsAndResize(Number(inputValue)); } - $('#loader').hide(); - }, 500) + }, 250) + }) - +//display records from first to last number async function displayRecords(recordCount: number): Promise { $('#loader').show(); let calculatedRows = adjustRowsByScreenHeight(); const inputValue = $('#searchInput').val() as number; + const tbody = $("tbody"); if (calculatedRows === 0) { globalVars.lastNumber = globalVars.firstNumber; } else if (globalVars.firstNumber < 0) { @@ -121,23 +126,25 @@ async function displayRecords(recordCount: number): Promise { $('#page').empty(); $('#page').append(`Showing record: ${globalVars.firstNumber} - ${globalVars.lastNumber}`); $('#loader').hide(); - const tbody = $("tbody"); tbody.empty(); for (const record of records) { - $("tbody").append(``); // creates row for each record + // creates row for each record + $("tbody").append(``); const lastRow = $(".row:last"); for (const value of record) { - lastRow.append(`${value}`); // assign each record to their column in a specified row + // assign each record to their column in a specified row + lastRow.append(`${value}`); } if (record.includes(inputValue)) { - lastRow.css('background-color', '#DDC0B4'); // highlights the searched row + // highlights the searched row + lastRow.css('background-color', '#DDC0B4'); } $("tbody").append(lastRow); } }) .catch((err) => { throw 'Error fetching records' + err; - }); + }) } else { globalVars.firstNumber = recordCount - (calculatedRows - 1); globalVars.lastNumber = recordCount; @@ -147,16 +154,18 @@ async function displayRecords(recordCount: number): Promise { $('#page').empty(); $('#page').append(`Showing record: ${globalVars.firstNumber} - ${globalVars.lastNumber}`); $('#loader').hide(); - const tbody = $("tbody"); tbody.empty(); for (const record of records) { - $("tbody").append(``); // creates row for each record + // creates row for each record + $("tbody").append(``); const lastRow = $(".row:last"); for (const value of record) { - lastRow.append(`${value}`); // assign each record to their column in a specified row + //assign each record to their column in a specified row + lastRow.append(`${value}`); } if (record.includes(inputValue)) { - lastRow.css('background-color', '#DDC0B4'); // highlights the searched row + //highlights the searched row + lastRow.css('background-color', '#DDC0B4'); } $("tbody").append(lastRow); $('#loader').hide(); @@ -172,14 +181,18 @@ async function displayRecords(recordCount: number): Promise { } async function updateRecordsAndResize(inputValue: number) { - if (inputValue < 0 || inputValue > recordCount) { // check if the search input - $('.modal').css('display', 'block');//opens modal if search input is not within range + //check if the search input + if (inputValue < 0 || inputValue > recordCount) { + //opens modal if search input is not within range + $('.modal').css('display', 'block'); $('.content').append(`

${inputValue} is not a number within the range. Please try a different number

`); - $('#searchInput').val(''); // empties search bar + //empties search bar + $('#searchInput').val(''); return } let calculatedRows = adjustRowsByScreenHeight(); - const halfRange = Math.floor(calculatedRows / 2); // divides the calculated max rows in half + //divides the calculated max rows in half + const halfRange = Math.floor(calculatedRows / 2); globalVars.firstNumber = Math.max(0, inputValue - halfRange); globalVars.lastNumber = Math.min(recordCount, globalVars.firstNumber + (calculatedRows - 1)); await displayRecords(recordCount); @@ -188,21 +201,29 @@ async function updateRecordsAndResize(inputValue: number) { async function rightArrow(): Promise { $('#page').empty(); $('#searchInput').val(''); - const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); // retrieves the last row - if (lastRow) { // checks if the last row exists + //retrieves the last row + const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); + // checks if the last row exists + if (lastRow) { const cells = lastRow.querySelectorAll("td"); const lastRecord = []; for (const cell of Array.from(cells)) { lastRecord.push(cell.textContent || ""); } - const lastID = parseFloat(lastRecord[0]); // determines te value in the last row - if (0 <= lastID && lastID <= (recordCount)) { // checks if the last value is within range + //determines te value in the last row + const lastID = parseFloat(lastRecord[0]); + // checks if the last value is within range + if (0 <= lastID && lastID <= (recordCount)) { const tbody = $("tbody"); - tbody.empty(); // empties the table - globalVars.firstNumber = lastID + 1; // calculates the first number of the page + //empties the table + tbody.empty(); + //calculates the first number of the page + globalVars.firstNumber = lastID + 1; let calculatedRows = adjustRowsByScreenHeight(); - globalVars.lastNumber = globalVars.firstNumber + (calculatedRows - 1);// calculates the first number of the page - await displayRecords(recordCount); // display the new records + // calculates the last number of the page + globalVars.lastNumber = globalVars.firstNumber + (calculatedRows - 1); + //display the new records + await displayRecords(recordCount); } } } @@ -210,20 +231,27 @@ async function rightArrow(): Promise { async function leftArrow(): Promise { $('#page').empty(); $('#searchInput').val(''); - const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); // retrieves the first row - if (firstRow) { // checks if the first row exists + //retrieves the first row + const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); + //checks if the first row exists + if (firstRow) { const cells = firstRow.querySelectorAll("td"); const firstRecord = []; for (const cell of Array.from(cells)) { firstRecord.push(cell.textContent || ""); - }; - const firstID = parseFloat(firstRecord[0]); // determines te value in the first row - if (0 <= firstID && firstID <= (recordCount)) { // checks if the first value is within range + } + //determines te value in the first row + const firstID = parseFloat(firstRecord[0]); + //checks if the first value is within range + if (0 <= firstID && firstID <= (recordCount)) { const tbody = $("tbody"); - tbody.empty(); // empties the table + //empties the table + tbody.empty(); const calculatedRows = adjustRowsByScreenHeight(); - globalVars.lastNumber = firstID - 1; // calculates the last number of the page - globalVars.firstNumber = globalVars.lastNumber - (calculatedRows - 1); // uses the last number to calculate + //calculates the last number of the page + globalVars.lastNumber = firstID - 1; + //uses the last number to calculate first number + globalVars.firstNumber = globalVars.lastNumber - (calculatedRows - 1); await displayRecords(recordCount); } } @@ -236,11 +264,13 @@ window.onload = () => { event.preventDefault(); let inputValue = $('#searchInput').val() as number; $('#page').empty(); - await updateRecordsAndResize(inputValue); // calls to calculate the range once button is clicked + //calls to calculate the range once button is clicked + await updateRecordsAndResize(inputValue); }) $('#closeModalBtn').on("click", () => { $('.content').empty(); - $('.modal').css('display', 'none'); // closes modal + //closes modal + $('.modal').css('display', 'none'); }) $('.arrow-right').on('click', () => { rightArrow(); From c15fe4c103b66ca520a5e29f7a0d797049b2d274 Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Wed, 6 Sep 2023 16:25:58 +0200 Subject: [PATCH 18/33] 09/06 --- app.ts | 79 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 35 deletions(-) diff --git a/app.ts b/app.ts index 280795b8..2a320a94 100644 --- a/app.ts +++ b/app.ts @@ -4,39 +4,48 @@ class GlobalVariables { lastNumber = 0; backend = "http://localhost:2050"; resizeTimeout = 0; - //fetches the number of records from localhost + //fetches the number of records from localhost fetchRecordCount(): Promise { return fetch(`${this.backend}/recordCount`) .then((res) => { if (!res.ok) { throw 'Failed to fetch record count'; } - return res.json(); + return res.text(); + }) + .then((data) => { + return JSON.parse(data); }) .catch((err) => { throw 'Error fetching the record count: ' + err; }) } - //fetches columns from localhost + //fetches columns from localhost fetchColumns(): Promise { return fetch(`${this.backend}/columns`) .then(res => { if (!res.ok) { throw 'Failed to fetch columns'; } - return res.json(); + return res.text(); + }) + .then((data) => { + return JSON.parse(data); }).catch(err => { throw 'Error fetching columns' + err; }) } - //fetches records from localhost + //fetches records from localhost fetchRecords(from: number, to: number): Promise { return fetch(`${this.backend}/records?from=${from}&to=${to}`) .then(res => { if (!res.ok) { throw "Sorry, there's a problem with the network"; } - return res.json(); + return res.text(); + }) + .then((data) => { + return JSON.parse(data); }).catch(err => { throw 'Error fetching records from server ' + err; }) @@ -49,7 +58,7 @@ let recordCount: number; globalVars.fetchRecordCount() .then((count) => { // Assign the fetched count to the variable - recordCount = count - 1; + recordCount = count - 1; displayRecords(recordCount); }) .catch(err => { @@ -72,7 +81,7 @@ const adjustRowsByScreenHeight = (): number => { //retrieves screen height const screenHeight = window.innerHeight; //subtracts the space used from the screeen - const availableHeight = screenHeight - 105; + const availableHeight = screenHeight - 105; let rowHeight = 35; if (availableHeight <= 0) { return 0; @@ -87,12 +96,12 @@ $(window).on('resize', () => { globalVars.resizeTimeout = setTimeout(async () => { await displayRecords(recordCount); // priotizes the search input value if available - let inputValue = $('#searchInput').val(); + let inputValue = $('#searchInput').val(); if (inputValue !== '') { await updateRecordsAndResize(Number(inputValue)); } }, 250) - + }) //display records from first to last number async function displayRecords(recordCount: number): Promise { @@ -133,11 +142,11 @@ async function displayRecords(recordCount: number): Promise { const lastRow = $(".row:last"); for (const value of record) { // assign each record to their column in a specified row - lastRow.append(`${value}`); + lastRow.append(`${value}`); } if (record.includes(inputValue)) { // highlights the searched row - lastRow.css('background-color', '#DDC0B4'); + lastRow.css('background-color', '#DDC0B4'); } $("tbody").append(lastRow); } @@ -157,15 +166,15 @@ async function displayRecords(recordCount: number): Promise { tbody.empty(); for (const record of records) { // creates row for each record - $("tbody").append(``); + $("tbody").append(``); const lastRow = $(".row:last"); for (const value of record) { //assign each record to their column in a specified row - lastRow.append(`${value}`); + lastRow.append(`${value}`); } if (record.includes(inputValue)) { //highlights the searched row - lastRow.css('background-color', '#DDC0B4'); + lastRow.css('background-color', '#DDC0B4'); } $("tbody").append(lastRow); $('#loader').hide(); @@ -182,17 +191,17 @@ async function displayRecords(recordCount: number): Promise { async function updateRecordsAndResize(inputValue: number) { //check if the search input - if (inputValue < 0 || inputValue > recordCount) { + if (inputValue < 0 || inputValue > recordCount) { //opens modal if search input is not within range $('.modal').css('display', 'block'); $('.content').append(`

${inputValue} is not a number within the range. Please try a different number

`); //empties search bar - $('#searchInput').val(''); + $('#searchInput').val(''); return } let calculatedRows = adjustRowsByScreenHeight(); //divides the calculated max rows in half - const halfRange = Math.floor(calculatedRows / 2); + const halfRange = Math.floor(calculatedRows / 2); globalVars.firstNumber = Math.max(0, inputValue - halfRange); globalVars.lastNumber = Math.min(recordCount, globalVars.firstNumber + (calculatedRows - 1)); await displayRecords(recordCount); @@ -202,28 +211,28 @@ async function rightArrow(): Promise { $('#page').empty(); $('#searchInput').val(''); //retrieves the last row - const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); + const lastRow = document.querySelector("#recordsTable tbody .row:last-child"); // checks if the last row exists - if (lastRow) { + if (lastRow) { const cells = lastRow.querySelectorAll("td"); const lastRecord = []; for (const cell of Array.from(cells)) { lastRecord.push(cell.textContent || ""); } - //determines te value in the last row - const lastID = parseFloat(lastRecord[0]); + //determines te value in the last row + const lastID = parseFloat(lastRecord[0]); // checks if the last value is within range - if (0 <= lastID && lastID <= (recordCount)) { + if (0 <= lastID && lastID <= (recordCount)) { const tbody = $("tbody"); //empties the table - tbody.empty(); + tbody.empty(); //calculates the first number of the page - globalVars.firstNumber = lastID + 1; + globalVars.firstNumber = lastID + 1; let calculatedRows = adjustRowsByScreenHeight(); // calculates the last number of the page globalVars.lastNumber = globalVars.firstNumber + (calculatedRows - 1); //display the new records - await displayRecords(recordCount); + await displayRecords(recordCount); } } } @@ -232,26 +241,26 @@ async function leftArrow(): Promise { $('#page').empty(); $('#searchInput').val(''); //retrieves the first row - const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); + const firstRow = document.querySelector("#recordsTable tbody .row:first-child"); //checks if the first row exists - if (firstRow) { + if (firstRow) { const cells = firstRow.querySelectorAll("td"); const firstRecord = []; for (const cell of Array.from(cells)) { firstRecord.push(cell.textContent || ""); } //determines te value in the first row - const firstID = parseFloat(firstRecord[0]); + const firstID = parseFloat(firstRecord[0]); //checks if the first value is within range - if (0 <= firstID && firstID <= (recordCount)) { + if (0 <= firstID && firstID <= (recordCount)) { const tbody = $("tbody"); //empties the table - tbody.empty(); + tbody.empty(); const calculatedRows = adjustRowsByScreenHeight(); //calculates the last number of the page - globalVars.lastNumber = firstID - 1; + globalVars.lastNumber = firstID - 1; //uses the last number to calculate first number - globalVars.firstNumber = globalVars.lastNumber - (calculatedRows - 1); + globalVars.firstNumber = globalVars.lastNumber - (calculatedRows - 1); await displayRecords(recordCount); } } @@ -265,12 +274,12 @@ window.onload = () => { let inputValue = $('#searchInput').val() as number; $('#page').empty(); //calls to calculate the range once button is clicked - await updateRecordsAndResize(inputValue); + await updateRecordsAndResize(inputValue); }) $('#closeModalBtn').on("click", () => { $('.content').empty(); //closes modal - $('.modal').css('display', 'none'); + $('.modal').css('display', 'none'); }) $('.arrow-right').on('click', () => { rightArrow(); From 90d744baf04176c5d69dbd27d2fe71088880904b Mon Sep 17 00:00:00 2001 From: ThokozaniNqwili Date: Fri, 8 Sep 2023 09:04:17 +0200 Subject: [PATCH 19/33] 09/08 --- app.ts | 10 +++++----- index.html | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app.ts b/app.ts index 2a320a94..65b8e469 100644 --- a/app.ts +++ b/app.ts @@ -101,7 +101,6 @@ $(window).on('resize', () => { await updateRecordsAndResize(Number(inputValue)); } }, 250) - }) //display records from first to last number async function displayRecords(recordCount: number): Promise { @@ -124,16 +123,17 @@ async function displayRecords(recordCount: number): Promise { } if (globalVars.lastNumber >= recordCount) { $('.arrow-right').hide(); + } else { $('.arrow-right').show(); } + // $('#page').empty().append(`Showing record: ${globalVars.firstNumber} - ${globalVars.lastNumber}`); if (!isNaN(globalVars.firstNumber) && !isNaN(globalVars.lastNumber) && !isNaN(recordCount) && !isNaN(calculatedRows)) { if (globalVars.lastNumber <= recordCount && globalVars.lastNumber >= 0) { return globalVars .fetchRecords(globalVars.firstNumber, globalVars.lastNumber) .then((records) => { - $('#page').empty(); - $('#page').append(`Showing record: ${globalVars.firstNumber} - ${globalVars.lastNumber}`); + $('#page').empty().append(`Showing record: ${globalVars.firstNumber} - ${globalVars.lastNumber}`); $('#loader').hide(); tbody.empty(); for (const record of records) { @@ -160,8 +160,7 @@ async function displayRecords(recordCount: number): Promise { return globalVars .fetchRecords(globalVars.firstNumber, recordCount) .then((records) => { - $('#page').empty(); - $('#page').append(`Showing record: ${globalVars.firstNumber} - ${globalVars.lastNumber}`); + $('#page').empty().append(`Showing record: ${globalVars.firstNumber} - ${globalVars.lastNumber}`); $('#loader').hide(); tbody.empty(); for (const record of records) { @@ -195,6 +194,7 @@ async function updateRecordsAndResize(inputValue: number) { //opens modal if search input is not within range $('.modal').css('display', 'block'); $('.content').append(`

${inputValue} is not a number within the range. Please try a different number

`); + $('#page').empty().append(`Showing record: ${globalVars.firstNumber} - ${globalVars.lastNumber}`); //empties search bar $('#searchInput').val(''); return diff --git a/index.html b/index.html index c2879fd4..f5f78f03 100644 --- a/index.html +++ b/index.html @@ -12,7 +12,7 @@
-

OnBoard-Javascript

+

Javascript Project