StdCurd.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <script setup lang="ts">
  2. import gettext from '@/gettext'
  3. const {$gettext, interpolate} = gettext
  4. import StdTable from './StdTable.vue'
  5. import StdDataEntry from '@/components/StdDataEntry'
  6. import {reactive, ref} from 'vue'
  7. import {message} from 'ant-design-vue'
  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. })
  44. const visible = ref(false)
  45. const update = ref(0)
  46. const data: any = reactive({id: null})
  47. const error: any = reactive({})
  48. const params = reactive({})
  49. const selected = reactive([])
  50. function onSelect(keys: any) {
  51. selected.concat(...keys)
  52. }
  53. function editableColumns() {
  54. return props.columns!.filter((c: any) => {
  55. return c.edit
  56. })
  57. }
  58. function add() {
  59. Object.keys(data).forEach(v => {
  60. delete data[v]
  61. })
  62. clear_error()
  63. visible.value = true
  64. }
  65. const table = ref(null)
  66. interface Table {
  67. get_list(): void
  68. }
  69. function clear_error() {
  70. Object.keys(error).forEach(v => {
  71. delete error[v]
  72. })
  73. }
  74. const ok = async () => {
  75. clear_error()
  76. props.api!.save(data.id, data).then((r: any) => {
  77. message.success($gettext('Save Successfully'))
  78. Object.assign(data, r)
  79. const t: Table | null = table.value
  80. t!.get_list()
  81. }).catch((e: any) => {
  82. message.error($gettext(e?.message ?? 'Server error'), 5)
  83. Object.assign(error, e.errors)
  84. })
  85. }
  86. function cancel() {
  87. visible.value = false
  88. clear_error()
  89. }
  90. function edit(id: any) {
  91. props.api!.get(id).then((r: any) => {
  92. Object.assign(data, r)
  93. visible.value = true
  94. }).catch((e: any) => {
  95. message.error($gettext(e?.message ?? 'Server error'), 5)
  96. })
  97. }
  98. </script>
  99. <template>
  100. <div class="std-curd">
  101. <a-card :title="title||$gettext('Table')">
  102. <template v-if="!disable_add" #extra>
  103. <a @click="add" v-translate>Add</a>
  104. </template>
  105. <std-table
  106. ref="table"
  107. v-bind="props"
  108. @clickEdit="edit"
  109. @selected="onSelect"
  110. :key="update"
  111. >
  112. <template v-slot:actions="slotProps">
  113. <slot name="actions" :actions="slotProps.record"/>
  114. </template>
  115. </std-table>
  116. </a-card>
  117. <a-modal
  118. class="std-curd-edit-modal"
  119. :mask="false"
  120. :title="data.id ? $gettext('Modify') : $gettext('Add')"
  121. :visible="visible"
  122. :cancel-text="$gettext('Cancel')"
  123. :ok-text="$gettext('OK')"
  124. @cancel="cancel"
  125. @ok="ok"
  126. :width="600"
  127. destroyOnClose
  128. >
  129. <std-data-entry
  130. ref="std_data_entry"
  131. :data-list="editableColumns()"
  132. :data-source="data"
  133. :error="error"
  134. />
  135. </a-modal>
  136. </div>
  137. </template>
  138. <style lang="less" scoped>
  139. </style>