StdCurd.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <script setup lang="ts">
  2. import gettext from '@/gettext'
  3. import StdTable from './StdTable.vue'
  4. import StdDataEntry from '@/components/StdDataEntry'
  5. import {reactive, ref} from 'vue'
  6. import {message} from 'ant-design-vue'
  7. const {$gettext} = gettext
  8. const props = defineProps({
  9. api: Object,
  10. columns: Array,
  11. title: String,
  12. data_key: {
  13. type: String,
  14. default: 'data'
  15. },
  16. disable_search: {
  17. type: Boolean,
  18. default: false
  19. },
  20. disable_add: {
  21. type: Boolean,
  22. default: false
  23. },
  24. soft_delete: {
  25. type: Boolean,
  26. default: false
  27. },
  28. edit_text: String,
  29. deletable: {
  30. type: Boolean,
  31. default: true
  32. },
  33. get_params: {
  34. type: Object,
  35. default() {
  36. return {}
  37. }
  38. },
  39. editable: {
  40. type: Boolean,
  41. default: true
  42. },
  43. beforeSave: {
  44. type: Function,
  45. default: null
  46. },
  47. exportCsv: {
  48. type: Boolean,
  49. default: false
  50. }
  51. })
  52. const visible = ref(false)
  53. const update = ref(0)
  54. const data: any = reactive({id: null})
  55. const error: any = reactive({})
  56. const selected = reactive([])
  57. function onSelect(keys: any) {
  58. selected.concat(...keys)
  59. }
  60. function editableColumns() {
  61. return props.columns!.filter((c: any) => {
  62. return c.edit
  63. })
  64. }
  65. function add() {
  66. Object.keys(data).forEach(v => {
  67. delete data[v]
  68. })
  69. clear_error()
  70. visible.value = true
  71. }
  72. const table = ref(null)
  73. interface Table {
  74. get_list(): void
  75. }
  76. function clear_error() {
  77. Object.keys(error).forEach(v => {
  78. delete error[v]
  79. })
  80. }
  81. const ok = async () => {
  82. clear_error()
  83. await props?.beforeSave?.(data)
  84. props.api!.save(data.id, data).then((r: any) => {
  85. message.success($gettext('Save Successfully'))
  86. Object.assign(data, r)
  87. const t: Table | null = table.value
  88. t!.get_list()
  89. }).catch((e: any) => {
  90. message.error($gettext(e?.message ?? 'Server error'), 5)
  91. Object.assign(error, e.errors)
  92. })
  93. }
  94. function cancel() {
  95. visible.value = false
  96. clear_error()
  97. }
  98. function edit(id: any) {
  99. props.api!.get(id).then((r: any) => {
  100. Object.assign(data, r)
  101. visible.value = true
  102. }).catch((e: any) => {
  103. message.error($gettext(e?.message ?? 'Server error'), 5)
  104. })
  105. }
  106. </script>
  107. <template>
  108. <div class="std-curd">
  109. <a-card :title="title||$gettext('Table')">
  110. <template v-if="!disable_add" #extra>
  111. <a @click="add" v-translate>Add</a>
  112. </template>
  113. <std-table
  114. ref="table"
  115. v-bind="props"
  116. @clickEdit="edit"
  117. @selected="onSelect"
  118. :key="update"
  119. :get_params="get_params"
  120. :exportCsv="exportCsv"
  121. >
  122. <template v-slot:actions="slotProps">
  123. <slot name="actions" :actions="slotProps.record"/>
  124. </template>
  125. </std-table>
  126. </a-card>
  127. <a-modal
  128. class="std-curd-edit-modal"
  129. :mask="false"
  130. :title="data.id ? $gettext('Modify') : $gettext('Add')"
  131. :visible="visible"
  132. :cancel-text="$gettext('Cancel')"
  133. :ok-text="$gettext('OK')"
  134. @cancel="cancel"
  135. @ok="ok"
  136. :width="600"
  137. destroyOnClose
  138. >
  139. <std-data-entry
  140. ref="std_data_entry"
  141. :data-list="editableColumns()"
  142. v-model:data-source="data"
  143. :error="error"
  144. />
  145. <slot name="edit" :data="data"/>
  146. </a-modal>
  147. </div>
  148. </template>
  149. <style lang="less" scoped>
  150. </style>