select.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. console.log(options)
  12. if (options.params) {
  13. let params = JSON.parse(options.params);
  14. if (!params.content.pageNumber || !params.content.pageTotal) {
  15. params.content.pageNumber = 1;
  16. params.content.pageTotal = 1;
  17. }
  18. this.setData({
  19. params
  20. });
  21. }
  22. if (options.title) wx.setNavigationBarTitle({
  23. title: options.title,
  24. })
  25. this.setData({
  26. radio: options.radio ? true : false,
  27. idname: options.idname || this.data.idname,
  28. showName: options.showName || this.data.showName,
  29. result: (!options.result || options.result == 'undefined') ? [] : JSON.parse(options.result)
  30. });
  31. this.getList()
  32. },
  33. getList(init = false) {
  34. //init 用于初始化分页
  35. if (init.detail != undefined) init = init.detail;
  36. let params = this.data.params;
  37. if (init) params.content.pageNumber = 1;
  38. if (params.content.pageNumber > params.content.pageTotal) return;
  39. _Http.basic(params).then(res => {
  40. console.log("选择客户列表", res)
  41. this.selectComponent('#ListBox').RefreshToComplete();
  42. if (res.msg != '成功') return wx.showToast({
  43. title: res.data,
  44. icon: "none"
  45. })
  46. if (params.excludeid) res.data = res.data.filter(v => v[this.data.idname] != params.excludeid);
  47. this.setData({
  48. 'params.content.pageNumber': res.pageNumber + 1,
  49. 'params.content.pageTotal': res.pageTotal,
  50. 'params.content.total': res.total,
  51. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data)
  52. })
  53. })
  54. },
  55. /* 删除项 */
  56. deteleItem(id) {
  57. this.setData({
  58. list: this.data.list.filter(v => v[this.data.idname] != id),
  59. "params.content.total": this.data.params.content.total - 1
  60. })
  61. },
  62. /* 选中 */
  63. changeResult(e) {
  64. let {
  65. id
  66. } = e.currentTarget.dataset, result = this.data.result;
  67. if (this.data.radio) {
  68. result = [id];
  69. } else {
  70. result.some(v => v == id) ? result = result.filter(v => v != id) : result.push(id)
  71. }
  72. this.setData({
  73. result
  74. });
  75. if (this.data.radio) this.submit();
  76. },
  77. /* 提交 */
  78. submit() {
  79. let result = this.data.result,
  80. obj = this.data.radio ? {
  81. id: result,
  82. item: this.data.list.find(value => value[this.data.idname] == result),
  83. value: [this.data.list.find(value => value[this.data.idname] == result)[this.data.showName], result]
  84. } : {
  85. result,
  86. list: result.map(v => this.data.list.find(value => value[this.data.idname] == v)),
  87. value: [result.map(v => {
  88. let data = this.data.list.find(value => value[this.data.idname] == v);
  89. return data ? data[this.data.showName] : ""
  90. }), result]
  91. }
  92. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  93. },
  94. /* 开始搜索 */
  95. startSearch({
  96. detail
  97. }) {
  98. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  99. if (detail == condition) return;
  100. this.setData({
  101. 'content.where.condition': detail,
  102. 'params.content.where.condition': detail
  103. });
  104. this.getList(true);
  105. },
  106. /* 取消搜索 */
  107. onClear() {
  108. this.setData({
  109. 'content.where.condition': "",
  110. 'params.content.where.condition': ""
  111. });
  112. this.getList(true);
  113. },
  114. onReady() {
  115. this.selectComponent("#ListBox").setHeight(".total", this);
  116. },
  117. onUnload() {
  118. //回收数据
  119. getApp().globalData.handleSelect = null;
  120. }
  121. })