select.js 4.9 KB

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