Please make the table output from this function sortable by clicking on the column…
- User
Please make the table output from this function sortable by clicking on the column headings.
function get_Graph(){ let rv = ''; const rows=[77, 76.5, 76.1, 76.2, 76.5, 77.2, 76.8, 76, 75, 75.5]; const ga=[77, 76.5, 76.1, 76.2, 76.5, 77.2, 76.8]; let sdt='2025-04-28'; const mx=Math.max(...ga); if (rows.length > 0) { rv +=
<div id="pid">Quiz Results: ${sdt}</div> <table> <thead> <tr> <th>#</th> <th title="Weight change">WC</th> <th width=${mx*2}px>Graph</th> </tr> </thead> <tbody>; let i = 0; let gr=''; for (const v of rows) { if (ga[1] > 0) { i++; let j=0; for (const gv of ga) { j++; let h = (v / 3.1).toFixed(1) + "px"; let l = ((j * 10) + j + 2) + "px";
cls='gr'; // if(v<1) {cls='wt'; h='31px';} gr +=<div class=${cls} style='height:${h};left:${l}' title='${j} → ${gv}'></div>;
} rv +=<tr> <td>${i}</td> <td class="no">${v.toFixed(1)}</td> <td>${gr}</td> </tr>; } }rv += ` </tbody> </table> `; } document.getElementById('body').innerHTML=rv; return rv; } Assistant
Here's the modified version of your function that includes sortable columns:
function get_Graph() { let rv = ''; const rows = [77, 76.5, 76.1, 76.2, 76.5, 77.2, 76.8, 76, 75, 75.5]; const ga = [77, 76.5, 76.1, 76.2, 76.5, 77.2, 76.8]; let sdt = '2025-04-28'; const mx = Math.max(...ga); if (rows.length > 0) { rv += `<div id="pid">Quiz Results: ${sdt}</div> <table id="sortableTable"> <thead> <tr> <th class="sortable" onclick="sortTable(0)"># ↕</th> <th class="sortable" onclick="sortTable(1)">WC ↕</th> <th width=${mx*2}px>Graph</th> </tr> </thead> <tbody>`; let i = 0; let gr = ''; for (const v of rows) { if (ga[1] > 0) { i++; let j = 0; for (const gv of ga) { j++; let h = (v / 3.1).toFixed(1) + "px"; let l = ((j * 10) + j + 2) + "px"; cls = 'gr'; gr += `<div class=${cls} style='height:${h};left:${l}' title='${j} → ${gv}'></div>`; } rv += ` <tr> <td>${i}</td> <td class="no">${v.toFixed(1)}</td> <td>${gr}</td> </tr>`; } } rv += ` </tbody> </table> `; } document.getElementById('body').innerHTML = rv; return rv; } // Add this sorting function function sortTable(n) { let table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0; table = document.getElementById("sortableTable"); switching = true; // Set the sorting direction to ascending dir = "asc"; while (switching) { switching = false; rows = table.rows; for (i = 1; i < (rows.length - 1); i++) { shouldSwitch = false; x = rows[i].getElementsByTagName("TD")[n]; y = rows[i + 1].getElementsByTagName("TD")[n]; // Convert to numbers for numerical sorting let xContent = n === 0 ? parseInt(x.innerHTML) : parseFloat(x.innerHTML); let yContent = n === 0 ? parseInt(y.innerHTML) : parseFloat(y.innerHTML); if (dir === "asc") { if (xContent > yContent) { shouldSwitch = true; break; } } else if (dir === "desc") { if (xContent < yContent) { shouldSwitch = true; break; } } } if (shouldSwitch) { rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); switching = true; switchcount++; } else { if (switchcount === 0 && dir === "asc") { dir = "desc"; switching = true; } } } }And add this CSS to style the sortable columns:
.sortable { cursor: pointer; user-select: none; } .sortable:hover { background-color: #f0f0f0; } table { border-collapse: collapse; } th, td { padding: 8px; border: 1px solid #ddd; } th { background-color: #f4f4f4; }The changes made include:
- Added an ID to the table (
sortableTable) - Added
sortableclass andonclickevents to the sortable column headers - Added visual indicators (↕) to show which columns are sortable
- Added a
sortTable()function that handles the sorting logic - Added CSS to style the sortable columns and improve the table appearance
Now you can click on the "#" or "WC" column headers to sort the table in ascending or descending order. The sorting function handles both numerical sorting for both columns. Clicking a column header multiple times will toggle between ascending and descending order.
The graph column is left unsortable since it contains complex HTML content rather than simple values.
- Added an ID to the table (
- User
Are you sure about this line? it says: Uncaught SyntaxError: Unexpected token '==='
if === (switchcount 0 && dir === "asc") { Assistant
Apologies for the syntax error. Here's the corrected version of the sorting function:
function sortTable(n) { let table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0; table = document.getElementById("sortableTable"); switching = true; // Set the sorting direction to ascending dir = "asc"; while (switching) { switching = false; rows = table.rows; for (i = 1; i < (rows.length - 1); i++) { shouldSwitch = false; x = rows[i].getElementsByTagName("TD")[n]; y = rows[i + 1].getElementsByTagName("TD")[n]; // Convert to numbers for numerical sorting let xContent = n === 0 ? parseInt(x.innerHTML) : parseFloat(x.innerHTML); let yContent = n === 0 ? parseInt(y.innerHTML) : parseFloat(y.innerHTML); if (dir === "asc") { if (xContent > yContent) { shouldSwitch = true; break; } } else if (dir === "desc") { if (xContent < yContent) { shouldSwitch = true; break; } } } if (shouldSwitch) { rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); switching = true; switchcount++; } else { if (switchcount === 0 && dir === "asc") { dir = "desc"; switching = true; } } } }The corrected line should be:
if (switchcount === 0 && dir === "asc") {This should now work correctly. The error was in the syntax of the if statement. Thank you for catching that!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.