WorkOverChoose.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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.no }}</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 overService from '@/api/garbageClearance/overService'
  27. export default {
  28. data() {
  29. return {
  30. checkType: '1',
  31. show: false,
  32. labels:'',
  33. index:'',
  34. type:'',
  35. data: [],
  36. localMode: '', // 声明一个本地数据属性
  37. allDataLoaded: false, // 跟踪是否所有数据都已加载
  38. loading: false, // 跟踪加载状态
  39. pageSize: 10, // 每页获取的项目数
  40. currentPage: 1 // 当前页数
  41. };
  42. },
  43. props: {
  44. value: String,
  45. placeholder: {
  46. type: String,
  47. default: '请选择项目'
  48. },
  49. readonly: {
  50. type: Boolean,
  51. default: false
  52. },
  53. disabled: {
  54. type: Boolean,
  55. default: false
  56. },
  57. },
  58. mounted() {
  59. // this.loadData();
  60. },
  61. methods: {
  62. init(index) {
  63. this.pageSize = 10
  64. this.currentPage = 1
  65. this.show = true;
  66. this.index = index;
  67. this.localMode = 'multiple'
  68. this.loadData();
  69. },
  70. onItemClick(item) {
  71. if (this.localMode === 'single') {
  72. this.data.forEach(node => {
  73. node.checked = node === item;
  74. });
  75. } else {
  76. item.checked = !item.checked;
  77. }
  78. },
  79. selectUsers() {
  80. if (this.data.some(item => item.checked)) {
  81. let checkedItems = this.data.filter(item => item.checked).map(item => {
  82. return {
  83. id: item.id,
  84. no: item.no,
  85. };
  86. });
  87. this.$emit('input', checkedItems, this.index);
  88. this.onClose()
  89. } else {
  90. uni.showToast({
  91. title: '请至少选择一条数据',
  92. icon: 'none',
  93. duration: 2000
  94. });
  95. }
  96. },
  97. loadData() {
  98. this.loading = true; // 开始加载数据,显示loading状态
  99. console.log(3213123)
  100. overService.getNotDisposeList({ status: 1, current: this.currentPage, pageSize: this.pageSize })
  101. .then(data => {
  102. this.loading = false; // 数据加载完成,隐藏loading状态
  103. // 检查新返回的数据是否已经存在,如果存在,则不添加到原始数据数组中
  104. let newData = data.records.filter(record => !this.data.some(item => item.id === record.id));
  105. console.log(newData)
  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. checkTypeChange(value){
  145. this.pageSize = 10
  146. this.currentPage = 1
  147. this.data = []; // 清空原始数据
  148. this.loadData(); // 加载更多数据
  149. },
  150. isEmpty(value) {
  151. let result = false;
  152. if (value == null || value == undefined) {
  153. result = true;
  154. }
  155. if (typeof value == 'string' && (value.replace(/\s+/g, "") == "" || value == "")) {
  156. result = true;
  157. }
  158. if (typeof value == "object" && value instanceof Array && value.length === 0) {
  159. result = true;
  160. }
  161. return result;
  162. },
  163. isNotEmpty (value) {
  164. return !this.isEmpty(value)
  165. },
  166. }
  167. };
  168. </script>
  169. <style scoped>
  170. .load-more-button {
  171. background-color: #409eff;
  172. color: #fff;
  173. border: none;
  174. padding: 10px 20px;
  175. border-radius: 4px;
  176. cursor: pointer;
  177. }
  178. </style>