|
1 | 1 | <script lang="ts"> |
2 | 2 | import type { Action } from './table-types'; |
3 | | - import { get } from 'svelte/store'; |
4 | 3 | import Button from './Button.svelte'; |
5 | 4 | import Select from './Select.svelte'; |
6 | 5 | import { TableDataProvider } from './table-abstract-provider'; |
7 | 6 | import TableActions from './TableActions.svelte'; |
| 7 | + import { noop } from '../../utils/misc'; |
8 | 8 |
|
9 | 9 | export let provider: TableDataProvider; |
10 | 10 | export let actions: Action[]; |
11 | 11 | export let selected: number[]; |
12 | 12 |
|
13 | | - $: total = provider.total; |
| 13 | + // $: total = provider.total; |
14 | 14 | $: itemsPerPage = provider.options.itemsPerPage; |
15 | 15 |
|
16 | 16 | let page = 1; |
| 17 | + let numPages = 1; |
| 18 | + let start = 0; |
| 19 | + let end = 0; |
| 20 | + let total = 0; |
| 21 | + let unsub = noop; |
17 | 22 |
|
18 | | - $: numPages = Math.ceil($total / itemsPerPage); |
19 | | - $: start = (page - 1) * itemsPerPage + 1; |
20 | | - $: end = Math.min($total, page * itemsPerPage); |
| 23 | + $: { |
| 24 | + unsub(); |
| 25 | + unsub = provider.total.subscribe((t) => { |
| 26 | + if (t === undefined || t === 0) { |
| 27 | + numPages = 1; |
| 28 | + start = 0; |
| 29 | + end = 0; |
| 30 | + total = 0; |
| 31 | + } else { |
| 32 | + numPages = Math.ceil(total / itemsPerPage); |
| 33 | + start = (page - 1) * itemsPerPage + 1; |
| 34 | + end = Math.min(total || 0, page * itemsPerPage); |
| 35 | + total = t; |
| 36 | + } |
| 37 | + }); |
| 38 | + } |
| 39 | +
|
| 40 | + // $: console.log('provider', provider); |
| 41 | + // $: console.log('total', total); |
| 42 | + // $: console.log('$total', $total); |
| 43 | + // $: numPages = $total ? Math.ceil($total / itemsPerPage) : 1; |
| 44 | + // $: start = $total ? (page - 1) * itemsPerPage + 1 : 0; |
| 45 | + // $: end = Math.min($total || 0, page * itemsPerPage); |
21 | 46 |
|
22 | 47 | function gotoPage(i: number): void { |
23 | 48 | page = i; |
|
41 | 66 | options={['10', '20', '50', 'all']} |
42 | 67 | value={itemsPerPage.toString()} |
43 | 68 | on:change={({ detail }) => { |
44 | | - const n = detail === 'all' ? get(total) : parseInt(detail); |
| 69 | + const n = detail === 'all' ? total : parseInt(detail); |
45 | 70 | provider.paginate(n); |
46 | 71 | itemsPerPage = n; |
47 | 72 | }} |
48 | 73 | /> |
49 | 74 | </div> |
50 | 75 | </div> |
51 | 76 | <div class="mx-3"> |
52 | | - {start}-{end} of {$total} |
| 77 | + {start}-{end} of {total} |
53 | 78 | </div> |
54 | 79 | <Button |
55 | 80 | round |
|
0 commit comments