You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

form-cascader.js 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. const reqInterface = require("../../../api/models");
  2. const {
  3. findidx,
  4. hasValue
  5. } = require("../../../utils/dataHelper");
  6. Component({
  7. properties: {
  8. dict: Number,
  9. label: String,
  10. value: String,
  11. position: {
  12. type: String,
  13. value: 'bottom'
  14. },
  15. requiredMark: {
  16. type: Boolean,
  17. value: true
  18. },
  19. activeMode: {
  20. type: Boolean,
  21. value: false
  22. },
  23. },
  24. observers: {
  25. 'dict': function (val) {
  26. let self = this;
  27. reqInterface.GetDictDetail({
  28. id: val
  29. }).then(res => {
  30. self.setData({
  31. spinning: false,
  32. cascaderOption: res.childs
  33. })
  34. }).catch(err => {
  35. self.setData({
  36. spinning: false,
  37. })
  38. })
  39. },
  40. 'label': function (val) {
  41. let self = this;
  42. self.setData({
  43. placeholder: '请选择' + val
  44. })
  45. },
  46. 'value': function (val) {
  47. let self = this;
  48. if (val) {
  49. console.log(val)
  50. self.setData({
  51. placeholder: '',
  52. child_value: val
  53. })
  54. }
  55. },
  56. },
  57. data: {
  58. cascaderOption: [],
  59. cascaderOptionNames: {
  60. label: 'name',
  61. value: 'id',
  62. children: 'childs'
  63. },
  64. child_value: '',
  65. select_value: [],
  66. show: false,
  67. spinning: true
  68. },
  69. methods: {
  70. openPopup() {
  71. this.setData({
  72. show: true
  73. })
  74. },
  75. close() {
  76. this.setData({
  77. show: false,
  78. select_value: []
  79. })
  80. },
  81. comfirm() {
  82. let self = this;
  83. self.triggerEvent('getvalue', { val: self.data.select_value, label: self.data.child_value})
  84. self.setData({
  85. show: false,
  86. select_value: [],
  87. })
  88. },
  89. onChange(e) {
  90. let self = this;
  91. console.log(e)
  92. self.data.child_value = e.detail.options.map(item => {
  93. return item.name
  94. }).join(',')
  95. self.setData({
  96. select_value: e.detail.value,
  97. child_value: self.data.child_value
  98. })
  99. },
  100. onLoadOptions(e) {
  101. let self = this;
  102. // self.setData({
  103. // spinning: true
  104. // })
  105. reqInterface.GetDictDetail({
  106. id: e.detail.value[e.detail.value.length - 1]
  107. }).then(res => {
  108. self.setData({
  109. spinning: false,
  110. })
  111. if(e.detail.value.length == 1) {
  112. let idx1 = findidx(self.data.cascaderOption, e.detail.value[0]);
  113. self.data.cascaderOption[idx1].childs = res.childs;
  114. self.setData({
  115. cascaderOption: self.data.cascaderOption
  116. })
  117. } else if (e.detail.value.length == 2) {
  118. let idx1 = findidx(self.data.cascaderOption, e.detail.value[0]);
  119. let idx2 = findidx(self.data.cascaderOption[idx1].childs, e.detail.value[1]);
  120. self.data.cascaderOption[idx1].childs[idx2].childs = res.childs;
  121. self.setData({
  122. cascaderOption: self.data.cascaderOption
  123. })
  124. } else if (e.detail.value.length == 3) {
  125. let idx1 = findidx(self.data.cascaderOption, e.detail.value[0]);
  126. let idx2 = findidx(self.data.cascaderOption[idx1].childs, e.detail.value[1]);
  127. let idx3 = findidx(self.data.cascaderOption[idx1].childs[idx2].childs, e.detail.value[2]);
  128. self.data.cascaderOption[idx1].childs[idx2].childs[idx3].childs = res.childs;
  129. self.setData({
  130. cascaderOption: self.data.cascaderOption
  131. })
  132. }
  133. }).catch(err => {
  134. self.setData({
  135. spinning: false,
  136. })
  137. })
  138. self.setData({ select_value: e.detail.value, cascaderOption: self.data.cascaderOption })
  139. }
  140. }
  141. })