headless-table-core 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +146 -40
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,22 +1,18 @@
|
|
|
1
|
-
# headless-table-core
|
|
1
|
+
# headless-table-core — How to Use
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
No UI.
|
|
6
|
-
No framework lock-in.
|
|
7
|
-
Works with Angular, React, Vue, or plain JavaScript.
|
|
3
|
+
This guide shows **practical usage patterns** of `headless-table-core` in real projects.
|
|
8
4
|
|
|
9
5
|
---
|
|
10
6
|
|
|
11
|
-
## Install
|
|
7
|
+
## 1️⃣ Install
|
|
12
8
|
|
|
13
9
|
```bash
|
|
14
10
|
npm install headless-table-core
|
|
15
|
-
|
|
11
|
+
```
|
|
16
12
|
|
|
17
13
|
---
|
|
18
14
|
|
|
19
|
-
## Import
|
|
15
|
+
## 2️⃣ Import
|
|
20
16
|
|
|
21
17
|
```ts
|
|
22
18
|
import {
|
|
@@ -29,7 +25,9 @@ import {
|
|
|
29
25
|
|
|
30
26
|
---
|
|
31
27
|
|
|
32
|
-
##
|
|
28
|
+
## 3️⃣ Prepare your data
|
|
29
|
+
|
|
30
|
+
Your data can come from **any API** and can be **nested**.
|
|
33
31
|
|
|
34
32
|
```ts
|
|
35
33
|
const users = [
|
|
@@ -48,80 +46,188 @@ const users = [
|
|
|
48
46
|
|
|
49
47
|
---
|
|
50
48
|
|
|
51
|
-
##
|
|
49
|
+
## 4️⃣ Display nested values (safe way)
|
|
50
|
+
|
|
51
|
+
### Problem
|
|
52
|
+
|
|
53
|
+
Direct access can crash:
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
row.user.profile.name
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Solution
|
|
60
|
+
|
|
61
|
+
Use `getValue`:
|
|
52
62
|
|
|
53
63
|
```ts
|
|
54
|
-
getValue(
|
|
55
|
-
getValue(
|
|
56
|
-
getValue(users[0], "user.address.city"); // ""
|
|
64
|
+
getValue(row, "user.name");
|
|
65
|
+
getValue(row, "user.profile.age");
|
|
57
66
|
```
|
|
58
67
|
|
|
59
68
|
With fallback:
|
|
60
69
|
|
|
61
70
|
```ts
|
|
62
|
-
getValue(
|
|
71
|
+
getValue(row, "user.address.city", "N/A");
|
|
63
72
|
```
|
|
64
73
|
|
|
74
|
+
Use this **in rendering**.
|
|
75
|
+
|
|
65
76
|
---
|
|
66
77
|
|
|
67
|
-
##
|
|
78
|
+
## 5️⃣ Search (global search)
|
|
79
|
+
|
|
80
|
+
### Step 1: Choose searchable fields
|
|
68
81
|
|
|
69
82
|
```ts
|
|
70
|
-
|
|
71
|
-
sortData(users, "user.profile.age", "desc");
|
|
83
|
+
const searchKeys = ["user.name", "email"];
|
|
72
84
|
```
|
|
73
85
|
|
|
74
|
-
|
|
75
|
-
|
|
86
|
+
### Step 2: Apply search
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
const searchedRows = filterData(users, searchText, searchKeys);
|
|
90
|
+
```
|
|
76
91
|
|
|
77
92
|
---
|
|
78
93
|
|
|
79
|
-
##
|
|
94
|
+
## 6️⃣ Sort (column sorting)
|
|
95
|
+
|
|
96
|
+
### Sort by text
|
|
80
97
|
|
|
81
98
|
```ts
|
|
82
|
-
|
|
99
|
+
const sortedByName = sortData(searchedRows, "user.name", "asc");
|
|
83
100
|
```
|
|
84
101
|
|
|
85
|
-
|
|
86
|
-
|
|
102
|
+
### Sort by number
|
|
103
|
+
|
|
104
|
+
```ts
|
|
105
|
+
const sortedByAge = sortData(searchedRows, "user.profile.age", "desc");
|
|
106
|
+
```
|
|
87
107
|
|
|
88
108
|
---
|
|
89
109
|
|
|
90
|
-
##
|
|
110
|
+
## 7️⃣ Paginate (page data)
|
|
91
111
|
|
|
92
112
|
```ts
|
|
93
|
-
paginate(
|
|
113
|
+
const result = paginate(sortedByAge, page, pageSize);
|
|
94
114
|
```
|
|
95
115
|
|
|
96
|
-
|
|
116
|
+
Use:
|
|
97
117
|
|
|
98
118
|
```ts
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
total: number,
|
|
102
|
-
page: number,
|
|
103
|
-
limit: number
|
|
104
|
-
}
|
|
119
|
+
result.data // rows for current page
|
|
120
|
+
result.total // total rows
|
|
105
121
|
```
|
|
106
122
|
|
|
107
123
|
---
|
|
108
124
|
|
|
109
|
-
##
|
|
125
|
+
## 8️⃣ Full usage flow (MOST IMPORTANT)
|
|
126
|
+
|
|
127
|
+
This is the **correct order** and **most common usage**.
|
|
110
128
|
|
|
111
129
|
```ts
|
|
112
130
|
let rows = users;
|
|
113
131
|
|
|
114
|
-
|
|
132
|
+
// 1. Search
|
|
133
|
+
rows = filterData(rows, searchText, [
|
|
134
|
+
"user.name",
|
|
135
|
+
"email"
|
|
136
|
+
]);
|
|
137
|
+
|
|
138
|
+
// 2. Sort
|
|
115
139
|
rows = sortData(rows, sortKey, sortOrder);
|
|
140
|
+
|
|
141
|
+
// 3. Paginate
|
|
116
142
|
const result = paginate(rows, page, limit);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
You render:
|
|
146
|
+
|
|
147
|
+
```ts
|
|
148
|
+
result.data
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## 9️⃣ Example: Plain JavaScript table
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
const searchText = "ali";
|
|
157
|
+
const sortKey = "user.name";
|
|
158
|
+
const sortOrder = "asc";
|
|
159
|
+
const page = 1;
|
|
160
|
+
const limit = 10;
|
|
161
|
+
|
|
162
|
+
let rows = users;
|
|
163
|
+
|
|
164
|
+
rows = filterData(rows, searchText, ["user.name", "email"]);
|
|
165
|
+
rows = sortData(rows, sortKey, sortOrder);
|
|
166
|
+
const table = paginate(rows, page, limit);
|
|
167
|
+
|
|
168
|
+
console.log(table.data);
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## 🔟 Example: Angular component
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
this.rows = paginate(
|
|
177
|
+
sortData(
|
|
178
|
+
filterData(this.users, this.searchText, ["user.name", "email"]),
|
|
179
|
+
this.sortKey,
|
|
180
|
+
this.sortOrder
|
|
181
|
+
),
|
|
182
|
+
this.page,
|
|
183
|
+
10
|
|
184
|
+
).data;
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
---
|
|
117
188
|
|
|
118
|
-
|
|
189
|
+
## 1️⃣1️⃣ Example: React component
|
|
190
|
+
|
|
191
|
+
```ts
|
|
192
|
+
const filtered = filterData(data, search, ["user.name", "email"]);
|
|
193
|
+
const sorted = sortData(filtered, sortKey, order);
|
|
194
|
+
const pageData = paginate(sorted, page, 10).data;
|
|
119
195
|
```
|
|
120
196
|
|
|
121
197
|
---
|
|
122
198
|
|
|
123
|
-
##
|
|
199
|
+
## 1️⃣2️⃣ Common mistakes (avoid these)
|
|
200
|
+
|
|
201
|
+
❌ Sorting before filtering
|
|
202
|
+
❌ Mutating original array
|
|
203
|
+
❌ Expecting UI components
|
|
204
|
+
❌ Passing wrong nested keys
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
## 1️⃣3️⃣ When to use this library
|
|
209
|
+
|
|
210
|
+
Use `headless-table-core` when:
|
|
211
|
+
|
|
212
|
+
* You have more than one table
|
|
213
|
+
* API responses are nested
|
|
214
|
+
* You want consistent behavior
|
|
215
|
+
* You want framework-agnostic logic
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
## 1️⃣4️⃣ When NOT to use it
|
|
220
|
+
|
|
221
|
+
* One-off small table
|
|
222
|
+
* No search/sort/pagination needed
|
|
223
|
+
|
|
224
|
+
---
|
|
225
|
+
|
|
226
|
+
## Summary (TL;DR)
|
|
227
|
+
|
|
228
|
+
1. Install
|
|
229
|
+
2. Import
|
|
230
|
+
3. Filter → Sort → Paginate
|
|
231
|
+
4. Render result.data
|
|
124
232
|
|
|
125
|
-
|
|
126
|
-
* You control UI and rendering
|
|
127
|
-
* Original data is never mutated
|
|
233
|
+
That’s it.
|