select.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const _Http = getApp().globalData.http,
  2. currency = require("../../../utils/currency"),
  3. CNY = value => currency(value, {
  4. symbol: "¥",
  5. precision: 2
  6. }).format();
  7. Page({
  8. data: {
  9. params: {}, //请求体
  10. result: [], //返回结果
  11. radio: false, //是否为单选
  12. lastResult: [],
  13. idname: "sa_projectid", //idkey
  14. showName: "projectname", //表单用 显示名称
  15. },
  16. onLoad(options) {
  17. if (options.params) {
  18. let params = JSON.parse(options.params);
  19. if (!params.content.pageNumber || !params.content.pageTotal) {
  20. params.content.pageNumber = 1;
  21. params.content.pageTotal = 1;
  22. }
  23. this.setData({
  24. params
  25. });
  26. }
  27. if (options.value) {
  28. let value = JSON.parse(options.value)
  29. if (value.length) this.setData({
  30. result: value[1],
  31. lastResult: value[0].map((v, i) => {
  32. return {
  33. name: v,
  34. id: value[1][i]
  35. }
  36. })
  37. })
  38. }
  39. getApp().globalData.Language.getLanguagePackage(this, options.title || '选择项目');
  40. this.setData({
  41. radio: options.radio ? true : false,
  42. idname: options.idname || this.data.idname,
  43. showName: options.showName || this.data.showName,
  44. });
  45. this.getList()
  46. },
  47. getList(init = false) {
  48. //init 用于初始化分页
  49. if (init.detail != undefined) init = init.detail;
  50. let params = this.data.params;
  51. if (init) params.content.pageNumber = 1;
  52. if (params.content.pageNumber > params.content.pageTotal) return;
  53. _Http.basic(params).then(res => {
  54. console.log("选择项目列表", res)
  55. this.selectComponent('#ListBox').RefreshToComplete();
  56. if (res.code != '1') return wx.showToast({
  57. title: res.msg,
  58. icon: "none"
  59. })
  60. res.data = res.data.map(v => {
  61. v.signamount_due = CNY(v.signamount_due)
  62. return v
  63. })
  64. this.setData({
  65. 'params.content.pageNumber': res.pageNumber + 1,
  66. 'params.content.pageTotal': res.pageTotal,
  67. 'params.content.total': res.total,
  68. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data)
  69. })
  70. // this.getTags();
  71. })
  72. },
  73. /* 获取列表标签 */
  74. getTags() {
  75. let list = this.data.list,
  76. ownerids = list.map(v => v.sa_projectid);
  77. _Http.basic({
  78. "id": 20221018102001,
  79. "content": {
  80. nocache: true,
  81. "ownertable": "sa_project",
  82. ownerids
  83. }
  84. }).then(res => {
  85. console.log("标签", res)
  86. for (let key in res.data) {
  87. let index = list.findIndex(v => v.sa_projectid == key);
  88. list[index].tags = res.data[key]
  89. };
  90. console.log(list)
  91. this.setData({
  92. list
  93. })
  94. })
  95. },
  96. /* 删除项 */
  97. deteleItem(id) {
  98. this.setData({
  99. list: this.data.list.filter(v => v[this.data.idname] != id),
  100. "params.content.total": this.data.params.content.total - 1
  101. })
  102. },
  103. /* 选中 */
  104. changeResult(e) {
  105. let {
  106. id
  107. } = e.currentTarget.dataset, result = this.data.result;
  108. if (this.data.radio) {
  109. result = [id];
  110. } else {
  111. result.some(v => v == id) ? result = result.filter(v => v != id) : result.push(id)
  112. }
  113. this.setData({
  114. result
  115. });
  116. if (this.data.radio) this.submit();
  117. },
  118. /* 提交 */
  119. submit() {
  120. let result = this.data.result,
  121. obj = this.data.radio ? {
  122. id: result,
  123. item: this.data.list.find(value => value[this.data.idname] == result),
  124. value: [this.data.list.find(value => value[this.data.idname] == result)[this.data.showName], result]
  125. } : {
  126. result,
  127. list: result.map(v => this.data.list.find(value => value[this.data.idname] == v) || {}),
  128. value: [result.map(v => {
  129. let data = this.data.list.find(value => value[this.data.idname] == v) || this.data.lastResult.find(value => value.id == v);
  130. return data ? data[this.data.showName] || data.name : ""
  131. }), result]
  132. }
  133. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  134. },
  135. /* 开始搜索 */
  136. startSearch({
  137. detail
  138. }) {
  139. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  140. if (detail == condition) return;
  141. this.setData({
  142. 'content.where.condition': detail,
  143. 'params.content.where.condition': detail
  144. });
  145. this.getList(true);
  146. },
  147. /* 取消搜索 */
  148. onClear() {
  149. this.setData({
  150. 'content.where.condition': "",
  151. 'params.content.where.condition': ""
  152. });
  153. this.getList(true);
  154. },
  155. onUnload() {
  156. //回收数据
  157. getApp().globalData.handleSelect = null;
  158. }
  159. })