cl-select-region.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <view class="cl-select-region">
  3. <cl-select
  4. mode="multiSelector"
  5. :value="sel"
  6. :options="list"
  7. :label-key="labelKey"
  8. :value-key="valueKey"
  9. :separator="separator"
  10. :disabled="disabled"
  11. :border="border"
  12. set-options-is-parse-value
  13. @column-change="onColumnChange"
  14. @change="onChange"
  15. ></cl-select>
  16. </view>
  17. </template>
  18. <script>
  19. let cities = null;
  20. /**
  21. * select-region 下拉城市选择
  22. * @description 下拉城市选择
  23. * @tutorial https://docs.cool-js.com/uni/components/form/select-region.html
  24. * @property {null} value 绑定值
  25. * @property {String} api 城市数据接口,默认https://cool-uni.oss-cn-shanghai.aliyuncs.com/comm/city.json
  26. * @property {Array} options 城市数据列表
  27. * @property {String} disabled 是否禁用
  28. * @property {String} border 是否带有边框
  29. * @property {String} labelKey 内容关键字,默认label
  30. * @property {String} valueKey 值关键字,默认value
  31. * @property {String} separator 分隔符,默认 -
  32. * @example <cl-select-region />
  33. */
  34. export default {
  35. name: "cl-select-region",
  36. props: {
  37. // 绑定值
  38. value: Array,
  39. // 城市数据接口
  40. api: {
  41. type: String,
  42. default: "https://cool-uni.oss-cn-shanghai.aliyuncs.com/comm/city.json",
  43. },
  44. // 城市数据列表
  45. options: Array,
  46. // 是否禁用
  47. disabled: Boolean,
  48. // 是否带有边框
  49. border: Boolean,
  50. // 内容关键字,默认label
  51. labelKey: {
  52. type: String,
  53. default: "label",
  54. },
  55. // 值关键字,默认value
  56. valueKey: {
  57. type: String,
  58. default: "value",
  59. },
  60. // 分隔符,默认 -
  61. separator: {
  62. type: String,
  63. default: "-",
  64. },
  65. },
  66. data() {
  67. return {
  68. list: [[], [], []],
  69. sel: [],
  70. };
  71. },
  72. watch: {
  73. value: {
  74. immediate: true,
  75. handler(val) {
  76. // 获取城市数据
  77. if (cities) {
  78. this.onUpdate(this.value);
  79. } else {
  80. uni.request({
  81. url: this.api,
  82. success: (res) => {
  83. cities = res.data;
  84. this.onUpdate(this.value);
  85. },
  86. });
  87. }
  88. },
  89. },
  90. },
  91. methods: {
  92. onChange(arr) {
  93. this.sel = arr;
  94. this.$emit("input", arr);
  95. this.$emit("change", arr);
  96. },
  97. onUpdate([x, y, z]) {
  98. this.sel = [x, y, z];
  99. let a = 0;
  100. let b = 0;
  101. if (!x) {
  102. a = 0;
  103. b = 0;
  104. } else {
  105. a = cities.findIndex((e) => e[this.valueKey] == x);
  106. b = cities[a].children.findIndex((e) => e[this.valueKey] == y);
  107. }
  108. this.updateList([a, b]);
  109. },
  110. onColumnChange({ selects, column }) {
  111. this.updateList(selects.map((e) => (e < 0 ? 0 : e)));
  112. },
  113. updateList([a, b]) {
  114. this.list = [cities, cities[a].children, cities[a].children[b].children];
  115. },
  116. },
  117. };
  118. </script>