Add bulk cell delete featuer
This commit is contained in:
parent
06aa4ba09c
commit
37c0417848
|
@ -1,19 +1,35 @@
|
||||||
<script>
|
<script>
|
||||||
import { Modal, ModalContent } from "@budibase/bbui"
|
import { Modal, ModalContent } from "@budibase/bbui"
|
||||||
import { getContext, onMount } from "svelte"
|
import { getContext, onMount } from "svelte"
|
||||||
|
import { parseCellID } from "../lib/utils"
|
||||||
|
|
||||||
const { selectedRows, rows, subscribe, notifications, menu } =
|
const {
|
||||||
getContext("grid")
|
selectedRows,
|
||||||
|
rows,
|
||||||
|
subscribe,
|
||||||
|
notifications,
|
||||||
|
menu,
|
||||||
|
selectedCellCount,
|
||||||
|
selectedRowCount,
|
||||||
|
selectedCells,
|
||||||
|
} = getContext("grid")
|
||||||
|
|
||||||
let modal
|
let rowsModal
|
||||||
|
let cellsModal
|
||||||
|
|
||||||
$: selectedRowCount = Object.values($selectedRows).length
|
|
||||||
$: rowsToDelete = Object.entries($selectedRows)
|
$: rowsToDelete = Object.entries($selectedRows)
|
||||||
.map(entry => $rows.find(x => x._id === entry[0]))
|
.map(entry => $rows.find(x => x._id === entry[0]))
|
||||||
.filter(x => x != null)
|
.filter(x => x != null)
|
||||||
|
|
||||||
// Deletion callback when confirmed
|
const handleBulkDeleteRequest = () => {
|
||||||
const performDeletion = async () => {
|
if ($selectedRowCount) {
|
||||||
|
rowsModal?.show()
|
||||||
|
} else if ($selectedCellCount) {
|
||||||
|
cellsModal?.show()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const bulkDeleteRows = async () => {
|
||||||
const count = rowsToDelete.length
|
const count = rowsToDelete.length
|
||||||
await rows.actions.deleteRows(rowsToDelete)
|
await rows.actions.deleteRows(rowsToDelete)
|
||||||
$notifications.success(`Deleted ${count} row${count === 1 ? "" : "s"}`)
|
$notifications.success(`Deleted ${count} row${count === 1 ? "" : "s"}`)
|
||||||
|
@ -22,18 +38,48 @@
|
||||||
menu.actions.close()
|
menu.actions.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => subscribe("request-bulk-delete", () => modal?.show()))
|
const bulkDeleteCells = async () => {
|
||||||
|
let changeMap = {}
|
||||||
|
for (let row of $selectedCells) {
|
||||||
|
for (let cellId of row) {
|
||||||
|
const { rowId, field } = parseCellID(cellId)
|
||||||
|
if (!changeMap[rowId]) {
|
||||||
|
changeMap[rowId] = {}
|
||||||
|
}
|
||||||
|
changeMap[rowId][field] = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await rows.actions.bulkUpdate(changeMap)
|
||||||
|
|
||||||
|
// Ensure menu is closed, as we may have triggered this from there
|
||||||
|
menu.actions.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
onMount(() => subscribe("request-bulk-delete", handleBulkDeleteRequest))
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Modal bind:this={modal}>
|
<Modal bind:this={rowsModal}>
|
||||||
<ModalContent
|
<ModalContent
|
||||||
title="Delete rows"
|
title="Delete rows"
|
||||||
confirmText="Continue"
|
confirmText="Continue"
|
||||||
cancelText="Cancel"
|
cancelText="Cancel"
|
||||||
onConfirm={performDeletion}
|
onConfirm={bulkDeleteRows}
|
||||||
size="M"
|
size="M"
|
||||||
>
|
>
|
||||||
Are you sure you want to delete {selectedRowCount}
|
Are you sure you want to delete {$selectedRowCount}
|
||||||
row{selectedRowCount === 1 ? "" : "s"}?
|
row{$selectedRowCount === 1 ? "" : "s"}?
|
||||||
|
</ModalContent>
|
||||||
|
</Modal>
|
||||||
|
|
||||||
|
<Modal bind:this={cellsModal}>
|
||||||
|
<ModalContent
|
||||||
|
title="Delete cells"
|
||||||
|
confirmText="Continue"
|
||||||
|
cancelText="Cancel"
|
||||||
|
onConfirm={bulkDeleteCells}
|
||||||
|
size="M"
|
||||||
|
>
|
||||||
|
Are you sure you want to delete {$selectedCellCount}
|
||||||
|
cell{$selectedCellCount === 1 ? "" : "s"}?
|
||||||
</ModalContent>
|
</ModalContent>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
|
@ -81,6 +81,9 @@
|
||||||
return handle(() => changeFocusedRow(1, e.shiftKey))
|
return handle(() => changeFocusedRow(1, e.shiftKey))
|
||||||
case "Escape":
|
case "Escape":
|
||||||
return handle(selectedCells.actions.clear)
|
return handle(selectedCells.actions.clear)
|
||||||
|
case "Delete":
|
||||||
|
case "Backspace":
|
||||||
|
return handle(() => dispatch("request-bulk-delete"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
selectedRowCount,
|
selectedRowCount,
|
||||||
copyAllowed,
|
copyAllowed,
|
||||||
pasteAllowed,
|
pasteAllowed,
|
||||||
|
selectedCellCount,
|
||||||
} = getContext("grid")
|
} = getContext("grid")
|
||||||
|
|
||||||
let anchor
|
let anchor
|
||||||
|
@ -92,8 +93,12 @@
|
||||||
>
|
>
|
||||||
Paste
|
Paste
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
<MenuItem icon="Delete" disabled={isNewRow} on:click={() => {}}>
|
<MenuItem
|
||||||
Delete
|
icon="Delete"
|
||||||
|
disabled={!$config.canEditRows}
|
||||||
|
on:click={() => dispatch("request-bulk-delete")}
|
||||||
|
>
|
||||||
|
Delete {$selectedCellCount} cells
|
||||||
</MenuItem>
|
</MenuItem>
|
||||||
{:else}
|
{:else}
|
||||||
<MenuItem
|
<MenuItem
|
||||||
|
|
Loading…
Reference in New Issue