index.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <!-- 省市区选择组件 -->
  2. <template>
  3. <el-cascader
  4. ref="cascader"
  5. clearable
  6. :value="value"
  7. :options="regionsData"
  8. :placeholder="placeholder"
  9. popper-class="ele-pop-wrap-higher"
  10. :props="props"
  11. @input="updateValue"
  12. />
  13. </template>
  14. <script>
  15. import { getRegionsData } from './load-data';
  16. export default {
  17. name: 'RegionsSelect',
  18. props: {
  19. value: [Array, String, Number],
  20. placeholder: String,
  21. options: Array,
  22. valueField: {
  23. type: String,
  24. validator: (val) => {
  25. return !val || val === 'label';
  26. }
  27. },
  28. type: {
  29. type: String,
  30. validator: (type) => {
  31. return !type || ['provinceCity', 'province'].includes(type);
  32. }
  33. },
  34. props: Object
  35. },
  36. data() {
  37. return {
  38. // 级联选择器数据
  39. regionsData: []
  40. };
  41. },
  42. methods: {
  43. /* 更新 value */
  44. updateValue(value) {
  45. this.$emit('input', value);
  46. this.$emit('getCheckedNodes', this.$refs.cascader.getCheckedNodes());
  47. },
  48. /* 级联选择器数据 value 处理 */
  49. formatData(data) {
  50. if (this.valueField === 'label') {
  51. return data.map((d) => {
  52. const item = {
  53. label: d.label,
  54. value: d.label
  55. };
  56. if (d.children) {
  57. item.children = d.children.map((c) => {
  58. const cItem = {
  59. label: c.label,
  60. value: c.label
  61. };
  62. if (c.children) {
  63. cItem.children = c.children.map((cc) => {
  64. return {
  65. label: cc.label,
  66. value: cc.label
  67. };
  68. });
  69. }
  70. return cItem;
  71. });
  72. }
  73. return item;
  74. });
  75. } else {
  76. return data;
  77. }
  78. },
  79. /* 省市区数据筛选 */
  80. filterData(data) {
  81. if (this.type === 'provinceCity') {
  82. return this.formatData(
  83. data.map((d) => {
  84. const item = {
  85. label: d.label,
  86. value: d.value
  87. };
  88. if (d.children) {
  89. item.children = d.children.map((c) => {
  90. return {
  91. label: c.label,
  92. value: c.value
  93. };
  94. });
  95. }
  96. return item;
  97. })
  98. );
  99. } else if (this.type === 'province') {
  100. return this.formatData(
  101. data.map((d) => {
  102. return {
  103. label: d.label,
  104. value: d.value
  105. };
  106. })
  107. );
  108. } else {
  109. return this.formatData(data);
  110. }
  111. }
  112. },
  113. watch: {
  114. options: {
  115. handler(options) {
  116. this.regionsData = this.filterData(options ?? []);
  117. if (!options) {
  118. getRegionsData().then((data) => {
  119. this.regionsData = this.filterData(data ?? []);
  120. this.$emit('load-data-done', data);
  121. });
  122. }
  123. },
  124. immediate: true
  125. }
  126. }
  127. };
  128. </script>