WIP - Mass delete working
This commit is contained in:
@@ -12,6 +12,7 @@ import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
import { Badge } from '@/components/ui/badge'
|
||||
import { Checkbox } from '@/components/ui/checkbox'
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||
import FieldRenderer from '@/components/fields/FieldRenderer.vue'
|
||||
import { ListViewConfig, ViewMode, FieldType } from '@/types/field-types'
|
||||
import { ChevronDown, ChevronUp, Search, Plus, Download, Trash2, Edit } from 'lucide-vue-next'
|
||||
@@ -49,11 +50,13 @@ const emit = defineEmits<{
|
||||
}>()
|
||||
|
||||
// State
|
||||
const selectedRows = ref<Set<string>>(new Set())
|
||||
const normalizeId = (id: any) => String(id)
|
||||
const selectedRowIds = ref<string[]>([])
|
||||
const searchQuery = ref('')
|
||||
const sortField = ref<string>('')
|
||||
const sortDirection = ref<'asc' | 'desc'>('asc')
|
||||
const currentPage = ref(1)
|
||||
const bulkAction = ref('delete')
|
||||
|
||||
// Computed
|
||||
const visibleFields = computed(() =>
|
||||
@@ -94,27 +97,39 @@ const showLoadMore = computed(() => (
|
||||
))
|
||||
|
||||
const allSelected = computed({
|
||||
get: () => props.data.length > 0 && selectedRows.value.size === props.data.length,
|
||||
get: () => props.data.length > 0 && selectedRowIds.value.length === props.data.length,
|
||||
set: (val: boolean) => {
|
||||
if (val) {
|
||||
selectedRows.value = new Set(props.data.map(row => row.id))
|
||||
selectedRowIds.value = props.data.map(row => normalizeId(row.id))
|
||||
} else {
|
||||
selectedRows.value.clear()
|
||||
selectedRowIds.value = []
|
||||
}
|
||||
emit('row-select', getSelectedRows())
|
||||
},
|
||||
})
|
||||
|
||||
const getSelectedRows = () => {
|
||||
return props.data.filter(row => selectedRows.value.has(row.id))
|
||||
const idSet = new Set(selectedRowIds.value)
|
||||
return props.data.filter(row => idSet.has(normalizeId(row.id)))
|
||||
}
|
||||
|
||||
const toggleRowSelection = (rowId: string) => {
|
||||
if (selectedRows.value.has(rowId)) {
|
||||
selectedRows.value.delete(rowId)
|
||||
const normalizedId = normalizeId(rowId)
|
||||
const nextSelection = new Set(selectedRowIds.value)
|
||||
nextSelection.has(normalizedId) ? nextSelection.delete(normalizedId) : nextSelection.add(normalizedId)
|
||||
selectedRowIds.value = Array.from(nextSelection)
|
||||
emit('row-select', getSelectedRows())
|
||||
}
|
||||
|
||||
const setRowSelection = (rowId: string, checked: boolean) => {
|
||||
const normalizedId = normalizeId(rowId)
|
||||
const nextSelection = new Set(selectedRowIds.value)
|
||||
if (checked) {
|
||||
nextSelection.add(normalizedId)
|
||||
} else {
|
||||
selectedRows.value.add(rowId)
|
||||
nextSelection.delete(normalizedId)
|
||||
}
|
||||
selectedRowIds.value = Array.from(nextSelection)
|
||||
emit('row-select', getSelectedRows())
|
||||
}
|
||||
|
||||
@@ -136,6 +151,14 @@ const handleAction = (actionId: string) => {
|
||||
emit('action', actionId, getSelectedRows())
|
||||
}
|
||||
|
||||
const handleBulkAction = () => {
|
||||
if (bulkAction.value === 'delete') {
|
||||
emit('delete', getSelectedRows())
|
||||
return
|
||||
}
|
||||
emit('action', bulkAction.value, getSelectedRows())
|
||||
}
|
||||
|
||||
const goToPage = (page: number) => {
|
||||
const nextPage = Math.min(Math.max(page, 1), availablePages.value)
|
||||
if (nextPage !== currentPage.value) {
|
||||
@@ -157,6 +180,19 @@ watch(
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
watch(
|
||||
() => props.data,
|
||||
(rows) => {
|
||||
const rowIds = new Set(rows.map(row => normalizeId(row.id)))
|
||||
const nextSelection = selectedRowIds.value.filter(id => rowIds.has(id))
|
||||
if (nextSelection.length !== selectedRowIds.value.length) {
|
||||
selectedRowIds.value = nextSelection
|
||||
emit('row-select', getSelectedRows())
|
||||
}
|
||||
},
|
||||
{ deep: true }
|
||||
)
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -181,14 +217,24 @@ watch(
|
||||
|
||||
<div class="flex items-center gap-2">
|
||||
<!-- Bulk Actions -->
|
||||
<template v-if="selectedRows.size > 0">
|
||||
<template v-if="selectedRowIds.length > 0">
|
||||
<Badge variant="secondary" class="px-3 py-1">
|
||||
{{ selectedRows.size }} selected
|
||||
{{ selectedRowIds.length }} selected
|
||||
</Badge>
|
||||
<Button variant="outline" size="sm" @click="emit('delete', getSelectedRows())">
|
||||
<Trash2 class="h-4 w-4 mr-2" />
|
||||
Delete
|
||||
</Button>
|
||||
<div class="flex items-center gap-2">
|
||||
<Select v-model="bulkAction" @update:model-value="(value) => bulkAction = value">
|
||||
<SelectTrigger class="h-8 w-[180px]">
|
||||
<SelectValue placeholder="Select action" />
|
||||
</SelectTrigger>
|
||||
<SelectContent>
|
||||
<SelectItem value="delete">Delete selected</SelectItem>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<Button variant="outline" size="sm" @click="handleBulkAction">
|
||||
<Trash2 class="h-4 w-4 mr-2" />
|
||||
Run
|
||||
</Button>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- Custom Actions -->
|
||||
@@ -222,7 +268,10 @@ watch(
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
<TableHead v-if="selectable" class="w-12">
|
||||
<Checkbox v-model:checked="allSelected" />
|
||||
<Checkbox
|
||||
:model-value="allSelected"
|
||||
@update:model-value="(value: boolean) => (allSelected = value)"
|
||||
/>
|
||||
</TableHead>
|
||||
<TableHead
|
||||
v-for="field in visibleFields"
|
||||
@@ -263,8 +312,8 @@ watch(
|
||||
>
|
||||
<TableCell v-if="selectable" @click.stop>
|
||||
<Checkbox
|
||||
:checked="selectedRows.has(row.id)"
|
||||
@update:checked="toggleRowSelection(row.id)"
|
||||
:model-value="selectedRowIds.includes(normalizeId(row.id))"
|
||||
@update:model-value="(checked: boolean) => setRowSelection(normalizeId(row.id), checked)"
|
||||
/>
|
||||
</TableCell>
|
||||
<TableCell v-for="field in visibleFields" :key="field.id">
|
||||
|
||||
Reference in New Issue
Block a user