Skip to content

Commit 201244b

Browse files
committed
feat: Add row actions to delete or duplicate a row
Signed-off-by: Julius Knorr <[email protected]>
1 parent d075d13 commit 201244b

File tree

3 files changed

+90
-9
lines changed

3 files changed

+90
-9
lines changed

src/modules/modals/EditRow.vue

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ export default {
178178
},
179179
reset() {
180180
this.localRow = {}
181-
this.dataLoaded = false
182181
this.prepareDeleteRow = false
183182
},
184183
actionDeleteRow() {

src/shared/components/ncTable/partials/TableRow.vue

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,44 @@
1313
:row-id="row.id"
1414
:value="getCellValue(col)" />
1515
</td>
16-
<td v-if="config.showActions" :class="{sticky: config.showActions}">
17-
<NcButton v-if="config.canEditRows || config.canDeleteRows" type="primary" :aria-label="t('tables', 'Edit row')" data-cy="editRowBtn" @click="$emit('edit-row', row.id)">
18-
<template #icon>
19-
<Pencil :size="20" />
20-
</template>
21-
</NcButton>
16+
<td v-if="config.showActions" :class="{sticky: config.showActions}" class="row-actions">
17+
<div class="row-actions-container">
18+
<NcButton v-if="config.canEditRows || config.canDeleteRows" type="secondary" :aria-label="t('tables', 'Edit row')" data-cy="editRowBtn" @click="$emit('edit-row', row.id)">
19+
<template #icon>
20+
<Pencil :size="20" />
21+
</template>
22+
</NcButton>
23+
<NcActions v-if="config.canDeleteRows || config.canCreateRows">
24+
<NcActionButton v-if="config.canCreateRows"
25+
:close-after-click="true"
26+
@click="handleCloneRow">
27+
<template #icon>
28+
<ContentCopy :size="20" />
29+
</template>
30+
{{ t('tables', 'Duplicate row') }}
31+
</NcActionButton>
32+
<NcActionButton v-if="config.canDeleteRows"
33+
:close-after-click="true"
34+
@click="handleDeleteRow">
35+
<template #icon>
36+
<Delete :size="20" />
37+
</template>
38+
{{ t('tables', 'Delete row') }}
39+
</NcActionButton>
40+
</NcActions>
41+
</div>
2242
</td>
2343
</tr>
2444
</template>
2545

2646
<script>
27-
import { NcCheckboxRadioSwitch, NcButton } from '@nextcloud/vue'
47+
import { mapActions } from 'pinia'
48+
import { useDataStore } from '../../../../store/data.js'
49+
import { NcCheckboxRadioSwitch, NcButton, NcActions, NcActionButton } from '@nextcloud/vue'
50+
import { getDialogBuilder, showError, DialogSeverity } from '@nextcloud/dialogs'
2851
import Pencil from 'vue-material-design-icons/Pencil.vue'
52+
import ContentCopy from 'vue-material-design-icons/ContentCopy.vue'
53+
import Delete from 'vue-material-design-icons/Delete.vue'
2954
import TableCellHtml from './TableCellHtml.vue'
3055
import TableCellProgress from './TableCellProgress.vue'
3156
import TableCellLink from './TableCellLink.vue'
@@ -55,13 +80,17 @@ export default {
5580
TableCellHtml,
5681
NcButton,
5782
Pencil,
83+
ContentCopy,
84+
Delete,
5885
NcCheckboxRadioSwitch,
5986
TableCellDateTime,
6087
TableCellTextLine,
6188
TableCellSelection,
6289
TableCellMultiSelection,
6390
TableCellTextRich,
6491
TableCellUsergroup,
92+
NcActions,
93+
NcActionButton,
6594
},
6695
6796
props: {
@@ -85,6 +114,14 @@ export default {
85114
type: Object,
86115
default: null,
87116
},
117+
elementId: {
118+
type: Number,
119+
default: null,
120+
},
121+
isView: {
122+
type: Boolean,
123+
default: false,
124+
},
88125
},
89126
computed: {
90127
getSelection: {
@@ -165,6 +202,42 @@ export default {
165202
return text
166203
}
167204
},
205+
...mapActions(useDataStore, ['removeRow', 'insertNewRow']),
206+
async handleDeleteRow() {
207+
await getDialogBuilder(t('tables', 'Delete row'))
208+
.setText(t('tables', 'Are you sure you want to delete this row?'))
209+
.setSeverity(DialogSeverity.Warning)
210+
.addButton({
211+
label: t('tables', 'Delete'),
212+
type: 'error',
213+
callback: async () => {
214+
const res = await this.removeRow({
215+
rowId: this.row.id,
216+
isView: this.isView,
217+
elementId: this.elementId,
218+
})
219+
if (!res) {
220+
showError(t('tables', 'Could not delete row.'))
221+
}
222+
},
223+
})
224+
.build()
225+
.show()
226+
},
227+
async handleCloneRow() {
228+
const data = this.row.data.reduce((acc, curr) => {
229+
acc[curr.columnId] = curr.value
230+
return acc
231+
}, {})
232+
const res = await this.insertNewRow({
233+
viewId: this.isView ? this.elementId : null,
234+
tableId: this.isView ? null : this.elementId,
235+
data,
236+
})
237+
if (!res) {
238+
showError(t('tables', 'Could not clone row.'))
239+
}
240+
},
168241
},
169242
}
170243
</script>
@@ -185,4 +258,11 @@ tr.selected {
185258
margin: 0;
186259
}
187260
261+
.row-actions-container {
262+
display: flex;
263+
align-items: center;
264+
justify-content: center;
265+
gap: var(--default-grid-baseline);
266+
}
267+
188268
</style>

src/shared/components/ncTable/sections/CustomTable.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
<tbody>
2626
<TableRow v-for="(row, index) in currentPageRows"
2727
:key="index"
28+
:element-id="elementId"
29+
:is-view="isView"
2830
data-cy="customTableRow"
2931
:row="row"
3032
:columns="columns"
@@ -521,7 +523,7 @@ export default {
521523
tr>th.sticky:last-child,tr>td.sticky:last-child {
522524
position: sticky;
523525
right: 0;
524-
width: 55px;
526+
width: calc(var(--button-size) * 2);
525527
background-color: inherit;
526528
padding-right: 16px;
527529
}

0 commit comments

Comments
 (0)