-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
236 lines (205 loc) · 6.03 KB
/
index.js
File metadata and controls
236 lines (205 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
* VanillaTable - Lightweight vanilla JavaScript table library
* Features: sorting, pagination, filtering, search, and export capabilities
*/
// Load all modules (in browser, these will be included via script tags)
// In Node.js/bundler environments, you would use actual imports
/**
* Main VanillaTable class
*/
class VanillaTable {
/**
* Create a new VanillaTable instance
* @param {string|HTMLElement} selector - CSS selector or DOM element for the table
* @param {Object} options - Configuration options
*/
constructor(selector, options = {}) {
// Find the table element
this.tableElement = typeof selector === 'string'
? document.querySelector(selector)
: selector;
if (!this.tableElement) {
throw new Error('Table element not found');
}
// Default options
this.options = {
data: [], // Array of row objects
columns: [], // Column configuration
pagination: false, // Enable pagination
perPage: 25, // Rows per page
paginationInfo: null, // Element or selector for pagination info
paginationControls: null, // Element or selector for pagination controls
sorting: true, // Enable sorting
search: false, // Enable search
searchInput: null, // Element or selector for search input
filters: {}, // Initial filters
zebra: true, // Enable zebra striping
noResultsText: 'No results found.',
...options
};
// Add vanilla-table class to the table
this.tableElement.classList.add('vanilla-table');
// Initialize core
this.core = new VanillaTableCore(this.tableElement, this.options);
// Initialize modules
this.renderer = new VanillaTableRenderer(this.core);
this.sorter = this.options.sorting ? new VanillaTableSorter(this.core, this.renderer) : null;
this.paginator = this.options.pagination ? new VanillaTablePaginator(this.core) : null;
this.filter = new VanillaTableFilter(this.core, this.renderer);
this.exporter = new VanillaTableExporter(this.core, this.renderer);
// Setup callbacks
if (this.sorter) {
this.core.onSort = () => {
if (this.paginator) this.paginator.update();
};
}
// Initialize search if enabled
if (this.options.search && this.options.searchInput) {
this.initSearch();
}
// Load initial data if provided
if (this.options.data && this.options.data.length > 0) {
this.loadData(this.options.data);
}
}
/**
* Load data into the table
* @param {Array} data - Array of row objects
*/
loadData(data) {
this.renderer.renderTable(data);
if (this.sorter) {
this.sorter.init();
}
if (this.paginator) {
this.paginator.update();
}
return this;
}
/**
* Initialize search functionality
*/
initSearch() {
const searchInput = typeof this.options.searchInput === 'string'
? document.querySelector(this.options.searchInput)
: this.options.searchInput;
if (!searchInput) return;
searchInput.addEventListener('input', (e) => {
this.search(e.target.value);
});
}
/**
* Search the table
* @param {string} query - Search query
*/
search(query) {
this.filter.applySearch(query);
if (this.paginator) {
this.core.state.currentPage = 1; // Reset to first page
this.paginator.update();
}
return this;
}
/**
* Apply filters to the table
* @param {Object} filters - Filter configuration
*/
applyFilters(filters) {
this.filter.applyFilters(filters);
if (this.paginator) {
this.core.state.currentPage = 1; // Reset to first page
this.paginator.update();
}
return this;
}
/**
* Clear all filters and search
*/
clearFilters() {
this.filter.clearAll();
if (this.paginator) {
this.paginator.update();
}
return this;
}
/**
* Go to a specific page
* @param {number} page - Page number
*/
goToPage(page) {
if (this.paginator) {
this.paginator.goToPage(page);
}
return this;
}
/**
* Set page size
* @param {number} size - Number of rows per page
*/
setPageSize(size) {
this.core.state.perPage = size;
if (this.paginator) {
this.paginator.update();
}
return this;
}
/**
* Export table data to CSV
* @param {string} filename - Output filename
* @param {boolean} includeFiltered - Include filtered rows
*/
exportCSV(filename = 'table.csv', includeFiltered = false) {
this.exporter.downloadCSV(filename, includeFiltered);
return this;
}
/**
* Export table data to JSON
* @param {string} filename - Output filename
* @param {boolean} includeFiltered - Include filtered rows
*/
exportJSON(filename = 'table.json', includeFiltered = false) {
this.exporter.downloadJSON(filename, includeFiltered);
return this;
}
/**
* Get current table data as CSV string
* @param {boolean} includeFiltered - Include filtered rows
*/
toCSV(includeFiltered = false) {
return this.exporter.toCSV(includeFiltered);
}
/**
* Get current table data as JSON string
* @param {boolean} includeFiltered - Include filtered rows
*/
toJSON(includeFiltered = false) {
return this.exporter.toJSON(includeFiltered);
}
/**
* Get visible data
*/
getVisibleData() {
return this.exporter.getVisibleData();
}
/**
* Get all data
*/
getAllData() {
return this.exporter.getAllData();
}
/**
* Destroy the table instance
*/
destroy() {
// Remove event listeners and clean up
this.tableElement.classList.remove('vanilla-table', 'vanilla-table-zebra', 'sorting-enabled');
// Note: In a full implementation, you'd want to track and remove all event listeners
}
}
// Export for different module systems
if (typeof module !== 'undefined' && module.exports) {
module.exports = VanillaTable;
}
if (typeof window !== 'undefined') {
window.VanillaTable = VanillaTable;
}