From fcad85920c040e0b6e00d001b1e363bce5494a2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A5=9E=E4=BB=99=E9=83=BD=E6=B2=A1=E7=94=A8?= <615206459@qq.com> Date: Fri, 22 Mar 2024 14:38:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../other => user/components}/select-user.vue | 182 +++++++++++++----- 1 file changed, 134 insertions(+), 48 deletions(-) rename src/modules/{demo/views/crud/components/other => user/components}/select-user.vue (56%) diff --git a/src/modules/demo/views/crud/components/other/select-user.vue b/src/modules/user/components/select-user.vue similarity index 56% rename from src/modules/demo/views/crud/components/other/select-user.vue rename to src/modules/user/components/select-user.vue index e41579c..ab008a7 100644 --- a/src/modules/demo/views/crud/components/other/select-user.vue +++ b/src/modules/user/components/select-user.vue @@ -1,25 +1,45 @@ @@ -64,10 +97,11 @@ import { useCrud, useForm, useTable } from "@cool-vue/crud"; import { useCool } from "/@/cool"; import { type PropType, computed, nextTick, reactive, ref, watch } from "vue"; -import { cloneDeep } from "lodash-es"; +import { cloneDeep, isEmpty } from "lodash-es"; +import { CircleClose } from "@element-plus/icons-vue"; // 替换你的类型 -type Item = Eps.BaseSysUserEntity; +type Item = Eps.UserInfoEntity; const props = defineProps({ modelValue: { @@ -77,7 +111,13 @@ const props = defineProps({ isDisabled: Boolean, prop: String, scope: null, - disabled: Boolean + disabled: Boolean, + + // 是否多选 + multiple: { + type: Boolean, + default: true + } }); const emit = defineEmits(["update:modelValue"]); @@ -107,13 +147,19 @@ const options = reactive({ const Table = useTable({ contextMenu: [], columns: [ + props.multiple + ? { + type: "selection", + width: 60, + reserveSelection: true + } + : { + label: "操作", + prop: "check", + width: 100 + }, { - type: "selection", - width: 60, - reserveSelection: true - }, - { - prop: "headImg", + prop: "avatarUrl", label: "头像", component: { name: "cl-avatar" @@ -121,20 +167,15 @@ const Table = useTable({ minWidth: 100 }, { - prop: "username", + prop: "phone", label: "手机号", minWidth: 120 }, { - prop: "name", + prop: "nickName", label: "姓名", minWidth: 150 }, - { - prop: "departmentName", - label: "部门名称", - minWidth: 150 - }, { label: "状态", prop: "status", @@ -152,7 +193,7 @@ const Table = useTable({ // cl-crud const Crud = useCrud({ - service: service.base.sys.user, + service: service.user.info, async onRefresh(params, { next }) { const res = await next(params); @@ -231,8 +272,14 @@ function close() { } // 选择 -function select() { +function select(item?: Item) { + // 单选不触发 onSelectionChange 手动设置 + if (item) { + selection.value = [item]; + } + list.value = cloneDeep(selection.value || []); + close(); } @@ -251,31 +298,41 @@ async function selectAll() { // 移除 function remove() { - const ids = (refs.table.selection as any[]).map((e) => e.id); + const ids = selection.value.map((e) => e.id); list.value = list.value.filter((e) => { // 清空选择状态 - refs.table.toggleRowSelection(e, false); + refs.table?.toggleRowSelection(e, false); // 移除已选的 return !ids.find((id) => id == e.id); }); } -// 监听已选列表,返回 ids +// 监听已选列表,返回 ids/id watch( list, (arr = []) => { - emit( - "update:modelValue", - arr.map((e) => e.id) - ); + const ids = arr.map((e) => e.id); + + if (props.multiple) { + emit("update:modelValue", ids); + } else { + emit("update:modelValue", ids[0]); + } + Form.value?.validateField(props.prop); }, { deep: true } ); + +defineExpose({ + remove, + select, + selectAll +});