InvoiceProjectChoose.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 v-if="isShow">
  7. <u-radio-group v-model="checkType" >
  8. <u-radio
  9. :customStyle="{marginRight: '16px'}"
  10. v-for="(item, index) in checkTypeList"
  11. :key="index"
  12. :label="item.name"
  13. :name="item.value"
  14. @change="checkTypeChange"
  15. ></u-radio>
  16. </u-radio-group>
  17. </view>
  18. <view class="action text-green" @tap="selectUsers">确定</view>
  19. </view>
  20. <view style="max-height: 300px; overflow-y: auto;">
  21. <view v-for="item in data" :key="item.id" style="padding: 10px;">
  22. <view @tap="onItemClick(item)" style="display: flex; align-items: center;">
  23. <view style="flex: 1;text-align: left; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{{ item.projectName }}</view>
  24. <view v-if="item.checked" style="color: #409eff;">已选择</view>
  25. </view>
  26. </view>
  27. </view>
  28. <view v-if="!allDataLoaded && !loading" style="text-align: center; padding: 10px;">
  29. <button class="load-more-button" @tap="loadMore">加载更多</button>
  30. </view>
  31. <view v-if="loading" style="text-align: center; padding: 10px;">
  32. Loading...
  33. </view>
  34. </u-action-sheet>
  35. </view>
  36. </template>
  37. <script>
  38. import projectRecordsService from '@/api/cw/projectRecords/ProjectRecordsService'
  39. export default {
  40. data() {
  41. return {
  42. checkType: '1',
  43. checkTypeList: [{
  44. name: '项目',
  45. value: '1',
  46. disabled: false
  47. },{
  48. name: '报告',
  49. value: '2',
  50. disabled: false
  51. }],
  52. show: false,
  53. isShow: false,
  54. labels:'',
  55. index:'',
  56. type:'',
  57. data: [],
  58. localMode: '', // 声明一个本地数据属性
  59. allDataLoaded: false, // 跟踪是否所有数据都已加载
  60. loading: false, // 跟踪加载状态
  61. pageSize: 10, // 每页获取的项目数
  62. currentPage: 1 // 当前页数
  63. };
  64. },
  65. props: {
  66. value: String,
  67. placeholder: {
  68. type: String,
  69. default: '请选择项目'
  70. },
  71. readonly: {
  72. type: Boolean,
  73. default: false
  74. },
  75. disabled: {
  76. type: Boolean,
  77. default: false
  78. },
  79. },
  80. mounted() {
  81. // this.loadData();
  82. },
  83. methods: {
  84. init(index, type) {
  85. this.pageSize = 10
  86. this.currentPage = 1
  87. this.show = true;
  88. this.index = index;
  89. //选项的选择模式,单选或多选,默认为多选
  90. // if (type === 'report') {
  91. // this.localMode = 'single'
  92. // } else {
  93. // this.localMode = 'multiple'
  94. // }
  95. this.localMode = 'multiple'
  96. if (this.isNotEmpty(type)) {
  97. this.checkType = type
  98. this.isShow = false
  99. } else {
  100. this.isShow = true
  101. }
  102. if (index === 1 || index === '1') {
  103. if (type === '1') {
  104. uni.showToast({
  105. title: '第一条为项目后面新增只能选择项目',
  106. icon: 'none',
  107. duration: 2000
  108. });
  109. } else if (type === '2') {
  110. uni.showToast({
  111. title: '第一条为报告后面新增只能选择报告',
  112. icon: 'none',
  113. duration: 2000
  114. });
  115. }
  116. }
  117. this.loadData();
  118. },
  119. onItemClick(item) {
  120. if (this.localMode === 'single') {
  121. this.data.forEach(node => {
  122. node.checked = node === item;
  123. });
  124. } else {
  125. item.checked = !item.checked;
  126. }
  127. },
  128. selectUsers() {
  129. if (this.data.some(item => item.checked)) {
  130. let checkedItems = this.data.filter(item => item.checked).map(item => {
  131. return {
  132. id: item.id,
  133. projectName: item.projectName,
  134. contractName: item.contractName,
  135. projectNumber: item.projectNumber,
  136. contractId: item.contractId,
  137. reportNo: item.reportNo,
  138. isPreInvoice: item.isPreInvoice,
  139. };
  140. });
  141. this.$emit('input', checkedItems, this.index);
  142. this.onClose()
  143. } else {
  144. uni.showToast({
  145. title: '请至少选择一条数据',
  146. icon: 'none',
  147. duration: 2000
  148. });
  149. }
  150. },
  151. loadData() {
  152. this.loading = true; // 开始加载数据,显示loading状态
  153. if (this.checkType === '1') {
  154. projectRecordsService.noReportList({ status: 5, current: this.currentPage, pageSize: this.pageSize })
  155. .then(data => {
  156. this.loading = false; // 数据加载完成,隐藏loading状态
  157. // 检查新返回的数据是否已经存在,如果存在,则不添加到原始数据数组中
  158. let newData = data.records.filter(record => !this.data.some(item => item.id === record.id));
  159. if (this.currentPage === 1) {
  160. // 如果是加载第一页,则直接赋值给 data
  161. this.data = newData.map(item => ({ ...item, checked: false }));
  162. } else {
  163. // 如果不是第一页,则追加到原始数据数组后面
  164. this.data = [...this.data, ...newData.map(item => ({ ...item, checked: false }))];
  165. }
  166. if (data.records.length < this.pageSize) {
  167. this.allDataLoaded = true; // 如果返回的数据少于每页数量,则表示所有数据都已加载
  168. }
  169. if (this.value) {
  170. let keys = this.value.split(',');
  171. this.data.forEach(node => {
  172. if (keys.includes(node.id)) {
  173. node.checked = true;
  174. }
  175. });
  176. this.labels = this.data.filter(node => node.checked).map(node => node.label).join(',');
  177. }
  178. })
  179. .catch(e => {
  180. this.loading = false; // 数据加载失败,隐藏loading状态
  181. throw e;
  182. });
  183. } else if (this.checkType === '2') {
  184. projectRecordsService.list1({ status: 5, current: this.currentPage, pageSize: this.pageSize })
  185. .then(data => {
  186. this.loading = false; // 数据加载完成,隐藏loading状态
  187. // 检查新返回的数据是否已经存在,如果存在,则不添加到原始数据数组中
  188. let newData = data.records.filter(record => !this.data.some(item => item.id === record.id));
  189. if (this.currentPage === 1) {
  190. // 如果是加载第一页,则直接赋值给 data
  191. this.data = newData.map(item => ({ ...item, checked: false }));
  192. } else {
  193. // 如果不是第一页,则追加到原始数据数组后面
  194. this.data = [...this.data, ...newData.map(item => ({ ...item, checked: false }))];
  195. }
  196. if (data.records.length < this.pageSize) {
  197. this.allDataLoaded = true; // 如果返回的数据少于每页数量,则表示所有数据都已加载
  198. }
  199. if (this.value) {
  200. let keys = this.value.split(',');
  201. this.data.forEach(node => {
  202. if (keys.includes(node.id)) {
  203. node.checked = true;
  204. }
  205. });
  206. this.labels = this.data.filter(node => node.checked).map(node => node.label).join(',');
  207. }
  208. })
  209. .catch(e => {
  210. this.loading = false; // 数据加载失败,隐藏loading状态
  211. throw e;
  212. });
  213. }
  214. },
  215. loadMore() {
  216. this.currentPage++; // 增加当前页数
  217. this.loadData(); // 加载更多数据
  218. },
  219. onClose() {
  220. // 在关闭操作中清除已选择标记
  221. this.data.forEach(item => {
  222. item.checked = false;
  223. });
  224. this.labels = ''; // 清空标签
  225. this.show = false;
  226. this.checkType = '1';
  227. this.data = []; // 清空原始数据
  228. },
  229. checkTypeChange(value){
  230. this.checkType = value
  231. this.pageSize = 10
  232. this.currentPage = 1
  233. this.data = []; // 清空原始数据
  234. this.loadData(); // 加载更多数据
  235. },
  236. isEmpty(value) {
  237. let result = false;
  238. if (value == null || value == undefined) {
  239. result = true;
  240. }
  241. if (typeof value == 'string' && (value.replace(/\s+/g, "") == "" || value == "")) {
  242. result = true;
  243. }
  244. if (typeof value == "object" && value instanceof Array && value.length === 0) {
  245. result = true;
  246. }
  247. return result;
  248. },
  249. isNotEmpty (value) {
  250. return !this.isEmpty(value)
  251. },
  252. }
  253. };
  254. </script>
  255. <style scoped>
  256. .load-more-button {
  257. background-color: #409eff;
  258. color: #fff;
  259. border: none;
  260. padding: 10px 20px;
  261. border-radius: 4px;
  262. cursor: pointer;
  263. }
  264. </style>