select.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const _Http = getApp().globalData.http;
  2. Page({
  3. data: {
  4. params: {}, //请求体
  5. result: [], //返回结果
  6. radio: false, //是否为单选
  7. idname: "sa_contractid", //idkey
  8. showName: "title", //表单用 显示名称
  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. list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data)
  51. })
  52. })
  53. },
  54. /* 删除项 */
  55. deteleItem(id) {
  56. this.setData({
  57. list: this.data.list.filter(v => v[this.data.idname] != id),
  58. "params.content.total": this.data.params.content.total - 1
  59. })
  60. },
  61. /* 选中 */
  62. changeResult(e) {
  63. let {
  64. id
  65. } = e.currentTarget.dataset, result = this.data.result;
  66. if (this.data.radio) {
  67. result = [id];
  68. } else {
  69. result.some(v => v == id) ? result = result.filter(v => v != id) : result.push(id)
  70. }
  71. this.setData({
  72. result
  73. });
  74. if (this.data.radio) this.submit();
  75. },
  76. /* 提交 */
  77. submit() {
  78. let result = this.data.result,
  79. obj = this.data.radio ? {
  80. id: result,
  81. item: this.data.list.find(value => value[this.data.idname] == result),
  82. value: [this.data.list.find(value => value[this.data.idname] == result)[this.data.showName], result]
  83. } : {
  84. result,
  85. list: result.map(v => this.data.list.find(value => value[this.data.idname] == v)),
  86. value: [result.map(v => {
  87. let data = this.data.list.find(value => value[this.data.idname] == v);
  88. return data ? data[this.data.showName] : ""
  89. }), result]
  90. }
  91. getApp().globalData.handleSelect && getApp().globalData.handleSelect(obj)
  92. },
  93. /* 开始搜索 */
  94. startSearch({
  95. detail
  96. }) {
  97. let condition = this.data.content ? this.data.content.where.condition : this.data.params.content.where.condition;
  98. if (detail == condition) return;
  99. this.setData({
  100. 'content.where.condition': detail,
  101. 'params.content.where.condition': detail
  102. });
  103. this.getList(true);
  104. },
  105. /* 取消搜索 */
  106. onClear() {
  107. this.setData({
  108. 'content.where.condition': "",
  109. 'params.content.where.condition': ""
  110. });
  111. this.getList(true);
  112. },
  113. onReady() {
  114. this.selectComponent("#ListBox").setHeight(".total", this);
  115. },
  116. onUnload() {
  117. //回收数据
  118. getApp().globalData.handleSelect = null;
  119. }
  120. })