StdBatchEdit.vue 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <script setup lang="ts">
  2. import type Curd from '@/api/curd'
  3. import type { Column } from '@/components/StdDesign/types'
  4. import { getPithyColumns } from '@/components/StdDesign/StdDataDisplay/methods/columns'
  5. import StdDataEntry from '@/components/StdDesign/StdDataEntry'
  6. import { message } from 'ant-design-vue'
  7. const props = defineProps<{
  8. // eslint-disable-next-line ts/no-explicit-any
  9. api: Curd<any>
  10. beforeSave?: () => Promise<void>
  11. columns: Column[]
  12. }>()
  13. const emit = defineEmits(['save'])
  14. const batchColumns = ref<Column[]>([])
  15. const selectedRowKeys = ref<(number | string)[]>([])
  16. // eslint-disable-next-line ts/no-explicit-any
  17. const selectedRows = ref<any[]>([])
  18. const visible = ref(false)
  19. const data = ref({})
  20. const error = ref({})
  21. const loading = ref(false)
  22. // eslint-disable-next-line ts/no-explicit-any
  23. function showModal(c: Column[], rowKeys: (number | string)[], rows: any[]) {
  24. data.value = {}
  25. visible.value = true
  26. selectedRowKeys.value = rowKeys
  27. batchColumns.value = c
  28. selectedRows.value = rows
  29. }
  30. defineExpose({
  31. showModal,
  32. })
  33. async function ok() {
  34. loading.value = true
  35. await props.beforeSave?.()
  36. await props.api.batch_save(selectedRowKeys.value, data.value)
  37. .then(async () => {
  38. message.success($gettext('Save successfully'))
  39. emit('save')
  40. visible.value = false
  41. })
  42. .catch(e => {
  43. error.value = e.errors
  44. message.error($gettext(e?.message) ?? $gettext('Server error'))
  45. })
  46. .finally(() => {
  47. loading.value = false
  48. })
  49. }
  50. </script>
  51. <template>
  52. <AModal
  53. v-model:open="visible"
  54. class="std-curd-edit-modal"
  55. :mask="false"
  56. :title="$gettext('Batch Modify')"
  57. :cancel-text="$gettext('No')"
  58. :ok-text="$gettext('Save')"
  59. :confirm-loading="loading"
  60. :width="600"
  61. destroy-on-close
  62. @ok="ok"
  63. >
  64. <p>{{ $gettext('Belows are selected items that you want to batch modify') }}</p>
  65. <ATable
  66. class="mb-4"
  67. size="small"
  68. :columns="getPithyColumns(columns)"
  69. :data-source="selectedRows"
  70. :pagination="{ showSizeChanger: false, pageSize: 5, size: 'small' }"
  71. />
  72. <p>{{ $gettext('Leave blank if do not want to modify') }}</p>
  73. <StdDataEntry
  74. :data-list="batchColumns"
  75. :data-source="data"
  76. :errors="error"
  77. />
  78. <slot name="extra" />
  79. </AModal>
  80. </template>
  81. <style scoped></style>