|
56 | 56 | pageSizes = [5, 10, 20, 50, 100], // Page sizes to display in the pagination component
|
57 | 57 | fitToScreen = true, // Whether to fit the table to the screen,
|
58 | 58 | exportable = false, // Whether to display the export button and enable export functionality
|
59 |
| - serverSide = false, // Whether the table is client or server-side |
60 |
| - URL = '', // URL to fetch data from |
61 |
| - token = '', // Bearer token to authenticate the request |
62 |
| - sendModel = new Send(), // Model to send requests |
63 |
| - entityId = 0, // Entity ID to send with the request |
64 |
| - versionId = 0 // Version ID to send with the request, |
| 59 | + server |
65 | 60 | } = config;
|
66 | 61 |
|
67 | 62 | let searchValue = '';
|
68 | 63 | let isFetching = false;
|
| 64 | + const serverSide = server !== undefined; |
| 65 | + const { baseUrl, sendModel, entityId, versionId } = server ?? {}; |
69 | 66 |
|
70 | 67 | const filters = writable<{
|
71 | 68 | [key: string]: { [key in FilterOptionsEnum]?: number | string | Date };
|
|
305 | 302 | const { hiddenColumnIds } = pluginStates.hideColumns;
|
306 | 303 |
|
307 | 304 | const updateTable = async () => {
|
| 305 | + if (!sendModel) throw new Error('Server-side configuration is missing'); |
| 306 | +
|
308 | 307 | sendModel.limit = $pageSize;
|
309 | 308 | sendModel.offset = $pageSize * $pageIndex;
|
310 |
| - sendModel.version = versionId; |
311 |
| - sendModel.id = entityId; |
| 309 | + sendModel.version = versionId || -1; |
| 310 | + sendModel.id = entityId || -1; |
312 | 311 | sendModel.filter = normalizeFilters($filters);
|
313 | 312 |
|
314 | 313 | let fetchData;
|
315 | 314 |
|
316 | 315 | try {
|
317 | 316 | isFetching = true;
|
318 |
| - fetchData = await fetch(URL, { |
| 317 | + fetchData = await fetch(baseUrl || '', { |
319 | 318 | headers: {
|
320 |
| - 'Content-Type': 'application/json', |
321 |
| - Authorization: `Bearer ${token}` |
| 319 | + 'Content-Type': 'application/json' |
322 | 320 | },
|
323 | 321 | method: 'POST',
|
324 | 322 | body: JSON.stringify(sendModel)
|
|
363 | 361 | };
|
364 | 362 |
|
365 | 363 | const sortServer = (order: 'asc' | 'desc' | undefined, id: string) => {
|
| 364 | + if (!sendModel) throw new Error('Server-side configuration is missing'); |
366 | 365 | // Set parameter for sorting
|
367 | 366 | if (order === undefined) {
|
368 | 367 | sendModel.order = [];
|
|
386 | 385 | {#if $data.length > 0 || (columns && Object.keys(columns).length > 0)}
|
387 | 386 | <div class="table-container">
|
388 | 387 | <!-- Enable the search filter if table is not empty -->
|
389 |
| - {#if !serverSide && search} |
| 388 | + {#if search} |
390 | 389 | <form
|
391 | 390 | class="flex gap-2"
|
392 | 391 | on:submit|preventDefault={() => {
|
| 392 | + if (!sendModel) throw new Error('Server-side configuration is missing'); |
| 393 | + |
393 | 394 | sendModel.q = searchValue;
|
394 | 395 | $filterValue = searchValue;
|
395 | 396 | }}
|
|
406 | 407 | id="{tableId}-searchReset"
|
407 | 408 | class="absolute right-3 items-center"
|
408 | 409 | on:click|preventDefault={() => {
|
| 410 | + if (!sendModel) throw new Error('Server-side configuration is missing'); |
| 411 | + |
409 | 412 | searchValue = '';
|
410 | 413 | sendModel.q = '';
|
411 | 414 | $filterValue = '';
|
|
417 | 420 | id="{tableId}-searchSubmit"
|
418 | 421 | class="btn variant-filled-primary"
|
419 | 422 | on:click|preventDefault={() => {
|
| 423 | + if (!sendModel) throw new Error('Server-side configuration is missing'); |
| 424 | + |
420 | 425 | $filterValue = searchValue;
|
421 | 426 | sendModel.q = searchValue;
|
422 | 427 | }}>Search</button
|
|
0 commit comments