123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <script setup lang="ts">
- import gettext from '@/gettext'
- import StdTable from './StdTable.vue'
- import StdDataEntry from '@/components/StdDataEntry'
- import {reactive, ref} from 'vue'
- import {message} from 'ant-design-vue'
- const {$gettext} = gettext
- const props = defineProps({
- api: Object,
- columns: Array,
- title: String,
- data_key: {
- type: String,
- default: 'data'
- },
- disable_search: {
- type: Boolean,
- default: false
- },
- disable_add: {
- type: Boolean,
- default: false
- },
- soft_delete: {
- type: Boolean,
- default: false
- },
- edit_text: String,
- deletable: {
- type: Boolean,
- default: true
- },
- get_params: {
- type: Object,
- default() {
- return {}
- }
- },
- editable: {
- type: Boolean,
- default: true
- },
- beforeSave: {
- type: Function,
- default: null
- },
- exportCsv: {
- type: Boolean,
- default: false
- }
- })
- const visible = ref(false)
- const update = ref(0)
- const data: any = reactive({id: null})
- const error: any = reactive({})
- const selected = reactive([])
- function onSelect(keys: any) {
- selected.concat(...keys)
- }
- function editableColumns() {
- return props.columns!.filter((c: any) => {
- return c.edit
- })
- }
- function add() {
- Object.keys(data).forEach(v => {
- delete data[v]
- })
- clear_error()
- visible.value = true
- }
- const table = ref(null)
- interface Table {
- get_list(): void
- }
- function clear_error() {
- Object.keys(error).forEach(v => {
- delete error[v]
- })
- }
- const ok = async () => {
- clear_error()
- await props?.beforeSave?.(data)
- props.api!.save(data.id, data).then((r: any) => {
- message.success($gettext('Save Successfully'))
- Object.assign(data, r)
- const t: Table | null = table.value
- t!.get_list()
- }).catch((e: any) => {
- message.error($gettext(e?.message ?? 'Server error'), 5)
- Object.assign(error, e.errors)
- })
- }
- function cancel() {
- visible.value = false
- clear_error()
- }
- function edit(id: any) {
- props.api!.get(id).then((r: any) => {
- Object.assign(data, r)
- visible.value = true
- }).catch((e: any) => {
- message.error($gettext(e?.message ?? 'Server error'), 5)
- })
- }
- </script>
- <template>
- <div class="std-curd">
- <a-card :title="title||$gettext('Table')">
- <template v-if="!disable_add" #extra>
- <a @click="add" v-translate>Add</a>
- </template>
- <std-table
- ref="table"
- v-bind="props"
- @clickEdit="edit"
- @selected="onSelect"
- :key="update"
- :get_params="get_params"
- :exportCsv="exportCsv"
- >
- <template v-slot:actions="slotProps">
- <slot name="actions" :actions="slotProps.record"/>
- </template>
- </std-table>
- </a-card>
- <a-modal
- class="std-curd-edit-modal"
- :mask="false"
- :title="data.id ? $gettext('Modify') : $gettext('Add')"
- :visible="visible"
- :cancel-text="$gettext('Cancel')"
- :ok-text="$gettext('OK')"
- @cancel="cancel"
- @ok="ok"
- :width="600"
- destroyOnClose
- >
- <std-data-entry
- ref="std_data_entry"
- :data-list="editableColumns()"
- v-model:data-source="data"
- :error="error"
- />
- <slot name="edit" :data="data"/>
- </a-modal>
- </div>
- </template>
- <style lang="less" scoped>
- </style>
|