select.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <template>
  2. <div class="user-select__inner">
  3. <template v-if="multiple">
  4. <div class="btns">
  5. <el-button type="success" @click="open">添加</el-button>
  6. <el-button
  7. type="danger"
  8. :disabled="refs.table?.selection.length == 0"
  9. @click="remove"
  10. >移除</el-button
  11. >
  12. </div>
  13. <cl-crud padding="0">
  14. <cl-table :data="data" :ref="setRefs('table')" :auto-height="false" />
  15. <cl-row type="flex" align="middle" justify="end" :style="{ marginTop: '10px' }">
  16. <el-pagination
  17. v-model:current-page="pager.page"
  18. :page-size="pager.size"
  19. :total="list.length"
  20. background
  21. layout="total, prev, pager, next, jumper"
  22. />
  23. </cl-row>
  24. </cl-crud>
  25. </template>
  26. <template v-else>
  27. <div class="user" @click="open">
  28. <template v-if="data[0]">
  29. <cl-avatar :size="24" :src="data[0].avatarUrl"></cl-avatar>
  30. <span>{{ data[0].nickName }}</span>
  31. <el-icon @click.stop="remove">
  32. <circle-close />
  33. </el-icon>
  34. </template>
  35. <span class="placeholder" v-else>请选择用户</span>
  36. </div>
  37. </template>
  38. </div>
  39. <cl-dialog v-model="visible" width="1200px" title="选择用户">
  40. <cl-crud ref="Crud" padding="0">
  41. <cl-row>
  42. <!-- 刷新按钮 -->
  43. <cl-refresh-btn />
  44. <!-- 全选 -->
  45. <el-button type="primary" @click="selectAll" v-if="multiple">全选</el-button>
  46. <cl-filter label="状态">
  47. <cl-select :options="options.status" prop="status" :width="120" />
  48. </cl-filter>
  49. <cl-flex1 />
  50. <!-- 关键字搜索 -->
  51. <cl-search-key placeholder="搜索昵称、手机号" />
  52. </cl-row>
  53. <cl-row>
  54. <!-- 数据表格 -->
  55. <cl-table ref="Table" :auto-height="false" @selection-change="onSelectionChange">
  56. <template #column-check="{ scope }">
  57. <el-button type="success" disabled v-if="selection[0]?.id == scope.row.id"
  58. >已选</el-button
  59. >
  60. <el-button @click="select(scope.row)" v-else>选择</el-button>
  61. </template>
  62. </cl-table>
  63. </cl-row>
  64. <cl-row>
  65. <span v-if="multiple">已选 {{ selection.length }} 人</span>
  66. <cl-flex1 />
  67. <!-- 分页控件 -->
  68. <cl-pagination />
  69. </cl-row>
  70. </cl-crud>
  71. <template #footer>
  72. <el-button @click="close">取消</el-button>
  73. <el-button
  74. type="success"
  75. :disabled="isEmpty(selection)"
  76. @click="select()"
  77. v-if="multiple"
  78. >选择</el-button
  79. >
  80. </template>
  81. </cl-dialog>
  82. </template>
  83. <script lang="ts" setup name="user-select">
  84. import { useCrud, useForm, useTable } from "@cool-vue/crud";
  85. import { useCool } from "/@/cool";
  86. import { type PropType, computed, nextTick, reactive, ref, watch } from "vue";
  87. import { cloneDeep, isArray, isEmpty } from "lodash-es";
  88. import { CircleClose } from "@element-plus/icons-vue";
  89. // 替换你的类型
  90. type Item = Eps.UserInfoEntity;
  91. const props = defineProps({
  92. modelValue: {
  93. type: Array as PropType<Item[]>,
  94. default: () => []
  95. },
  96. isDisabled: Boolean,
  97. prop: String,
  98. scope: null,
  99. disabled: Boolean,
  100. // 是否多选
  101. multiple: {
  102. type: Boolean,
  103. default: true
  104. }
  105. });
  106. const emit = defineEmits(["update:modelValue"]);
  107. const { service, refs, setRefs } = useCool();
  108. // 上级表单
  109. const Form = useForm();
  110. // 选项
  111. const options = reactive({
  112. status: [
  113. {
  114. label: "启用",
  115. value: 1,
  116. type: "success"
  117. },
  118. {
  119. label: "禁用",
  120. value: 0,
  121. type: "danger"
  122. }
  123. ]
  124. });
  125. // cl-table
  126. const Table = useTable({
  127. contextMenu: [],
  128. columns: [
  129. props.multiple
  130. ? {
  131. type: "selection",
  132. width: 60,
  133. reserveSelection: true
  134. }
  135. : {
  136. label: "操作",
  137. prop: "check",
  138. width: 100
  139. },
  140. {
  141. prop: "avatarUrl",
  142. label: "头像",
  143. component: {
  144. name: "cl-avatar"
  145. },
  146. minWidth: 100
  147. },
  148. {
  149. prop: "phone",
  150. label: "手机号",
  151. minWidth: 120
  152. },
  153. {
  154. prop: "nickName",
  155. label: "姓名",
  156. minWidth: 150
  157. },
  158. {
  159. label: "状态",
  160. prop: "status",
  161. minWidth: 100,
  162. dict: options.status
  163. },
  164. {
  165. label: "创建时间",
  166. prop: "createTime",
  167. sortable: "desc",
  168. minWidth: 170
  169. }
  170. ]
  171. });
  172. // cl-crud
  173. const Crud = useCrud({
  174. service: service.user.info,
  175. async onRefresh(params, { next }) {
  176. const res = await next(params);
  177. // 添加已加载列表的 id
  178. loadIds.value.push(...res.list.map((e) => e.id));
  179. // 数据反选
  180. selection.value.forEach((e) => {
  181. const d = Table.value?.data.find((a) => a.id == e.id);
  182. if (d) {
  183. Table.value?.toggleRowSelection(d, true);
  184. }
  185. });
  186. }
  187. });
  188. // 刷新
  189. async function refresh(params?: any) {
  190. return Crud.value?.refresh(params);
  191. }
  192. // 弹窗是否可见
  193. const visible = ref(false);
  194. // 已选的数据列表,双向绑定用
  195. const list = ref<Item[]>([]);
  196. // 已选列表
  197. const selection = ref<any[]>([]);
  198. // 分页
  199. const pager = reactive({
  200. page: 1,
  201. size: 10
  202. });
  203. // 分页数据
  204. const data = computed(() => {
  205. const { page, size } = pager;
  206. return list.value.slice((page - 1) * size, page * size);
  207. });
  208. // 已加载列表的 id
  209. const loadIds = ref<number[]>([]);
  210. // 监听已选列表
  211. function onSelectionChange(arr: Item[]) {
  212. // 已加载的
  213. const ids = Array.from(new Set(loadIds.value));
  214. // 过滤掉已加载的,再加上已选的
  215. selection.value = selection.value.filter((e) => !ids.includes(e.id!)).concat(...arr);
  216. }
  217. // 打开选择弹窗
  218. function open() {
  219. visible.value = true;
  220. // 清空数据
  221. loadIds.value = [];
  222. // 设置已选
  223. selection.value = cloneDeep(list.value);
  224. nextTick(() => {
  225. refresh({
  226. size: 10
  227. });
  228. });
  229. }
  230. // 关闭选择弹窗
  231. function close() {
  232. visible.value = false;
  233. }
  234. // 设置值
  235. function set(data: Item[] | Item) {
  236. list.value = cloneDeep(isArray(data) ? data : [data]);
  237. }
  238. // 选择
  239. function select(item?: Item) {
  240. // 单选不触发 onSelectionChange 手动设置
  241. if (item) {
  242. selection.value = [item];
  243. }
  244. list.value = cloneDeep(selection.value || []);
  245. close();
  246. }
  247. // 全选
  248. async function selectAll() {
  249. // 全部数据
  250. await Crud.value?.refresh({ page: 1, size: 10000 }).then((res) => {
  251. list.value = res.list;
  252. });
  253. // 当前页数据
  254. // list.value = Table.value?.data || [];
  255. close();
  256. }
  257. // 移除
  258. function remove() {
  259. const ids = selection.value.map((e) => e.id);
  260. list.value = list.value.filter((e) => {
  261. // 清空选择状态
  262. refs.table?.toggleRowSelection(e, false);
  263. // 移除已选的
  264. return !ids.find((id) => id == e.id);
  265. });
  266. }
  267. // 监听已选列表,返回 ids/id
  268. watch(
  269. list,
  270. (arr = []) => {
  271. const ids = arr.map((e) => e.id);
  272. if (props.multiple) {
  273. emit("update:modelValue", ids);
  274. } else {
  275. emit("update:modelValue", ids[0]);
  276. }
  277. Form.value?.validateField(props.prop);
  278. },
  279. {
  280. deep: true
  281. }
  282. );
  283. defineExpose({
  284. set,
  285. remove,
  286. select,
  287. selectAll
  288. });
  289. </script>
  290. <style lang="scss" scoped>
  291. .user-select__inner {
  292. .btns {
  293. margin-bottom: 10px;
  294. }
  295. .user {
  296. display: flex;
  297. align-items: center;
  298. border: 1px solid var(--el-border-color);
  299. border-radius: var(--el-border-radius-base);
  300. padding: 0 10px;
  301. height: 32px;
  302. cursor: pointer;
  303. position: relative;
  304. .cl-avatar {
  305. margin-right: 6px;
  306. }
  307. .el-icon,
  308. .placeholder {
  309. color: var(--el-text-color-placeholder);
  310. }
  311. .el-icon {
  312. position: absolute;
  313. right: 10px;
  314. }
  315. &:hover {
  316. border-color: var(--el-color-primary);
  317. }
  318. }
  319. }
  320. </style>