Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions javascript/nextjs/tables/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.*
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/versions

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.pnpm-debug.log*

# env files (can opt-in for committing if needed)
.env*

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
36 changes: 36 additions & 0 deletions javascript/nextjs/tables/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).

## Getting Started

First, run the development server:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.

This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.

## Learn More

To learn more about Next.js, take a look at the following resources:

- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.

You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!

## Deploy on Vercel

The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.

Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
121 changes: 121 additions & 0 deletions javascript/nextjs/tables/app/components/BasicTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
'use client'
import { useState } from "react";
import { ChevronDown, ChevronUp } from "lucide-react";

const DESCENDING = "desc"
const ASCENDING = "asc"

const getTableHeader = (column, sortField, sortDirection) => {
let columnValue = column.name
// TODO: If any columns can't be sorted the cursor should be cursor-not-allowed
let defaultClassName = "flex justify-center space-x-2 cursor-pointer"
if(column.key == sortField && sortDirection == DESCENDING){
return (
<div className={defaultClassName} key={column.key}>
{column.name} <ChevronDown/>
</div>
)
}else if(column.key == sortField && sortDirection == ASCENDING){
return (
<div className={defaultClassName} key={column.key}>
<div>
{column.name}
</div>
<ChevronUp/>
</div>
)
}
return (
<div className={`${defaultClassName}`} key={column.key}>
{column.name}
</div>
)
}

const BasicTable = ({columns, data}) => {
// console.log(`Data = ${JSON.stringify(data)}`)
// const rows = data.forEach((entry, idx) => {
// return
// })
// const [cursorColumn, setCursorColumn] = useState("")
const [tableData, setTableData] = useState(data)
const [sortDirection, setSortDirection] = useState("")
const [sortField, setSortField] = useState("")

const handleColumnClick = (column, event) => {
console.log(`click tracked on ${sortField} direction ${sortDirection}`)
setSortField(column.key)
let sortDirUpdate = (sortDirection == "" || sortDirection == DESCENDING) ? ASCENDING : DESCENDING;
console.log(sortDirUpdate)
setSortDirection(sortDirUpdate)
// Call function to sort data.
const sortedData = [...data].sort((a, b) => {
let a_value = a[column.key].toString()
let b_value = b[column.key].toString()
if(sortDirection == ASCENDING)
return a_value.localeCompare(b_value)
else
return b_value.localeCompare(a_value)
})

console.log(`Sorted Data by column ${column.key} \n ${JSON.stringify(data)}`)
// Update data via setTableData
setTableData(sortedData)
}

const handleMouseEnter = (column, event) => {
console.log(`Mouse entered ${column.name}`)
}

const handleMouseLeave = (column, event) => {
console.log(`Mouse left ${column.name}`)
}

return (
<div>
<header className="p-5 text-center">
<h1>Very Simple Basic Table with Row Coloring</h1>
</header>
<table className="table-auto w-full">
<thead
className="bg-gray-400"
>
<tr>
{columns.map((column) => (
<th
key={column.key}
onClick={e => handleColumnClick(column, e)}
onMouseEnter={e => handleMouseEnter(column, e)}
onMouseLeave={e => handleMouseLeave(column, e)}
>
{getTableHeader(column, sortField, sortDirection)}
</th>
))}
</tr>
</thead>
<tbody>
{tableData.map((entry, idx) => {
return (
<tr
className="odd:bg-white-100 even:bg-gray-200"
key={idx}>
{columns.map((column) => {
// console.log(`Making it into TR ${entry}`)
return (
<td
className="px-4 text-center"
key={column.key}>
{entry[column.key]}
</td>
)
})}
</tr>
)
})}
</tbody>
</table>
</div>
)
}

export default BasicTable
16 changes: 16 additions & 0 deletions javascript/nextjs/tables/app/components/FixedColumnTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use client'
import { useState } from "react";
import { ChevronDown, ChevronUp} from "lucide-react";

const FixedColumnTable = ({columns, data}) => {

return (
<div>
<header className="p-5 text-center">
<h1>Fixed Column Table</h1>
</header>
</div>
)
}

export default FixedColumnTable
50 changes: 50 additions & 0 deletions javascript/nextjs/tables/app/data/people.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
[
{
"name": "Michael Scott",
"position": "Regional Manager",
"episodes": 2000,
"company": "Dunder Mifflin",
"email": "michael.scott@dundermifflin.com",
"phone_number": "800-123-4567"
},
{
"name": "Dwight Schrute",
"position": "Assitant to Regional Manager",
"company": "Dunder Mifflin",
"episodes": 1000,
"email": "dwight.schrute@dundermifflin.com",
"phone_number": "800-456-8901"
},
{
"name": "Jim Halpert",
"position": "Salesman",
"company": "Dunder Mifflin",
"episodes": 2000,
"email": "jim.halpert@dundermifflin.com",
"phone_number": "800-456-8901"
},
{
"name": "Pam Halpert",
"position": "Receptionist/Saleswoman",
"company": "Dunder Mifflin",
"episodes": 2000,
"email": "pam.halpert@dundermifflin.com",
"phone_number": "800-456-8902"
},
{
"name": "Kevin Malone",
"position": "Accountant",
"company": "Dunder Mifflin",
"episodes": 2000,
"email": "kevin.malone@dundermifflin.com",
"phone_number": "800-124-5678"
},
{
"name": "Oscar Martinez",
"position": "Accountant",
"company": "Dunder Mifflin",
"episodes": 2000,
"email": "oscar.martinez@dundermifflin.com",
"phone_number": "800-123-5678"
}
]
Binary file added javascript/nextjs/tables/app/favicon.ico
Binary file not shown.
26 changes: 26 additions & 0 deletions javascript/nextjs/tables/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@import "tailwindcss";

:root {
--background: #ffffff;
--foreground: #171717;
}

@theme inline {
--color-background: var(--background);
--color-foreground: var(--foreground);
--font-sans: var(--font-geist-sans);
--font-mono: var(--font-geist-mono);
}

@media (prefers-color-scheme: dark) {
:root {
--background: #0a0a0a;
--foreground: #ededed;
}
}

body {
background: var(--background);
color: var(--foreground);
font-family: Arial, Helvetica, sans-serif;
}
34 changes: 34 additions & 0 deletions javascript/nextjs/tables/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import "./globals.css";

const geistSans = Geist({
variable: "--font-geist-sans",
subsets: ["latin"],
});

const geistMono = Geist_Mono({
variable: "--font-geist-mono",
subsets: ["latin"],
});

export const metadata: Metadata = {
title: "Create Next App",
description: "Generated by create next app",
};

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang="en">
<body
className={`${geistSans.variable} ${geistMono.variable} antialiased`}
>
{children}
</body>
</html>
);
}
Loading