2021-02-01 22:02:54 +01:00
|
|
|
<script>
|
|
|
|
import { getContext } from "svelte"
|
2021-02-06 13:31:12 +01:00
|
|
|
import {
|
|
|
|
Button,
|
|
|
|
DatePicker,
|
|
|
|
Label,
|
|
|
|
Select,
|
|
|
|
Toggle,
|
|
|
|
Input,
|
|
|
|
} from "@budibase/bbui"
|
2021-02-01 22:02:54 +01:00
|
|
|
|
2021-02-11 10:30:05 +01:00
|
|
|
const { API, styleable, Provider, builderStore, ActionTypes } = getContext(
|
|
|
|
"sdk"
|
|
|
|
)
|
2021-02-01 22:02:54 +01:00
|
|
|
const component = getContext("component")
|
|
|
|
|
2021-02-11 10:30:05 +01:00
|
|
|
export let table
|
2021-02-05 00:17:49 +01:00
|
|
|
export let columns = []
|
2021-02-10 15:58:29 +01:00
|
|
|
export let pageSize
|
2021-02-10 15:24:42 +01:00
|
|
|
export let noRowsMessage
|
2021-02-01 22:02:54 +01:00
|
|
|
|
|
|
|
let rows = []
|
|
|
|
let loaded = false
|
|
|
|
let search = {}
|
2021-02-05 00:17:49 +01:00
|
|
|
let tableDefinition
|
2021-02-08 18:44:44 +01:00
|
|
|
let schema
|
2021-02-01 22:02:54 +01:00
|
|
|
|
2021-02-08 18:44:44 +01:00
|
|
|
// pagination
|
2021-02-09 13:12:03 +01:00
|
|
|
let page = 0
|
2021-02-08 18:44:44 +01:00
|
|
|
|
2021-02-09 13:12:03 +01:00
|
|
|
$: fetchData(table, page)
|
2021-02-08 18:44:44 +01:00
|
|
|
// omit empty strings
|
2021-02-09 19:49:12 +01:00
|
|
|
$: parsedSearch = Object.keys(search).reduce(
|
|
|
|
(acc, next) =>
|
|
|
|
search[next] === "" ? acc : { ...acc, [next]: search[next] },
|
|
|
|
{}
|
|
|
|
)
|
2021-02-11 10:30:05 +01:00
|
|
|
$: actions = [
|
|
|
|
{
|
|
|
|
type: ActionTypes.RefreshDatasource,
|
|
|
|
callback: () => fetchData(table, page),
|
|
|
|
metadata: { datasource: { type: "table", tableId: table } },
|
|
|
|
},
|
|
|
|
]
|
2021-02-01 22:02:54 +01:00
|
|
|
|
2021-02-09 13:12:03 +01:00
|
|
|
async function fetchData(table, page) {
|
2021-02-11 10:30:05 +01:00
|
|
|
if (table) {
|
2021-02-05 00:17:49 +01:00
|
|
|
const tableDef = await API.fetchTableDefinition(table)
|
|
|
|
schema = tableDef.schema
|
2021-02-10 20:23:53 +01:00
|
|
|
rows = await API.searchTableData({
|
2021-02-05 00:17:49 +01:00
|
|
|
tableId: table,
|
2021-02-08 18:44:44 +01:00
|
|
|
search: parsedSearch,
|
|
|
|
pagination: {
|
|
|
|
pageSize,
|
2021-02-09 19:49:12 +01:00
|
|
|
page,
|
|
|
|
},
|
2021-02-01 22:02:54 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
loaded = true
|
|
|
|
}
|
2021-02-06 13:31:12 +01:00
|
|
|
|
2021-02-08 18:44:44 +01:00
|
|
|
function nextPage() {
|
2021-02-09 13:12:03 +01:00
|
|
|
page += 1
|
2021-02-08 18:44:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function previousPage() {
|
2021-02-09 13:12:03 +01:00
|
|
|
page -= 1
|
2021-02-08 18:44:44 +01:00
|
|
|
}
|
2021-02-01 22:02:54 +01:00
|
|
|
</script>
|
|
|
|
|
2021-02-11 10:30:05 +01:00
|
|
|
<Provider {actions}>
|
|
|
|
<div use:styleable={$component.styles}>
|
|
|
|
<div class="query-builder">
|
|
|
|
{#if schema}
|
|
|
|
{#each columns as field}
|
|
|
|
<div class="form-field">
|
|
|
|
<Label extraSmall grey>{schema[field].name}</Label>
|
|
|
|
{#if schema[field].type === 'options'}
|
|
|
|
<Select secondary bind:value={search[field]}>
|
|
|
|
<option value="">Choose an option</option>
|
|
|
|
{#each schema[field].constraints.inclusion as opt}
|
|
|
|
<option>{opt}</option>
|
|
|
|
{/each}
|
|
|
|
</Select>
|
|
|
|
{:else if schema[field].type === 'datetime'}
|
|
|
|
<DatePicker bind:value={search[field]} />
|
|
|
|
{:else if schema[field].type === 'boolean'}
|
|
|
|
<Toggle text={schema[field].name} bind:checked={search[field]} />
|
|
|
|
{:else if schema[field].type === 'number'}
|
|
|
|
<Input type="number" bind:value={search[field]} />
|
|
|
|
{:else if schema[field].type === 'string'}
|
|
|
|
<Input bind:value={search[field]} />
|
|
|
|
{/if}
|
|
|
|
</div>
|
2021-02-10 15:24:42 +01:00
|
|
|
{/each}
|
|
|
|
{/if}
|
2021-02-11 10:30:05 +01:00
|
|
|
<div class="actions">
|
|
|
|
<Button
|
|
|
|
secondary
|
|
|
|
on:click={() => {
|
|
|
|
search = {}
|
|
|
|
page = 0
|
|
|
|
}}>
|
|
|
|
Reset
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
primary
|
|
|
|
on:click={() => {
|
|
|
|
page = 0
|
|
|
|
fetchData(table, page)
|
|
|
|
}}>
|
|
|
|
Search
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{#if loaded}
|
|
|
|
{#if rows.length > 0}
|
|
|
|
{#if $component.children === 0 && $builderStore.inBuilder}
|
|
|
|
<p><i class="ri-image-line" />Add some components to display.</p>
|
|
|
|
{:else}
|
|
|
|
{#each rows as row}
|
|
|
|
<Provider data={row}>
|
|
|
|
<slot />
|
|
|
|
</Provider>
|
|
|
|
{/each}
|
|
|
|
{/if}
|
|
|
|
{:else if noRowsMessage}
|
|
|
|
<p><i class="ri-search-2-line" />{noRowsMessage}</p>
|
|
|
|
{/if}
|
2021-02-06 13:31:12 +01:00
|
|
|
{/if}
|
2021-02-11 10:30:05 +01:00
|
|
|
<div class="pagination">
|
|
|
|
{#if page > 0}
|
|
|
|
<Button primary on:click={previousPage}>Back</Button>
|
|
|
|
{/if}
|
|
|
|
{#if rows.length === pageSize}
|
|
|
|
<Button primary on:click={nextPage}>Next</Button>
|
|
|
|
{/if}
|
|
|
|
</div>
|
2021-02-06 13:31:12 +01:00
|
|
|
</div>
|
2021-02-11 10:30:05 +01:00
|
|
|
</Provider>
|
2021-02-01 22:02:54 +01:00
|
|
|
|
|
|
|
<style>
|
|
|
|
p {
|
2021-02-10 20:23:53 +01:00
|
|
|
margin: 0 var(--spacing-m);
|
|
|
|
background-color: var(--grey-2);
|
|
|
|
color: var(--grey-6);
|
|
|
|
font-size: var(--font-size-s);
|
|
|
|
padding: var(--spacing-l);
|
|
|
|
border-radius: var(--border-radius-s);
|
2021-02-01 22:02:54 +01:00
|
|
|
display: grid;
|
|
|
|
place-items: center;
|
2021-02-10 20:23:53 +01:00
|
|
|
}
|
|
|
|
p i {
|
|
|
|
margin-bottom: var(--spacing-m);
|
|
|
|
font-size: 1.5rem;
|
|
|
|
color: var(--grey-5);
|
2021-02-01 22:02:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
.query-builder {
|
|
|
|
padding: var(--spacing-m);
|
|
|
|
border-radius: var(--border-radius-s);
|
|
|
|
}
|
|
|
|
|
2021-02-08 18:44:44 +01:00
|
|
|
.actions {
|
|
|
|
display: grid;
|
|
|
|
grid-gap: var(--spacing-s);
|
|
|
|
justify-content: flex-end;
|
|
|
|
grid-auto-flow: column;
|
|
|
|
}
|
|
|
|
|
2021-02-01 22:02:54 +01:00
|
|
|
.form-field {
|
|
|
|
margin-bottom: var(--spacing-m);
|
|
|
|
}
|
2021-02-06 13:31:12 +01:00
|
|
|
|
|
|
|
.pagination {
|
2021-02-09 13:12:03 +01:00
|
|
|
display: grid;
|
|
|
|
grid-gap: var(--spacing-s);
|
2021-02-06 13:31:12 +01:00
|
|
|
justify-content: flex-end;
|
|
|
|
margin-top: var(--spacing-m);
|
2021-02-09 13:12:03 +01:00
|
|
|
grid-auto-flow: column;
|
2021-02-06 13:31:12 +01:00
|
|
|
}
|
2021-02-01 22:02:54 +01:00
|
|
|
</style>
|