select.js 4.8 KB

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