select2.js 4.9 KB

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