Skip to content

Commit d2cc93c

Browse files
committed
fix sort breaking the table
1 parent 993a76b commit d2cc93c

File tree

2 files changed

+38
-37
lines changed

2 files changed

+38
-37
lines changed

src/problems-by-company/company.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,9 @@ select {
107107
height: 30px;
108108
border-radius: 5px;
109109
text-align: center;
110+
}
111+
112+
#Title,
113+
#Frequency {
114+
min-width: 200px;
110115
}

src/problems-by-company/company.ts

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -158,28 +158,9 @@ function addCompanyProblems(sortMethod: string) {
158158

159159
console.log(solutions);
160160

161-
const table = document.getElementById('solutionTable') as HTMLTableElement;
162-
163-
// Modify table rendering to include difficulty and acceptance
164-
solutions.forEach(solution => {
165-
const row = table.insertRow(-1);
166-
row.insertCell(0).innerText = solution.id.toString();
167-
const difficultyText = solution.difficulty === 1 ? 'Easy' : solution.difficulty === 2 ? 'Medium' : 'Hard';
168-
row.insertCell(1).innerText = difficultyText || 'N/A';
169-
row.insertCell(2).innerHTML = `<a href="${solution.url}" target="_blank">${solution.title}</a>`;
170-
row.insertCell(3).innerText = (solution.acceptance ? (solution.acceptance * 100).toFixed(2) + '%' : 'N/A'); // New column for acceptance
171-
// add frequency as a bar
172-
if (solution.frequency) {
173-
const frequencyCell = row.insertCell(4);
174-
const bar = document.createElement('div');
175-
const width = ((solution.frequency - minFrequency) / (maxFrequency - minFrequency)) * 100;
176-
bar.style.width = width + '%';
177-
bar.style.height = '10px';
178-
bar.style.backgroundColor = 'lightgreen';
179-
bar.style.borderRadius = '10px';
180-
frequencyCell.appendChild(bar);
181-
}
182-
});
161+
// Rebuild the table with sorted solutions
162+
rebuildTable();
163+
183164
});
184165
}
185166

@@ -234,6 +215,35 @@ async function addCompaniesToSelect() {
234215
});
235216
}
236217

218+
// Function to rebuild the table with sorted solutions
219+
function rebuildTable() {
220+
const table = document.getElementById('solutionTable') as HTMLTableElement;
221+
while (table.rows.length > 1) {
222+
table.deleteRow(1);
223+
}
224+
225+
solutions.forEach((solution) => {
226+
const row = table.insertRow(-1);
227+
row.insertCell(0).innerText = solution.id.toString();
228+
const difficultyText = solution.difficulty === 1 ? 'Easy' : solution.difficulty === 2 ? 'Medium' : 'Hard';
229+
row.insertCell(1).innerText = difficultyText || 'N/A';
230+
row.insertCell(2).innerHTML = `<a href="${solution.url}" target="_blank">${solution.title}</a>`;
231+
row.insertCell(3).innerText = (solution.acceptance ? (solution.acceptance * 100).toFixed(2) + '%' : 'N/A'); // New column for acceptance
232+
233+
// Add frequency as a bar
234+
const frequencyCell = row.insertCell(4);
235+
const bar = document.createElement('div');
236+
const width = ((solution.frequency - minFrequency) / (maxFrequency - minFrequency)) * 100;
237+
bar.style.width = width + '%';
238+
bar.style.height = '10px';
239+
bar.style.backgroundColor = 'lightgreen';
240+
bar.style.borderRadius = '10px';
241+
bar.style.border = '1px solid lightgreen';
242+
frequencyCell.appendChild(bar);
243+
});
244+
}
245+
246+
237247

238248
// Keep track of the sorting order for each column
239249
const sortOrders = {
@@ -267,22 +277,8 @@ function sortBy(column: string) {
267277
}
268278

269279
// Rebuild the table with sorted solutions
270-
solutions.forEach((solution) => {
271-
const row = table.insertRow(-1);
272-
row.insertCell(0).innerText = solution.id.toString();
273-
row.insertCell(1).innerHTML = `<a href="${solution.url}" target="_blank">${solution.title}</a>`;
280+
rebuildTable();
274281

275-
// Add frequency as a bar
276-
const frequencyCell = row.insertCell(2);
277-
const bar = document.createElement('div');
278-
const width = ((solution.frequency - minFrequency) / (maxFrequency - minFrequency)) * 100;
279-
bar.style.width = width + '%';
280-
bar.style.height = '10px';
281-
bar.style.backgroundColor = 'lightgreen';
282-
bar.style.borderRadius = '10px';
283-
bar.style.border = '1px solid lightgreen';
284-
frequencyCell.appendChild(bar);
285-
});
286282
}
287283

288284
/* Run the script */

0 commit comments

Comments
 (0)