Always allow selecting rows in grids in apps, and add binding for grid selected rows
This commit is contained in:
parent
0b36dc8567
commit
e1a9762d21
|
@ -6739,10 +6739,22 @@
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"context": {
|
"context": [
|
||||||
|
{
|
||||||
"type": "schema",
|
"type": "schema",
|
||||||
"scope": "local"
|
"scope": "local"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "static",
|
||||||
|
"values": [
|
||||||
|
{
|
||||||
|
"label": "Selected rows",
|
||||||
|
"key": "selectedRows",
|
||||||
|
"type": "array"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"actions": ["RefreshDatasource"]
|
"actions": ["RefreshDatasource"]
|
||||||
},
|
},
|
||||||
"bbreferencefield": {
|
"bbreferencefield": {
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
<script>
|
<script>
|
||||||
// NOTE: this is not a block - it's just named as such to avoid confusing users,
|
// NOTE: this is not a block - it's just named as such to avoid confusing users,
|
||||||
// because it functions similarly to one
|
// because it functions similarly to one
|
||||||
import { getContext } from "svelte"
|
import { getContext, onMount } from "svelte"
|
||||||
import { get } from "svelte/store"
|
import { get, derived, readable } from "svelte/store"
|
||||||
import { Grid } from "@budibase/frontend-core"
|
import { Grid } from "@budibase/frontend-core"
|
||||||
|
|
||||||
// table is actually any datasource, but called table for legacy compatibility
|
// table is actually any datasource, but called table for legacy compatibility
|
||||||
|
@ -34,6 +34,7 @@
|
||||||
} = getContext("sdk")
|
} = getContext("sdk")
|
||||||
|
|
||||||
let grid
|
let grid
|
||||||
|
let gridContext
|
||||||
|
|
||||||
$: columnWhitelist = parsedColumns
|
$: columnWhitelist = parsedColumns
|
||||||
?.filter(col => col.active)
|
?.filter(col => col.active)
|
||||||
|
@ -41,10 +42,12 @@
|
||||||
$: schemaOverrides = getSchemaOverrides(parsedColumns)
|
$: schemaOverrides = getSchemaOverrides(parsedColumns)
|
||||||
$: enrichedButtons = enrichButtons(buttons)
|
$: enrichedButtons = enrichButtons(buttons)
|
||||||
$: parsedColumns = getParsedColumns(columns)
|
$: parsedColumns = getParsedColumns(columns)
|
||||||
|
$: selectedRows = deriveSelectedRows(gridContext)
|
||||||
|
$: data = { selectedRows: $selectedRows }
|
||||||
$: actions = [
|
$: actions = [
|
||||||
{
|
{
|
||||||
type: ActionTypes.RefreshDatasource,
|
type: ActionTypes.RefreshDatasource,
|
||||||
callback: () => grid?.getContext()?.rows.actions.refreshData(),
|
callback: () => gridContext?.rows.actions.refreshData(),
|
||||||
metadata: { dataSource: table },
|
metadata: { dataSource: table },
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
@ -104,13 +107,35 @@
|
||||||
},
|
},
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const deriveSelectedRows = gridContext => {
|
||||||
|
if (!gridContext) {
|
||||||
|
return readable([])
|
||||||
|
}
|
||||||
|
return derived(
|
||||||
|
[gridContext.selectedRows, gridContext.rowLookupMap, gridContext.rows],
|
||||||
|
([$selectedRows, $rowLookupMap, $rows]) => {
|
||||||
|
const rowIds = Object.entries($selectedRows || {})
|
||||||
|
.filter(([_, selected]) => selected)
|
||||||
|
.map(([rowId]) => rowId)
|
||||||
|
return rowIds.map(id => {
|
||||||
|
const idx = $rowLookupMap[id]
|
||||||
|
return gridContext.rows.actions.cleanRow($rows[idx])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
gridContext = grid.getContext()
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
use:styleable={$component.styles}
|
use:styleable={$component.styles}
|
||||||
class:in-builder={$builderStore.inBuilder}
|
class:in-builder={$builderStore.inBuilder}
|
||||||
>
|
>
|
||||||
<Provider {actions}>
|
<Provider {data} {actions}>
|
||||||
<Grid
|
<Grid
|
||||||
bind:this={grid}
|
bind:this={grid}
|
||||||
datasource={table}
|
datasource={table}
|
||||||
|
@ -128,6 +153,7 @@
|
||||||
canEditColumns={false}
|
canEditColumns={false}
|
||||||
canExpandRows={false}
|
canExpandRows={false}
|
||||||
canSaveSchema={false}
|
canSaveSchema={false}
|
||||||
|
canSelectRows={true}
|
||||||
showControls={false}
|
showControls={false}
|
||||||
notifySuccess={notificationStore.actions.success}
|
notifySuccess={notificationStore.actions.success}
|
||||||
notifyError={notificationStore.actions.error}
|
notifyError={notificationStore.actions.error}
|
||||||
|
|
|
@ -335,6 +335,7 @@ const exportDataHandler = async action => {
|
||||||
let selection = rowSelectionStore.actions.getSelection(
|
let selection = rowSelectionStore.actions.getSelection(
|
||||||
action.parameters.tableComponentId
|
action.parameters.tableComponentId
|
||||||
)
|
)
|
||||||
|
console.log(selection)
|
||||||
if (selection.selectedRows && selection.selectedRows.length > 0) {
|
if (selection.selectedRows && selection.selectedRows.length > 0) {
|
||||||
try {
|
try {
|
||||||
const data = await API.exportRows({
|
const data = await API.exportRows({
|
||||||
|
|
|
@ -16,6 +16,8 @@
|
||||||
const { config, dispatch, selectedRows } = getContext("grid")
|
const { config, dispatch, selectedRows } = getContext("grid")
|
||||||
const svelteDispatch = createEventDispatcher()
|
const svelteDispatch = createEventDispatcher()
|
||||||
|
|
||||||
|
$: selectionEnabled = $config.canSelectRows || $config.canDeleteRows
|
||||||
|
|
||||||
const select = e => {
|
const select = e => {
|
||||||
e.stopPropagation()
|
e.stopPropagation()
|
||||||
svelteDispatch("select")
|
svelteDispatch("select")
|
||||||
|
@ -52,7 +54,7 @@
|
||||||
<div
|
<div
|
||||||
on:click={select}
|
on:click={select}
|
||||||
class="checkbox"
|
class="checkbox"
|
||||||
class:visible={$config.canDeleteRows &&
|
class:visible={selectionEnabled &&
|
||||||
(disableNumber || rowSelected || rowHovered || rowFocused)}
|
(disableNumber || rowSelected || rowHovered || rowFocused)}
|
||||||
>
|
>
|
||||||
<Checkbox value={rowSelected} {disabled} />
|
<Checkbox value={rowSelected} {disabled} />
|
||||||
|
@ -60,7 +62,7 @@
|
||||||
{#if !disableNumber}
|
{#if !disableNumber}
|
||||||
<div
|
<div
|
||||||
class="number"
|
class="number"
|
||||||
class:visible={!$config.canDeleteRows ||
|
class:visible={!selectionEnabled ||
|
||||||
!(rowSelected || rowHovered || rowFocused)}
|
!(rowSelected || rowHovered || rowFocused)}
|
||||||
>
|
>
|
||||||
{row.__idx + 1}
|
{row.__idx + 1}
|
||||||
|
@ -117,19 +119,11 @@
|
||||||
.expand {
|
.expand {
|
||||||
margin-right: 4px;
|
margin-right: 4px;
|
||||||
}
|
}
|
||||||
.expand {
|
.expand:not(.visible),
|
||||||
|
.expand:not(.visible) :global(*) {
|
||||||
opacity: 0;
|
opacity: 0;
|
||||||
|
pointer-events: none !important;
|
||||||
}
|
}
|
||||||
.expand :global(.spectrum-Icon) {
|
|
||||||
pointer-events: none;
|
|
||||||
}
|
|
||||||
.expand.visible {
|
|
||||||
opacity: 1;
|
|
||||||
}
|
|
||||||
.expand.visible :global(.spectrum-Icon) {
|
|
||||||
pointer-events: all;
|
|
||||||
}
|
|
||||||
|
|
||||||
.delete:hover {
|
.delete:hover {
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@
|
||||||
export let canDeleteRows = true
|
export let canDeleteRows = true
|
||||||
export let canEditColumns = true
|
export let canEditColumns = true
|
||||||
export let canSaveSchema = true
|
export let canSaveSchema = true
|
||||||
|
export let canSelectRows = false
|
||||||
export let stripeRows = false
|
export let stripeRows = false
|
||||||
export let collaboration = true
|
export let collaboration = true
|
||||||
export let showAvatars = true
|
export let showAvatars = true
|
||||||
|
@ -90,6 +91,7 @@
|
||||||
canDeleteRows,
|
canDeleteRows,
|
||||||
canEditColumns,
|
canEditColumns,
|
||||||
canSaveSchema,
|
canSaveSchema,
|
||||||
|
canSelectRows,
|
||||||
stripeRows,
|
stripeRows,
|
||||||
collaboration,
|
collaboration,
|
||||||
showAvatars,
|
showAvatars,
|
||||||
|
|
Loading…
Reference in New Issue