select.js 4.7 KB

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