select.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const _Http = getApp().globalData.http;
  2. Page({
  3. data: {
  4. item: null, //自定义表单组件使用
  5. result: [], //选中结果
  6. radio: false, //是否为单选模式
  7. idName: 'contactsid', //选择ID
  8. list: []
  9. },
  10. onLoad(options) {
  11. if (options.item) {
  12. let item = JSON.parse(options.item);
  13. this.setData({
  14. item,
  15. radio: item.radio ? true : false,
  16. params: item.params,
  17. idName: item.params.idname ? item.params.idname : this.data.idName,
  18. result: item.value.length ? item.value[1].map(v => v + "") : []
  19. })
  20. };
  21. if (options.params) this.setData({
  22. params: JSON.parse(options.params)
  23. })
  24. if (options.idName) this.setData({
  25. idName: options.idName
  26. })
  27. if (options.radio) this.setData({
  28. radio: options.radio ? true : false
  29. })
  30. this.getList();
  31. },
  32. /* 提交 */
  33. submit() {
  34. let pages = getCurrentPages(), //页面
  35. item = this.data.item, //表单
  36. result = this.data.result, //选中结果
  37. list = this.data.result.map(value => this.data.list.find(v => v[this.data.idName] == value)); //选中列表
  38. try {
  39. if (item) { //判断是否表单调用,表单的话吧数据交还给表单
  40. let page = pages[pages.length - 2].selectComponent(item.model || "#Form");
  41. item.value = this.data.radio ? [list[0].name, result] : [list.map(v => v.name), result];
  42. page.handleRoute(item);
  43. } else if (this.data.params.model) {
  44. //判断是否上级页面中的组件调用
  45. let page = pages[pages.length - 2].selectComponent(this.data.params.model);
  46. page.handleSelectContacts(result, list)
  47. } else {
  48. //上级页面调用
  49. pages[pages.length - 2].handleSelectContacts(result, list)
  50. }
  51. } catch (e) {
  52. console.log(e)
  53. wx.showToast({
  54. title: '操作失败',
  55. icon: "none"
  56. })
  57. }
  58. },
  59. /* 开始搜索 */
  60. startSearch({
  61. detail
  62. }) {
  63. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  64. if (detail == condition) return;
  65. this.setData({
  66. 'content.where.condition': detail,
  67. 'params.content.where.condition': detail
  68. });
  69. this.getList(true);
  70. },
  71. /* 取消搜索 */
  72. onClear() {
  73. this.setData({
  74. 'content.where.condition': "",
  75. 'params.content.where.condition': ""
  76. });
  77. this.getList(true);
  78. },
  79. /* 选中 */
  80. onChange(e) {
  81. const id = e.currentTarget.dataset.item[this.data.idName] + "";
  82. if (!id) return;
  83. let result = this.data.result;
  84. if (this.data.radio) {
  85. result = [id]
  86. } else {
  87. if (result.some(v => v == id)) {
  88. result = result.filter(v => v != id)
  89. } else {
  90. result.push(id)
  91. }
  92. }
  93. this.setData({
  94. result
  95. })
  96. if (this.data.radio) this.submit();
  97. },
  98. //获取列表
  99. getList(init = false) {
  100. let params = this.data.params;
  101. if (init) params.content.pageNumber = 1;
  102. if (params.content.pageNumber > params.content.pageTotal) return;
  103. _Http.basic(params).then(res => {
  104. console.log("联系人", res)
  105. if (res.msg != '成功') return wx.showToast({
  106. title: res.data,
  107. icon: "none"
  108. });
  109. if (params.excludeid) res.data = res.data.filter(v => v[this.data.idName] != params.excludeid)
  110. this.setData({
  111. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
  112. "params.content.pageNumber": res.pageNumber + 1,
  113. "params.content.pageTotal": res.pageTotal,
  114. })
  115. })
  116. },
  117. onReachBottom() {
  118. this.getList();
  119. }
  120. })