budibase/packages/builder/src/components/backend/DataTable/DataTable.svelte

166 lines
4.5 KiB
Svelte
Raw Normal View History

2020-03-10 14:53:23 +01:00
<script>
import { fade } from "svelte/transition"
import { tables } from "stores/backend"
import CreateRowButton from "./buttons/CreateRowButton.svelte"
import CreateColumnButton from "./buttons/CreateColumnButton.svelte"
import CreateViewButton from "./buttons/CreateViewButton.svelte"
import ExistingRelationshipButton from "./buttons/ExistingRelationshipButton.svelte"
import ExportButton from "./buttons/ExportButton.svelte"
import ImportButton from "./buttons/ImportButton.svelte"
2020-12-04 09:27:42 +01:00
import EditRolesButton from "./buttons/EditRolesButton.svelte"
2021-02-10 19:18:31 +01:00
import ManageAccessButton from "./buttons/ManageAccessButton.svelte"
2021-02-16 14:56:40 +01:00
import HideAutocolumnButton from "./buttons/HideAutocolumnButton.svelte"
import TableFilterButton from "./buttons/TableFilterButton.svelte"
import Table from "./Table.svelte"
2020-12-04 09:27:42 +01:00
import { TableNames } from "constants"
import CreateEditRow from "./modals/CreateEditRow.svelte"
import { Pagination } from "@budibase/bbui"
import { fetchData } from "@budibase/frontend-core"
import { API } from "api"
2020-03-10 17:06:30 +01:00
let hideAutocolumns = true
$: isUsersTable = $tables.selected?._id === TableNames.USERS
$: type = $tables.selected?.type
$: isInternal = type !== "external"
2021-10-18 17:03:13 +02:00
$: schema = $tables.selected?.schema
$: enrichedSchema = enrichSchema($tables.selected?.schema)
$: id = $tables.selected?._id
$: fetch = createFetch(id)
2021-10-13 12:39:36 +02:00
2021-10-18 17:03:13 +02:00
const enrichSchema = schema => {
let tempSchema = { ...schema }
tempSchema._id = {
2021-10-13 12:39:36 +02:00
type: "internal",
editable: false,
displayName: "ID",
autocolumn: true,
}
if (isInternal) {
2021-10-18 17:03:13 +02:00
tempSchema._rev = {
2021-10-13 12:39:36 +02:00
type: "internal",
editable: false,
displayName: "Revision",
autocolumn: true,
}
}
2021-10-18 17:03:13 +02:00
return tempSchema
}
// Fetches new data whenever the table changes
const createFetch = tableId => {
return fetchData({
API,
datasource: {
tableId,
type: "table",
},
options: {
schema,
limit: 10,
paginate: true,
},
})
}
// Fetch data whenever sorting option changes
const onSort = e => {
fetch.update({
sortColumn: e.detail.column,
sortOrder: e.detail.order,
})
}
// Fetch data whenever filters change
const onFilter = e => {
fetch.update({
filter: e.detail,
})
}
// Fetch data whenever schema changes
const onUpdateColumns = () => {
fetch.refresh()
}
// Fetch data whenever rows are modified. Unfortunately we have to lose
// our pagination place, as our bookmarks will have shifted.
const onUpdateRows = () => {
fetch.update()
}
2020-03-10 14:53:23 +01:00
</script>
<div>
<Table
title={$tables.selected?.name}
2021-10-18 17:03:13 +02:00
schema={enrichedSchema}
{type}
tableId={id}
data={$fetch.rows}
bind:hideAutocolumns
loading={$fetch.loading}
on:sort={onSort}
allowEditing
disableSorting
on:updatecolumns={onUpdateColumns}
on:updaterows={onUpdateRows}
>
2021-10-28 20:39:42 +02:00
<CreateColumnButton on:updatecolumns={onUpdateColumns} />
{#if schema && Object.keys(schema).length > 0}
{#if !isUsersTable}
<CreateRowButton
on:updaterows={onUpdateRows}
title={"Create row"}
modalContentComponent={CreateEditRow}
/>
{/if}
{#if isInternal}
<CreateViewButton />
{/if}
<ManageAccessButton resourceId={$tables.selected?._id} />
{#if isUsersTable}
<EditRolesButton />
{/if}
{#if !isInternal}
<ExistingRelationshipButton
table={$tables.selected}
on:updatecolumns={onUpdateColumns}
/>
{/if}
<HideAutocolumnButton bind:hideAutocolumns />
<!-- always have the export last -->
<ExportButton view={$tables.selected?._id} />
<ImportButton
tableId={$tables.selected?._id}
on:updaterows={onUpdateRows}
/>
{#key id}
<TableFilterButton {schema} on:change={onFilter} />
{/key}
{/if}
</Table>
{#key id}
<div in:fade={{ delay: 200, duration: 100 }}>
<div class="pagination">
<Pagination
page={$fetch.pageNumber + 1}
hasPrevPage={$fetch.hasPrevPage}
hasNextPage={$fetch.hasNextPage}
goToPrevPage={$fetch.loading ? null : fetch.prevPage}
goToNextPage={$fetch.loading ? null : fetch.nextPage}
/>
</div>
</div>
{/key}
</div>
<style>
.pagination {
display: flex;
flex-direction: row;
justify-content: flex-end;
align-items: center;
margin-top: var(--spacing-xl);
}
</style>