2020-10-06 17:46:23 +02:00
|
|
|
<script>
|
2020-10-13 10:58:08 +02:00
|
|
|
import { Label, Multiselect } from "@budibase/bbui"
|
2020-10-06 17:46:23 +02:00
|
|
|
import api from "./api"
|
|
|
|
import { capitalise } from "./helpers"
|
|
|
|
|
|
|
|
export let schema = {}
|
2020-10-09 20:10:28 +02:00
|
|
|
export let linkedRows = []
|
2020-10-06 17:46:23 +02:00
|
|
|
export let showLabel = true
|
|
|
|
export let secondary
|
|
|
|
|
2020-10-09 19:49:23 +02:00
|
|
|
let linkedTable
|
2020-10-13 10:58:08 +02:00
|
|
|
let allRows = []
|
2020-10-06 17:46:23 +02:00
|
|
|
|
|
|
|
$: label = capitalise(schema.name)
|
2020-10-09 19:49:23 +02:00
|
|
|
$: linkedTableId = schema.tableId
|
2020-10-13 10:58:08 +02:00
|
|
|
$: fetchRows(linkedTableId)
|
2020-10-09 19:49:23 +02:00
|
|
|
$: fetchTable(linkedTableId)
|
2020-10-06 17:46:23 +02:00
|
|
|
|
2020-10-09 19:49:23 +02:00
|
|
|
async function fetchTable() {
|
|
|
|
if (linkedTableId == null) {
|
2020-10-06 17:46:23 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-09 19:49:23 +02:00
|
|
|
const FETCH_TABLE_URL = `/api/tables/${linkedTableId}`
|
|
|
|
const response = await api.get(FETCH_TABLE_URL)
|
|
|
|
linkedTable = await response.json()
|
2020-10-06 17:46:23 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
async function fetchRows(linkedTableId) {
|
2020-10-09 19:49:23 +02:00
|
|
|
if (linkedTableId == null) {
|
2020-10-06 17:46:23 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-09 20:10:28 +02:00
|
|
|
const FETCH_ROWS_URL = `/api/${linkedTableId}/rows`
|
|
|
|
const response = await api.get(FETCH_ROWS_URL)
|
2020-10-13 10:58:08 +02:00
|
|
|
allRows = await response.json()
|
2020-10-06 17:46:23 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 20:10:28 +02:00
|
|
|
function getPrettyName(row) {
|
|
|
|
return row[(linkedTable && linkedTable.primaryDisplay) || "_id"]
|
2020-10-06 17:46:23 +02:00
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
2020-10-09 19:49:23 +02:00
|
|
|
{#if linkedTable != null}
|
|
|
|
{#if linkedTable.primaryDisplay == null}
|
2020-10-06 17:46:23 +02:00
|
|
|
{#if showLabel}
|
|
|
|
<Label extraSmall grey>{label}</Label>
|
|
|
|
{/if}
|
|
|
|
<Label small black>
|
2020-10-14 21:38:32 +02:00
|
|
|
Please choose a display column for the
|
2020-10-09 19:49:23 +02:00
|
|
|
<b>{linkedTable.name}</b>
|
2020-10-06 17:46:23 +02:00
|
|
|
table.
|
|
|
|
</Label>
|
|
|
|
{:else}
|
2020-10-13 10:58:08 +02:00
|
|
|
<Multiselect
|
|
|
|
{secondary}
|
|
|
|
bind:value={linkedRows}
|
|
|
|
label={showLabel ? label : null}
|
|
|
|
placeholder="Choose some options">
|
|
|
|
{#each allRows as row}
|
|
|
|
<option value={row._id}>{getPrettyName(row)}</option>
|
|
|
|
{/each}
|
|
|
|
</Multiselect>
|
2020-10-06 17:46:23 +02:00
|
|
|
{/if}
|
|
|
|
{/if}
|