select.js 4.7 KB

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