selectPersonnel.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. <template>
  2. <el-select v-model="selectPersonnelValue" filterable @visible-change="toggleSelectPrefix" remote
  3. :remote-method="userListRemotemethod" :multiple="multiple" @focus="userFocus" :size="size" collapse-tags
  4. @change="updateValue" clearable :ref="`select${timeRef}`" :style="`width: ${width}`" class="custom-select">
  5. <template #prefix>
  6. <div style="height: 100%;display: flex;align-items: center;">
  7. <!-- 单选 -->
  8. <template v-if="!multiple">
  9. <div v-if="selectPrefixFlg" class="selectSingleChoice" :style="sizeStyle[size]">
  10. <template v-if="getSelectedLabel == '请选择'">
  11. <span class="pleaseChoose">请选择</span>
  12. </template>
  13. <template v-else>
  14. <!-- {{ getSelectedLabel }} -->
  15. <TranslationOpenDataText type='userName' :openid="getSelectedLabel"></TranslationOpenDataText>
  16. </template>
  17. </div>
  18. </template>
  19. <!-- 多选 -->
  20. <template v-if="multiple">
  21. <div class="selectMultiple" :style="sizeStyle[size]">
  22. <template v-if="value.length == 0">
  23. <span class="textSpan pleaseChoose">请选择</span>
  24. </template>
  25. <template v-else>
  26. <el-tag closable type="info" :size="size" class="narrow" style="margin: 0;" @close="userTagClose">
  27. {{
  28. getSelectedLabel
  29. }}
  30. <TranslationOpenDataText type='userName' :openid="getSelectedLabel"></TranslationOpenDataText>
  31. </el-tag>
  32. <el-tag type="info" :size="size" v-if="value.length > 1" class="narrow" style="margin: 0;">+ {{
  33. value.length - 1 }}</el-tag>
  34. </template>
  35. </div>
  36. </template>
  37. <!-- 占位符 -->
  38. <span></span>
  39. </div>
  40. </template>
  41. <!-- 主体显示内容 -->
  42. <div :ref="`mySelectUser${timeRef}`" class="select-user-class">
  43. <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
  44. <span class="floatLeft">
  45. <TranslationOpenDataText type='userName' :openid='item.name'></TranslationOpenDataText>
  46. </span>
  47. <span class="jonumberUser">{{ item.jobNumber }}</span>
  48. </el-option>
  49. <!-- 下滚加载提示 -->
  50. <div class="itemsLoading" v-if="loadingInProgress">
  51. 加载中...
  52. </div>
  53. </div>
  54. </el-select>
  55. </template>
  56. <script>
  57. export default {
  58. name: 'HelloWorld',
  59. props: {
  60. value: { // 双向数据绑定
  61. type: [String, Number, Array],
  62. required: true
  63. },
  64. size: { // 尺寸
  65. type: String,
  66. default: 'small'
  67. },
  68. multiple: { // 是否多选
  69. type: Boolean,
  70. default: () => false
  71. },
  72. width: { // 宽度
  73. type: String,
  74. default: '200px'
  75. }
  76. },
  77. data() {
  78. return {
  79. options: [],
  80. selectPersonnelValue: this.value,
  81. selectPrefixFlg: true,
  82. timeRef: new Date().getTime(),
  83. updateTrigger: 0, // 用来强制触发计算属性
  84. sizeStyle: {
  85. mini: `height: 24px;`,
  86. small: `height: 28px`,
  87. medium: `height: 32px`,
  88. },
  89. keyword: '',
  90. loadingInProgress: false,
  91. userListloading: false,
  92. userPageIndex: 1,
  93. userPageSize: 50,
  94. userTotal: 0,
  95. cursor: 1,
  96. }
  97. },
  98. watch: {
  99. selectPersonnelValue: {
  100. handler() {
  101. this.$nextTick(() => {
  102. if (this.multiple) {
  103. this.getSelectedLabel
  104. }
  105. });
  106. },
  107. deep: true,
  108. immediate: true
  109. }
  110. },
  111. computed: {
  112. getSelectedLabel() {
  113. this.updateTrigger;
  114. if (!this.multiple) {
  115. const selectedOption = this.options.find(item => item.id === this.selectPersonnelValue)
  116. return selectedOption ? selectedOption.name : '请选择'
  117. } else if (this.multiple) {
  118. if (this.selectPersonnelValue.length === 0) {
  119. return '请选择'
  120. }
  121. const selectVal = this.selectPersonnelValue[0]
  122. const selectedOption = this.options.find(item => item.id === selectVal)
  123. return selectedOption ? selectedOption.name : '请选择'
  124. }
  125. return '请选择'
  126. }
  127. },
  128. methods: {
  129. userListRemotemethod: _.debounce(function (val) {
  130. this.newProjectListPage = 1
  131. this.getUserList(val)
  132. }, 500),
  133. // 随机取名字4-6个字
  134. getRandomName() {
  135. const firstNames = ['张', '李', '王', '刘', '陈', '杨', '黄', '赵'];
  136. const lastNames = ['伟', '芳', '娜', '敏', '军', '洋', '静', '磊'];
  137. const nameLength = Math.floor(Math.random() * 3) + 4; // 生成4到6之间的随机数
  138. let name = '';
  139. for (let i = 0; i < nameLength; i++) {
  140. if (i % 2 === 0) {
  141. name += firstNames[Math.floor(Math.random() * firstNames.length)];
  142. } else {
  143. name += lastNames[Math.floor(Math.random() * lastNames.length)];
  144. }
  145. }
  146. return name;
  147. },
  148. uniqueById(arr) {
  149. return arr.reduce((accumulator, current) => {
  150. if (!accumulator.some(item => item.id === current.id)) {
  151. accumulator.push(current);
  152. }
  153. return accumulator;
  154. }, []);
  155. },
  156. userTagClose() {
  157. this.selectPersonnelValue = this.selectPersonnelValue.splice(0, 1)
  158. },
  159. userFocus() {
  160. console.log('失去焦点了', this.selectPrefixFlg)
  161. if (this.selectPrefixFlg) {
  162. this.userPageIndex = 1
  163. this.getUserList('', false)
  164. }
  165. },
  166. addUserList() {
  167. this.loadingInProgress = true
  168. this.postData(`/user/getSimpleActiveUserListPage`, {
  169. pageIndex: this.userPageIndex,
  170. pageSize: this.userPageSize,
  171. departmentId: '',
  172. keyword: this.keyword,
  173. cursor: '',
  174. userIds: this.multiple ? this.selectPersonnelValue.join(',') : this.selectPersonnelValue
  175. }).then(res => {
  176. const { data = [], total = 0 } = res.data
  177. this.userTotal = total
  178. // this.options = [...new Set([...this.options, ...data].map(JSON.stringify))].map(JSON.parse);
  179. // this.options = this.uniqueById([...this.options, ...newData])
  180. const newData = data.map((item) => {
  181. return {
  182. ...item,
  183. // name: this.getRandomName()
  184. }
  185. })
  186. this.options = this.uniqueById([...this.options, ...newData])
  187. console.log(this.options.length, '<==== this.options')
  188. }).finally(() => {
  189. setTimeout(() => {
  190. this.loadingInProgress = false
  191. }, 500)
  192. })
  193. },
  194. getUserList(keyword = '', flag = true) {
  195. this.keyword = keyword
  196. this.userListloading = flag
  197. this.postData(`/user/getSimpleActiveUserListPage`, {
  198. pageIndex: this.userPageIndex,
  199. pageSize: this.userPageSize,
  200. departmentId: '',
  201. keyword,
  202. cursor: '',
  203. userIds: this.multiple ? this.selectPersonnelValue.join(',') : this.selectPersonnelValue
  204. }).then(res => {
  205. const { data = [], total = 0 } = res.data
  206. this.userTotal = total
  207. // this.options = [...new Set(data.map(JSON.stringify))].map(JSON.parse);
  208. // this.options = this.uniqueById(data);
  209. const dataVal = data.map((item) => {
  210. return {
  211. ...item,
  212. // name: this.getRandomName()
  213. }
  214. })
  215. this.options = this.uniqueById(dataVal);
  216. }).finally(() => {
  217. this.userListloading = false
  218. })
  219. },
  220. async postData(urls, param) { // 单独封装 post 请求 方法
  221. return new Promise((resolve, reject) => {
  222. this.http.post(urls, { ...param },
  223. res => {
  224. if (res.code == 'ok') {
  225. resolve(res)
  226. } else {
  227. this.$message({
  228. message: res.msg,
  229. type: 'error'
  230. })
  231. reject(res)
  232. }
  233. resolve(res)
  234. },
  235. error => {
  236. this.$message({
  237. message: error,
  238. type: "error"
  239. });
  240. reject(error)
  241. }
  242. )
  243. });
  244. },
  245. handleScroll(event) {
  246. const container = event.target;
  247. const { scrollTop, scrollHeight, clientHeight } = container;
  248. const totalPage = Math.ceil(this.userTotal / this.userPageSize);
  249. if (scrollTop + clientHeight >= scrollHeight - 20 && this.userPageIndex < totalPage && !this.loadingInProgress) {
  250. this.loadMoreData()
  251. }
  252. },
  253. loadMoreData() {
  254. this.userPageIndex += 1;
  255. this.addUserList();
  256. },
  257. toggleSelectPrefix(value) {
  258. this.selectPrefixFlg = !value
  259. console.log(this.selectPrefixFlg, '<===== selectPrefixFlg')
  260. if (value) {
  261. this.$nextTick(() => {
  262. this.addScrollListener();
  263. });
  264. } else {
  265. this.removeScrollListener();
  266. }
  267. },
  268. addScrollListener() {
  269. const container = this.$refs[`mySelectUser${this.timeRef}`];
  270. const scrollbar = container.closest('.el-scrollbar__wrap');
  271. if (scrollbar) {
  272. this.loadingInProgressHight = scrollbar.clientHeight - 40
  273. this.removeScrollListener();
  274. const debouncedHandleScroll = _.debounce(this.handleScroll, 200);
  275. scrollbar.addEventListener('scroll', debouncedHandleScroll);
  276. this.scrollListener = debouncedHandleScroll;
  277. }
  278. },
  279. removeScrollListener() {
  280. const container = this.$refs[`mySelectUser${this.timeRef}`];
  281. const scrollbar = container.closest('.el-scrollbar__wrap');
  282. if (scrollbar && this.scrollListener) {
  283. scrollbar.removeEventListener('scroll', this.scrollListener);
  284. this.scrollListener = null;
  285. }
  286. },
  287. updateValue(value) { // 更新数据
  288. if (this.multiple) {
  289. this.updateTrigger += 1 // 强行触发计算属性
  290. }
  291. this.$emit('input', value);
  292. this.$emit('change', value, this.getSelectedLabel);
  293. },
  294. },
  295. mounted() {
  296. this.$nextTick(() => {
  297. this.getUserList()
  298. });
  299. },
  300. beforeDestroy() {
  301. this.removeScrollListener();
  302. }
  303. }
  304. </script>
  305. <style>
  306. .custom-select input::placeholder {
  307. color: transparent !important;
  308. }
  309. </style>
  310. <style scoped lang="scss">
  311. .select-base {
  312. position: absolute;
  313. width: auto;
  314. display: flex;
  315. align-items: center;
  316. background: #fff;
  317. box-sizing: border-box;
  318. color: #606266;
  319. white-space: nowrap;
  320. }
  321. .selectSingleChoice {
  322. @extend .select-base;
  323. left: 26px;
  324. transform-origin: left;
  325. }
  326. .selectMultiple {
  327. @extend .select-base;
  328. left: 0;
  329. z-index: 10;
  330. min-width: 40px;
  331. }
  332. .select-user-class {
  333. position: relative;
  334. .itemsLoading {
  335. position: absolute;
  336. left: 0;
  337. bottom: -2px;
  338. width: 100%;
  339. text-align: center;
  340. padding: 10px;
  341. background: #fff;
  342. box-shadow: 0px -4px 20px 0px #999;
  343. font-size: 12px;
  344. color: #999;
  345. z-index: 99;
  346. box-sizing: border-box;
  347. }
  348. }
  349. .narrow {
  350. transform: scale(0.9);
  351. transform-origin: left;
  352. }
  353. .selectMultiple .textSpan {
  354. position: relative;
  355. left: 26px;
  356. }
  357. .pleaseChoose {
  358. color: #C0C4CC;
  359. }
  360. .floatLeft {
  361. float: left;
  362. }
  363. .jonumberUser {
  364. float: right;
  365. color: #8492a6;
  366. }
  367. </style>