vv-select-area.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <!-- @format -->
  2. <template>
  3. <!-- v-model -->
  4. <!-- 使用组件 -->
  5. <!-- <vvSelect placeholder="请选择所属类别" :list="categorys" label="name" valueKey="id" @change="pickerChange"></vvSelect> -->
  6. <picker
  7. mode="multiSelector"
  8. @change="bindMultiPickerChange"
  9. @columnchange="bindMultiPickerColumnChange"
  10. range-key="name"
  11. :value="multiIndex"
  12. :disabled="disabled"
  13. :range="multiArray"
  14. >
  15. <view class="vv-input__body text-right">
  16. <view class="vv-input__control" style="text-align: left">
  17. <text v-if="checkLabels" :class="{ disabled: disabled }">{{
  18. checkLabels
  19. }}</text>
  20. <text class="vv-input__placeholder" v-else>{{ placeholder }}</text>
  21. </view>
  22. <!-- <uni-icons type="arrowright" size="14" color="#808080"></uni-icons> -->
  23. </view>
  24. </picker>
  25. </template>
  26. <script>
  27. /**
  28. * vv-select 选择
  29. * @description picker再封装
  30. * @property {String} valueKey 绑定的key
  31. * @property {String} label 绑定的label
  32. * @property {Array} list 数据
  33. * @property {Array} level 层级 默认3级 即:省-市-区
  34. * @property {Boolean} disabled 是否禁用
  35. * @event {Function} change
  36. */
  37. export default {
  38. name: "vv-select-area",
  39. props: {
  40. value: {
  41. type: [Array],
  42. },
  43. placeholder: {
  44. type: String,
  45. default: "请选择",
  46. },
  47. list: {
  48. type: [Array],
  49. },
  50. level: {
  51. type: Number,
  52. default: 3,
  53. },
  54. disabled: {
  55. type: Boolean,
  56. default: false,
  57. },
  58. },
  59. data() {
  60. return {
  61. city: [],
  62. multiArray: [],
  63. multiIndex: [],
  64. checkLabels: "",
  65. checkValues: [],
  66. };
  67. },
  68. watch: {
  69. value: {
  70. handler: function (newVal) {
  71. if (this.list.length > 0 && this.value.length > 0) {
  72. this.init();
  73. }
  74. },
  75. immediate: true,
  76. },
  77. list: {
  78. handler: function (newVal) {
  79. if (this.list.length > 0) {
  80. this.init();
  81. }
  82. },
  83. immediate: true,
  84. },
  85. },
  86. created() {},
  87. methods: {
  88. init: function () {
  89. this.city = [...this.list];
  90. this.multiArray[0] = this.city;
  91. this.setMultiData(0, 0); // 默认值
  92. if (this.value && this.value.length > 0) {
  93. // 有初始值
  94. this.setCheck(this.value);
  95. }
  96. },
  97. setCheck: function (codes) {
  98. if (codes.length == 0) return false;
  99. let indexs = [];
  100. let _labes = [];
  101. for (let i = 0, l = codes.length; i < l; i++) {
  102. let index = this.multiArray[i].findIndex((item) => {
  103. return item.id == codes[i];
  104. });
  105. indexs.push(index);
  106. let item = this.multiArray[i].find((item) => {
  107. return item.id == codes[i];
  108. });
  109. _labes.push(item.name);
  110. this.setMultiData(i, index);
  111. }
  112. this.multiIndex = indexs;
  113. this.checkValues = codes;
  114. this.checkLabels = _labes.join("-");
  115. },
  116. setMultiData: function (column, index) {
  117. var data = {
  118. multiArray: this.multiArray,
  119. multiIndex: this.multiIndex,
  120. };
  121. data.multiIndex[column] = index; //
  122. switch (column) {
  123. case 0: //拖动第1列
  124. if (this.city[index].children) {
  125. data.multiArray[1] = this.city[index].children;
  126. if (data.multiArray[1][0] && data.multiArray[1][0].children) {
  127. data.multiArray[2] = data.multiArray[1][0].children;
  128. if (this.level > 3) {
  129. if (data.multiArray[2][0] && data.multiArray[2][0].children) {
  130. data.multiArray[3] = data.multiArray[2][0].children;
  131. } else {
  132. data.multiArray[3] = [];
  133. }
  134. }
  135. } else {
  136. data.multiArray[2] = [];
  137. if (this.level > 3) {
  138. data.multiArray[3] = [];
  139. }
  140. }
  141. } else {
  142. data.multiArray[1] = [];
  143. data.multiArray[2] = [];
  144. if (this.level > 3) {
  145. data.multiArray[3] = [];
  146. }
  147. }
  148. this.multiIndex.splice(1, 1, 0);
  149. this.multiIndex.splice(2, 1, 0);
  150. if (this.level > 3) {
  151. this.multiIndex.splice(3, 1, 0);
  152. }
  153. break;
  154. case 1: //拖动第2列
  155. if (data.multiArray[1][index] && data.multiArray[1][index].children) {
  156. data.multiArray[2] = data.multiArray[1][index].children;
  157. if (this.level > 3) {
  158. if (data.multiArray[2][0] && data.multiArray[2][0].children) {
  159. data.multiArray[3] = data.multiArray[2][0].children;
  160. } else {
  161. data.multiArray[3] = [];
  162. }
  163. }
  164. } else {
  165. data.multiArray[2] = [];
  166. if (this.level > 3) {
  167. data.multiArray[3] = [];
  168. }
  169. }
  170. this.multiIndex.splice(2, 1, 0);
  171. if (this.level > 3) {
  172. this.multiIndex.splice(3, 1, 0);
  173. }
  174. break;
  175. case 2: //拖动第3列
  176. if (this.level < 4) {
  177. break;
  178. }
  179. if (data.multiArray[2][index] && data.multiArray[2][index].children) {
  180. data.multiArray[3] = data.multiArray[2][index].children;
  181. } else {
  182. data.multiArray[3] = [];
  183. }
  184. this.multiIndex.splice(3, 1, 0);
  185. break;
  186. }
  187. this.multiArray = data.multiArray;
  188. this.multiIndex = data.multiIndex;
  189. this.$forceUpdate();
  190. },
  191. bindMultiPickerColumnChange: function (e) {
  192. this.setMultiData(e.detail.column, e.detail.value);
  193. },
  194. bindMultiPickerChange: function (e) {
  195. let _labes = [];
  196. let _values = [];
  197. for (let i = 0, l = this.multiArray.length; i < l; i++) {
  198. let item = this.multiArray[i];
  199. let index = this.multiIndex[i];
  200. if (item.length === 0) {
  201. break;
  202. }
  203. _labes.push(item[index].name);
  204. _values.push(item[index].id);
  205. }
  206. console.log(_labes, _values);
  207. // console.log(
  208. // this.multiArray[0][e.detail.value[0]],
  209. // this.multiArray[1][e.detail.value[1]]
  210. // ); // {value: "431000", label: "郴州市", level: 2}
  211. // this.multiIndex = e.detail.value;
  212. // this.$apply();
  213. this.checkLabels = _labes.join("-");
  214. this.checkValues = _values;
  215. // 返回这个值到父级
  216. this.$emit("change", _values);
  217. this.$emit("input", _values);
  218. this.$emit("on-change", _values);
  219. },
  220. },
  221. };
  222. </script>
  223. <style lang="scss" scoped></style>