WorkPlaceChoose.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <view style="width: 100%;">
  3. <u-action-sheet :show="show" @close="onClose">
  4. <view class="cu-bar bg-white">
  5. <view class="action text-blue" @tap="onClose">取消</view>
  6. <view class="action text-green" @tap="selectUsers">确定</view>
  7. </view>
  8. <view style="max-height: 300px; overflow-y: auto;">
  9. <view v-for="item in data" :key="item.id" style="padding: 10px;">
  10. <view @tap="onItemClick(item)" style="display: flex; align-items: center;">
  11. <view style="flex: 1;text-align: left; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{{ item.name }}</view>
  12. <view v-if="item.checked" style="color: #409eff;">已选择</view>
  13. </view>
  14. </view>
  15. </view>
  16. <view v-if="!allDataLoaded && !loading" style="text-align: center; padding: 10px;">
  17. <button class="load-more-button" @tap="loadMore">加载更多</button>
  18. </view>
  19. <view v-if="loading" style="text-align: center; padding: 10px;">
  20. Loading...
  21. </view>
  22. </u-action-sheet>
  23. </view>
  24. </template>
  25. <script>
  26. import workClientService from '@/api/cw/workClientInfo/WorkClientService'
  27. export default {
  28. data() {
  29. return {
  30. show: false,
  31. labels:'',
  32. data: [],
  33. localMode: '', // 声明一个本地数据属性
  34. allDataLoaded: false, // 跟踪是否所有数据都已加载
  35. loading: false, // 跟踪加载状态
  36. pageSize: 10, // 每页获取的项目数
  37. currentPage: 1 // 当前页数
  38. };
  39. },
  40. props: {
  41. value: String,
  42. placeholder: {
  43. type: String,
  44. default: '请选择客户'
  45. },
  46. readonly: {
  47. type: Boolean,
  48. default: false
  49. },
  50. disabled: {
  51. type: Boolean,
  52. default: false
  53. },
  54. },
  55. mounted() {
  56. // this.loadData();
  57. },
  58. methods: {
  59. init() {
  60. this.pageSize = 10
  61. this.currentPage = 1
  62. this.show = true;
  63. //选项的选择模式,单选或多选,默认为多选
  64. this.localMode = 'single'
  65. this.loadData();
  66. },
  67. onItemClick(item) {
  68. if (this.localMode === 'single') {
  69. this.data.forEach(node => {
  70. node.checked = node === item;
  71. });
  72. } else {
  73. item.checked = !item.checked;
  74. }
  75. },
  76. selectUsers() {
  77. if (this.data.some(item => item.checked)) {
  78. let checkedItems = this.data.filter(item => item.checked)[0]; // 获取第一个符合条件的元素
  79. this.$emit('input', checkedItems);
  80. this.onClose()
  81. } else {
  82. uni.showToast({
  83. title: '请至少选择一条数据',
  84. icon: 'none',
  85. duration: 2000
  86. });
  87. }
  88. },
  89. loadData() {
  90. this.loading = true; // 开始加载数据,显示loading状态
  91. workClientService.listTree({ isTrue: '1', current: this.currentPage, size: this.pageSize })
  92. .then(data => {
  93. this.loading = false; // 数据加载完成,隐藏loading状态
  94. // 检查新返回的数据是否已经存在,如果存在,则不添加到原始数据数组中
  95. let newData = data.records.filter(record => !this.data.some(item => item.id === record.id));
  96. if (this.currentPage === 1) {
  97. // 如果是加载第一页,则直接赋值给 data
  98. this.data = newData.map(item => ({ ...item, checked: false }));
  99. } else {
  100. // 如果不是第一页,则追加到原始数据数组后面
  101. this.data = [...this.data, ...newData.map(item => ({ ...item, checked: false }))];
  102. }
  103. if (data.records.length < this.pageSize) {
  104. this.allDataLoaded = true; // 如果返回的数据少于每页数量,则表示所有数据都已加载
  105. }
  106. if (this.value) {
  107. let keys = this.value.split(',');
  108. this.data.forEach(node => {
  109. if (keys.includes(node.id)) {
  110. node.checked = true;
  111. }
  112. });
  113. this.labels = this.data.filter(node => node.checked).map(node => node.label).join(',');
  114. }
  115. })
  116. .catch(e => {
  117. this.loading = false; // 数据加载失败,隐藏loading状态
  118. throw e;
  119. });
  120. },
  121. loadMore() {
  122. this.currentPage++; // 增加当前页数
  123. this.loadData(); // 加载更多数据
  124. },
  125. onClose() {
  126. // 在关闭操作中清除已选择标记
  127. this.data.forEach(item => {
  128. item.checked = false;
  129. });
  130. this.labels = ''; // 清空标签
  131. this.show = false;
  132. this.data = []; // 清空原始数据
  133. },
  134. }
  135. };
  136. </script>
  137. <style scoped>
  138. .load-more-button {
  139. background-color: #409eff;
  140. color: #fff;
  141. border: none;
  142. padding: 10px 20px;
  143. border-radius: 4px;
  144. cursor: pointer;
  145. }
  146. </style>