From 29487de0e7e250ebcafb89e92a4ec5a30ca639ce Mon Sep 17 00:00:00 2001 From: Ajay Ghanwat Date: Thu, 6 Nov 2025 16:12:43 +0530 Subject: [PATCH] New Changes with table Classes and Modification --- src/components/Table.astro | 29 +++++++++++++++++++++++++---- src/global.d.ts | 1 + src/pages/table.astro | 9 ++++++++- src/types/table/types.ts | 1 + 4 files changed, 35 insertions(+), 5 deletions(-) diff --git a/src/components/Table.astro b/src/components/Table.astro index 52c1fd5..a4751da 100644 --- a/src/components/Table.astro +++ b/src/components/Table.astro @@ -15,6 +15,7 @@ const { classInfos = "", tableClassNames = "", hideHeader = false, + rowClick = "", } = Astro.props as TableProps; const id = `table-${Math.random().toString(36).slice(2, 9)}`; @@ -140,6 +141,7 @@ const id = `table-${Math.random().toString(36).slice(2, 9)}`; data, columnsRendererKey, classInfos, + rowClick, }} > const start = () => { @@ -173,6 +175,7 @@ const id = `table-${Math.random().toString(36).slice(2, 9)}`; `; const fetchDataFn = fetchData ? window[fetchData] : null; + const rowClickFn = rowClick ? window[rowClick] : null; const classNames = classInfos ? window[classInfos] : {}; const resolveClasses = (input, ...ctx) => { @@ -197,7 +200,7 @@ const id = `table-${Math.random().toString(36).slice(2, 9)}`; const rowCls = resolveClasses(classNames?.row, row, i); // 🖥️ Desktop - let tr = ``; tbody.insertAdjacentHTML("beforeend", tr); + tbody.onclick = (e) => { + if (!rowClickFn) return; + const tr = e.target.closest("tr[data-row-idx]"); + if (!tr) return; + const idx = Number(tr.dataset.rowIdx); + const row = dataset[idx]; + rowClickFn(row); + }; + // 📱 Mobile - let card = `
`; + let card = `
`; columns.forEach((col, colIndex) => { let value = row[col.key] ?? ""; @@ -266,13 +278,22 @@ const id = `table-${Math.random().toString(36).slice(2, 9)}`; }); card += ` -
+
${col.label}
-
${value}
+
${value}
`; }); card += `
`; mobileBody.insertAdjacentHTML("beforeend", card); + + mobileBody.onclick = (e) => { + if (!rowClickFn) return; + const tr = e.target.closest("div[data-row-idx]"); + if (!tr) return; + const idx = Number(tr.dataset.rowIdx); + const row = dataset[idx]; + rowClickFn(row); + }; }); } diff --git a/src/global.d.ts b/src/global.d.ts index 7ab01b2..838d7a5 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -13,6 +13,7 @@ declare global { hls: typeof Hls, flvjs: typeof FlvJs classNames: any; + checkDetails: any; } } diff --git a/src/pages/table.astro b/src/pages/table.astro index c80aac7..923c23b 100644 --- a/src/pages/table.astro +++ b/src/pages/table.astro @@ -279,6 +279,7 @@ const studentColumns = [ pagignations classInfos="classNames" tableClassNames="wr:rounded-2xl wr:border-2 wr:border-red-500" + rowClick="checkDetails" />
@@ -423,7 +424,7 @@ const studentColumns = [ window.classNames = { row: (_row: any, idx: number) => ({ - "wr:!bg-gray-50 wr:text-black": _row.role === "Engineer", + "wr:!bg-gray-50 wr:!text-black": _row.role === "Engineer", }), column: (col: any) => @@ -436,4 +437,10 @@ const studentColumns = [ ? "wr:text-green-600" : "", }; + + window.checkDetails = (row: any) => { + alert( + `Student Details:\n\nName: ${row.name}\nAge: ${row.age}\nRole: ${row.role}`, + ); + }; diff --git a/src/types/table/types.ts b/src/types/table/types.ts index 522b189..a7c0f88 100644 --- a/src/types/table/types.ts +++ b/src/types/table/types.ts @@ -34,4 +34,5 @@ export interface TableProps { classInfos?: string; tableClassNames?: string; hideHeader?: boolean; + rowClick?: string; } \ No newline at end of file