Skip to content

Commit adb029e

Browse files
add examples
1 parent e9dc5d1 commit adb029e

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

examples/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Qwik-table-loader Examples
2+
3+
![image](https://img.shields.io/badge/Qwik-E10098?style=for-the-badge&logo=lightning&logoColor=white) ![image](https://img.shields.io/badge/TypeScript-007ACC?style=for-the-badge&logo=typescript&logoColor=white) ![image](https://img.shields.io/badge/Tailwind_CSS-38B2AC?style=for-the-badge&logo=tailwind-css&logoColor=white)
4+
5+
By [`@jimmynguyen1308`](https://github.com/jimmynguyen1308)
6+
7+
## Example 1
8+
9+
Click [here](example1.tsx) to see code
10+
11+
### Screenshot
12+
13+
![screenshot-1](screenshot-example-1.png)
14+
15+
## Example 2
16+
17+
Click [here](example2.tsx) to see code
18+
19+
### Screenshot
20+
21+
![screenshot-1](screenshot-example-2.png)

examples/example1.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import TableLoader, { filterType } from "qwik-table-loader"
33
import { CellData, FilterValue } from "qwik-table-loader/lib-types/types"
44

55
export default component$(() => {
6+
// You can have a set of options for your filter
67
const statusOptions: FilterValue[] = [
78
{
89
key: 0,
@@ -22,6 +23,7 @@ export default component$(() => {
2223
},
2324
]
2425

26+
// Given this is your raw data
2527
const data = [
2628
{
2729
id: 1,
@@ -60,6 +62,7 @@ export default component$(() => {
6062
},
6163
]
6264

65+
// Special data types (eg. Date) should be optimized before loading to the table
6366
const tData = data.map((record: any) => {
6467
return {
6568
...record,
@@ -73,6 +76,7 @@ export default component$(() => {
7376
}
7477
})
7578

79+
// Custom format table cell "dob"
7680
const formatDob = $((record: CellData, heading: string) => {
7781
const list = record[heading]?.toString()?.split("-")
7882
if (list) {
@@ -81,6 +85,7 @@ export default component$(() => {
8185
return ""
8286
})
8387

88+
// Custom format table cell "status"
8489
const formatStatus = $((record: CellData, heading: string) => {
8590
switch (record[heading]) {
8691
case 0:
@@ -118,26 +123,29 @@ export default component$(() => {
118123

119124
return (
120125
<>
121-
<Link href="/">
122-
<span class="text-blue-500 underline">&lt;&lt; Go Back</span>
123-
</Link>
124126
<TableLoader
127+
// Your optimized data.
125128
tData={tData}
129+
// Your custom elements based on the column types
126130
element={{
127131
dob: formatDob,
128132
status: formatStatus,
129133
}}
130134
tableOptions={{
135+
// Customize your heading text here...
131136
customHeadings: {
132137
dob: "Date of Birth",
133138
},
139+
// For pure CSS, you can attach class names and add .css file styling those classes
140+
// For TailwindCSS, this is where you customize your table styling
134141
classNames: {
135142
table: "my-6 mx-3 border-2 border-[#333]",
136143
thead: "bg-gray-600 border-2 border-[#333]",
137144
th: "text-white border border-[#333] p-2 px-4",
138145
thContent: "h-full flex flex-col gap-1 min-w-[30px]",
139146
thText:
140147
"min-h-[24px] flex flex-row items-center justify-between gap-2",
148+
// Arrows' size, default color and highlight color should be store like this format in sortContainer
141149
sortContainer:
142150
"flex flex-col -mr-1 size-[20] default-[#ccc] highlight-[#fff]",
143151
sortArrowUp: "-mb-[7px]",
@@ -148,22 +156,27 @@ export default component$(() => {
148156
tbody: "bg-gray-100",
149157
tr: "border border-[#ccc]",
150158
td: "border border-[#ccc] p-1 px-3",
159+
// Custom class names based on column name
151160
tcol: {
152161
id: "max-w-[80px] text-center",
153162
fullName: "font-bold",
154163
dob: "max-w-[160px] text-right",
155164
},
156165
},
166+
// Sort options: choose which columns to have the sort feature included
157167
sortOptions: {
158168
params: ["username", "fullName", "dob", "status"],
159169
},
160170
filterOptions: {
171+
// You can use the filterType enum, or simply type "search" or "options"
161172
params: {
162173
username: filterType.SEARCH,
163174
fullName: filterType.SEARCH,
164175
dob: filterType.SEARCH,
165176
status: filterType.OPTIONS,
166177
},
178+
// If a column has "options" filter, it needs to have an option list
179+
// Otherwise, the options will be blank
167180
options: {
168181
status: statusOptions,
169182
},

examples/example2.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import TableLoader, { value2Options, filterType } from "qwik-table-loader"
33
import { CellData } from "qwik-table-loader/lib-types/types"
44

55
export default component$(() => {
6+
// Given this is your raw data
7+
// If it is optimized (data types are string or number), you can parse to the table directly
68
const tData: CellData[] = [
79
{
810
id: 1,
@@ -27,9 +29,11 @@ export default component$(() => {
2729
},
2830
]
2931

32+
// You can generate option lists for your filters using the fuction value2Options() from the lib
3033
const brandOptions = value2Options(tData, "brand_name")
3134
const yearOptions = value2Options(tData, "year")
3235

36+
// Custom format table cell "year"
3337
const formatYear = $((record: CellData, heading: string) => {
3438
const currentYear = new Date().getFullYear()
3539
if (record[heading] === currentYear) {
@@ -49,18 +53,23 @@ export default component$(() => {
4953
return (
5054
<>
5155
<TableLoader
56+
// Your optimized data.
5257
tData={tData}
58+
// Your custom elements based on the column types
5359
element={{
5460
year: formatYear,
5561
}}
5662
tableOptions={{
63+
// For pure CSS, you can attach class names and add .css file styling those classes
64+
// For TailwindCSS, this is where you customize your table styling
5765
classNames: {
5866
table: "my-6 mx-3",
5967
thead: "",
6068
th: "p-2 px-4 border border-transparent border-b-black",
6169
thContent: "h-full flex flex-col gap-1 min-w-[30px]",
6270
thText:
6371
"min-h-[24px] flex flex-row items-center justify-between gap-2",
72+
// Arrows' size, default color and highlight color should be store like this format in sortContainer
6473
sortContainer:
6574
"flex flex-col -mr-1 size-[20] default-[#aaa] highlight-[#000]",
6675
sortArrowUp: "-mb-[7px]",
@@ -70,6 +79,7 @@ export default component$(() => {
7079
"w-full outline-none border border-[#aaa] text-black font-light p-1 px-2 rounded-md",
7180
tbody: "border-[3px] border-transparent border-t-black",
7281
td: "p-3",
82+
// Custom class names based on column name
7383
tcol: {
7484
id: "max-w-[80px] border border-transparent border-r-black",
7585
brand_name:
@@ -78,15 +88,19 @@ export default component$(() => {
7888
description: "w-[240px] border border-transparent border-r-black",
7989
},
8090
},
91+
// Sort options: choose which columns to have the sort feature included
8192
sortOptions: {
8293
params: ["id", "brand_name", "device", "year"],
8394
},
8495
filterOptions: {
96+
// You can use the filterType enum, or simply type "search" or "options"
8597
params: {
8698
brand_name: filterType.OPTIONS,
8799
device: filterType.SEARCH,
88100
year: filterType.OPTIONS,
89101
},
102+
// If a column has "options" filter, it needs to have an option list
103+
// Otherwise, the options will be blank
90104
options: {
91105
brand_name: brandOptions,
92106
year: yearOptions,

0 commit comments

Comments
 (0)