CwProjectChoose.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.projectName }}</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 projectRecordsService from '@/api/cw/projectRecords/ProjectRecordsService'
  27. export default {
  28. data() {
  29. return {
  30. show: false,
  31. labels:'',
  32. index:'',
  33. type:'',
  34. data: [],
  35. localMode: '', // 声明一个本地数据属性
  36. allDataLoaded: false, // 跟踪是否所有数据都已加载
  37. loading: false, // 跟踪加载状态
  38. pageSize: 10, // 每页获取的项目数
  39. currentPage: 1 // 当前页数
  40. };
  41. },
  42. props: {
  43. value: String,
  44. placeholder: {
  45. type: String,
  46. default: '请选择项目'
  47. },
  48. readonly: {
  49. type: Boolean,
  50. default: false
  51. },
  52. disabled: {
  53. type: Boolean,
  54. default: false
  55. },
  56. },
  57. mounted() {
  58. // this.loadData();
  59. },
  60. methods: {
  61. init(index,type) {
  62. this.pageSize = 10
  63. this.currentPage = 1
  64. this.show = true;
  65. this.index = index;
  66. this.type = type;
  67. //选项的选择模式,单选或多选,默认为多选
  68. if (type === 'report') {
  69. this.localMode = 'single'
  70. } else {
  71. this.localMode = 'multiple'
  72. }
  73. this.loadData();
  74. },
  75. onItemClick(item) {
  76. if (this.localMode === 'single') {
  77. this.data.forEach(node => {
  78. node.checked = node === item;
  79. });
  80. } else {
  81. item.checked = !item.checked;
  82. }
  83. },
  84. selectUsers() {
  85. if (this.data.some(item => item.checked)) {
  86. let ids = this.data.filter(item => item.checked).map(item => item.id).join(',');
  87. let projectNames = this.data.filter(item => item.checked).map(item => item.projectName).join(',');
  88. this.labels = projectNames;
  89. this.$emit('input', ids, projectNames, this.index, this.type);
  90. this.onClose()
  91. } else {
  92. uni.showToast({
  93. title: '请至少选择一条数据',
  94. icon: 'none',
  95. duration: 2000
  96. });
  97. }
  98. },
  99. loadData() {
  100. this.loading = true; // 开始加载数据,显示loading状态
  101. projectRecordsService.list({ status: 5, current: this.currentPage, pageSize: this.pageSize })
  102. .then(data => {
  103. this.loading = false; // 数据加载完成,隐藏loading状态
  104. // 检查新返回的数据是否已经存在,如果存在,则不添加到原始数据数组中
  105. let newData = data.records.filter(record => !this.data.some(item => item.id === record.id));
  106. if (this.currentPage === 1) {
  107. // 如果是加载第一页,则直接赋值给 data
  108. this.data = newData.map(item => ({ ...item, checked: false }));
  109. } else {
  110. // 如果不是第一页,则追加到原始数据数组后面
  111. this.data = [...this.data, ...newData.map(item => ({ ...item, checked: false }))];
  112. }
  113. if (data.records.length < this.pageSize) {
  114. this.allDataLoaded = true; // 如果返回的数据少于每页数量,则表示所有数据都已加载
  115. }
  116. if (this.value) {
  117. let keys = this.value.split(',');
  118. this.data.forEach(node => {
  119. if (keys.includes(node.id)) {
  120. node.checked = true;
  121. }
  122. });
  123. this.labels = this.data.filter(node => node.checked).map(node => node.label).join(',');
  124. }
  125. })
  126. .catch(e => {
  127. this.loading = false; // 数据加载失败,隐藏loading状态
  128. throw e;
  129. });
  130. },
  131. loadMore() {
  132. this.currentPage++; // 增加当前页数
  133. this.loadData(); // 加载更多数据
  134. },
  135. onClose() {
  136. // 在关闭操作中清除已选择标记
  137. this.data.forEach(item => {
  138. item.checked = false;
  139. });
  140. this.labels = ''; // 清空标签
  141. this.show = false;
  142. this.data = []; // 清空原始数据
  143. },
  144. }
  145. };
  146. </script>
  147. <style scoped>
  148. .load-more-button {
  149. background-color: #409eff;
  150. color: #fff;
  151. border: none;
  152. padding: 10px 20px;
  153. border-radius: 4px;
  154. cursor: pointer;
  155. }
  156. </style>