fix: 修复列表内状态修改组件

This commit is contained in:
xlsea 2024-04-25 21:29:20 +08:00
parent ac9b427731
commit 0535fad50b
3 changed files with 4799 additions and 5732 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,39 +1,49 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref } from 'vue'; import { ref, watch } from 'vue';
defineOptions({ defineOptions({
name: 'StatusSwitch' name: 'StatusSwitch'
}); });
interface Props {
value: Api.Common.EnableStatusNumber;
}
const props = withDefaults(defineProps<Props>(), {
value: 0
});
interface Emits { interface Emits {
(e: 'update:value', value: Api.Common.EnableStatusNumber): void; (e: 'fetch', value: Api.Common.EnableStatusNumber): void;
} }
const emit = defineEmits<Emits>(); const emit = defineEmits<Emits>();
const value = defineModel<Api.Common.EnableStatusNumber>('value', { const active = ref(props.value);
required: true
});
const loading = ref(false); const loading = ref(false);
const setLoading = (val: boolean) => { watch(
loading.value = val; () => props.value,
}; value => {
active.value = value;
}
);
defineExpose({ const handleUpdateValue = (value: Api.Common.EnableStatusNumber) => {
setLoading loading.value = true;
}); emit('fetch', value);
loading.value = false;
};
</script> </script>
<template> <template>
<NSwitch <NSwitch
v-model:value="value" :value="active"
:loading="loading" :loading="loading"
:checked-value="1" :checked-value="1"
:unchecked-value="0" :unchecked-value="0"
@update:value="emit('update:value', value)" @update:value="handleUpdateValue"
></NSwitch> />
</template> </template>
<style scoped></style> <style scoped></style>

View File

@ -1,5 +1,4 @@
<script setup lang="tsx"> <script setup lang="tsx">
import { ref } from 'vue';
import { NButton, NPopconfirm, NTag } from 'naive-ui'; import { NButton, NPopconfirm, NTag } from 'naive-ui';
import { fetchGetJobPage, fetchUpdateJobStatus } from '@/service/api'; import { fetchGetJobPage, fetchUpdateJobStatus } from '@/service/api';
import { $t } from '@/locales'; import { $t } from '@/locales';
@ -57,29 +56,18 @@ const { columns, columnChecks, data, getData, loading, mobilePagination, searchP
align: 'center', align: 'center',
minWidth: 120, minWidth: 120,
render: row => { render: row => {
const switchRef = ref<InstanceType<typeof StatusSwitch>>(); const fetchFn = async (jobStatus: Api.Common.EnableStatusNumber) => {
const { error } = await fetchUpdateJobStatus({ id: row.id!, jobStatus });
const handleUpdateValue = async (id: string, jobStatus: Api.Common.EnableStatusNumber) => { if (!error) {
switchRef.value?.setLoading(true); row.jobStatus = jobStatus;
try {
await fetchUpdateJobStatus({ id, jobStatus });
window.$message?.success($t('common.updateSuccess')); window.$message?.success($t('common.updateSuccess'));
} catch {
window.$message?.error($t('common.updateFailed'));
} finally {
switchRef.value?.setLoading(false);
await getData();
} }
}; };
return ( return (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment <>
/* @ts-expect-error */ <StatusSwitch v-model:value={row.jobStatus} onFetch={fetchFn} />
<StatusSwitch </>
v-model:value={row.jobStatus}
ref={switchRef}
on-update:value={(status: Api.Common.EnableStatusNumber) => handleUpdateValue(row.id!, status)}
></StatusSwitch>
); );
} }
}, },