select.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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: null,
  93. isDisabled: Boolean,
  94. prop: String,
  95. scope: Object as PropType<Item>,
  96. disabled: Boolean,
  97. // 是否多选
  98. multiple: {
  99. type: Boolean,
  100. default: true
  101. }
  102. });
  103. const emit = defineEmits(["update:modelValue"]);
  104. const { service, refs, setRefs } = useCool();
  105. // 上级表单
  106. const Form = useForm();
  107. // 选项
  108. const options = reactive({
  109. status: [
  110. {
  111. label: "启用",
  112. value: 1,
  113. type: "success"
  114. },
  115. {
  116. label: "禁用",
  117. value: 0,
  118. type: "danger"
  119. }
  120. ]
  121. });
  122. // cl-table
  123. const Table = useTable({
  124. contextMenu: [],
  125. columns: [
  126. props.multiple
  127. ? {
  128. type: "selection",
  129. width: 60,
  130. reserveSelection: true
  131. }
  132. : {
  133. label: "操作",
  134. prop: "check",
  135. width: 100
  136. },
  137. {
  138. prop: "avatarUrl",
  139. label: "头像",
  140. component: {
  141. name: "cl-avatar"
  142. },
  143. minWidth: 100
  144. },
  145. {
  146. prop: "phone",
  147. label: "手机号",
  148. minWidth: 120
  149. },
  150. {
  151. prop: "nickName",
  152. label: "姓名",
  153. minWidth: 150
  154. },
  155. {
  156. label: "状态",
  157. prop: "status",
  158. minWidth: 100,
  159. dict: options.status
  160. },
  161. {
  162. label: "创建时间",
  163. prop: "createTime",
  164. sortable: "desc",
  165. minWidth: 170
  166. }
  167. ]
  168. });
  169. // cl-crud
  170. const Crud = useCrud({
  171. service: service.user.info,
  172. async onRefresh(params, { next }) {
  173. const res = await next(params);
  174. // 添加已加载列表的 id
  175. loadIds.value.push(...res.list.map((e) => e.id));
  176. // 数据反选
  177. selection.value.forEach((e) => {
  178. const d = Table.value?.data.find((a) => a.id == e.id);
  179. if (d) {
  180. Table.value?.toggleRowSelection(d, true);
  181. }
  182. });
  183. }
  184. });
  185. // 刷新
  186. async function refresh(params?: any) {
  187. return Crud.value?.refresh(params);
  188. }
  189. // 弹窗是否可见
  190. const visible = ref(false);
  191. // 已选的数据列表,双向绑定用
  192. const list = ref<Item[]>([]);
  193. // 已选列表
  194. const selection = ref<any[]>([]);
  195. // 分页
  196. const pager = reactive({
  197. page: 1,
  198. size: 10
  199. });
  200. // 分页数据
  201. const data = computed(() => {
  202. const { page, size } = pager;
  203. return list.value.slice((page - 1) * size, page * size);
  204. });
  205. // 已加载列表的 id
  206. const loadIds = ref<number[]>([]);
  207. // 监听已选列表
  208. function onSelectionChange(arr: Item[]) {
  209. // 已加载的
  210. const ids = Array.from(new Set(loadIds.value));
  211. // 过滤掉已加载的,再加上已选的
  212. selection.value = selection.value.filter((e) => !ids.includes(e.id!)).concat(...arr);
  213. }
  214. // 打开选择弹窗
  215. function open() {
  216. visible.value = true;
  217. // 清空数据
  218. loadIds.value = [];
  219. // 设置已选
  220. selection.value = cloneDeep(list.value);
  221. nextTick(() => {
  222. refresh({
  223. size: 10
  224. });
  225. });
  226. }
  227. // 关闭选择弹窗
  228. function close() {
  229. visible.value = false;
  230. }
  231. // 设置值
  232. function set(data: Item[] | Item) {
  233. list.value = cloneDeep(isArray(data) ? data : [data]);
  234. }
  235. // 选择
  236. function select(item?: Item) {
  237. // 单选不触发 onSelectionChange 手动设置
  238. if (item) {
  239. selection.value = [item];
  240. }
  241. list.value = cloneDeep(selection.value || []);
  242. close();
  243. }
  244. // 全选
  245. async function selectAll() {
  246. // 全部数据
  247. await Crud.value?.refresh({ page: 1, size: 10000 }).then((res) => {
  248. list.value = res.list;
  249. });
  250. // 当前页数据
  251. // list.value = Table.value?.data || [];
  252. close();
  253. }
  254. // 移除
  255. function remove() {
  256. const ids = selection.value.map((e) => e.id);
  257. list.value = list.value.filter((e) => {
  258. // 清空选择状态
  259. refs.table?.toggleRowSelection(e, false);
  260. // 移除已选的
  261. return !ids.find((id) => id == e.id);
  262. });
  263. }
  264. // 监听已选列表,返回 ids/id
  265. watch(
  266. list,
  267. (arr = []) => {
  268. const ids = arr.map((e) => e.id);
  269. if (props.multiple) {
  270. emit("update:modelValue", ids);
  271. } else {
  272. emit("update:modelValue", ids[0]);
  273. }
  274. Form.value?.validateField(props.prop);
  275. },
  276. {
  277. deep: true
  278. }
  279. );
  280. defineExpose({
  281. set,
  282. remove,
  283. select,
  284. selectAll
  285. });
  286. </script>
  287. <style lang="scss" scoped>
  288. .user-select__inner {
  289. .btns {
  290. margin-bottom: 10px;
  291. }
  292. .user {
  293. display: flex;
  294. align-items: center;
  295. border: 1px solid var(--el-border-color);
  296. border-radius: var(--el-border-radius-base);
  297. padding: 0 10px;
  298. height: 32px;
  299. cursor: pointer;
  300. position: relative;
  301. .cl-avatar {
  302. margin-right: 6px;
  303. }
  304. .el-icon,
  305. .placeholder {
  306. color: var(--el-text-color-placeholder);
  307. }
  308. .el-icon {
  309. position: absolute;
  310. right: 10px;
  311. }
  312. &:hover {
  313. border-color: var(--el-color-primary);
  314. }
  315. }
  316. }
  317. </style>